• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PHP web redirect code help

pushVTEC

Senior member
I'm trying to make a simple credentials checking php script. I have everything figured out except for after I verify them I want to re-direct their browser to a different page, how do I do that. Header() only works if it's the first thing that's called so that wont work, any other ideas or know of a site with some tips?
 
Actually now that I think about it would Header work? All I do after they submit the form is send them to a validate.php file which gets the post data and verifies it, could I then just do a header("Location: blah.com"); ?
 
Originally posted by: pushVTEC
Actually now that I think about it would Header work? All I do after they submit the form is send them to a validate.php file which gets the post data and verifies it, could I then just do a header("Location: blah.com"); ?

Yep that would work, as long as you don't display anything for that page 🙂
 
Originally posted by: screw3d
Originally posted by: pushVTEC
Actually now that I think about it would Header work? All I do after they submit the form is send them to a validate.php file which gets the post data and verifies it, could I then just do a header("Location: blah.com"); ?

Yep that would work, as long as you don't display anything for that page 🙂

:thumbsup:
 
By the way, just a heads up ...
by Yep that would work, as long as you don't display anything for that page, it also means that there should not be any blank space after the PHP closing ?> tag.. If you have any, then it will send output already, and the header() command will fail with headers already sent error..

Also, try to make the header("Location:"); point to the absolute (full) URL that you're redirecting to, instead of relative URL. I forgot why it is, but if you search for header() on php.net's website, you can see why from user comments.

So, use: header ("Location: http://mysite.com/hi.php");
instead of: header ("Location: hi.php");
 
Originally posted by: screw3d
Originally posted by: pushVTEC
Actually now that I think about it would Header work? All I do after they submit the form is send them to a validate.php file which gets the post data and verifies it, could I then just do a header("Location: blah.com"); ?

Yep that would work, as long as you don't display anything for that page 🙂

Why would it matter if he displays anything for that page? Any sane browser will follow the location header regardless of whether or not there's a body. And pushVTEC: so long as you've got a handy thing like php around the location header is definitely the preferred way to do the job. Good find.
 
Originally posted by: kamper
Originally posted by: screw3d
Originally posted by: pushVTEC
Actually now that I think about it would Header work? All I do after they submit the form is send them to a validate.php file which gets the post data and verifies it, could I then just do a header("Location: blah.com"); ?

Yep that would work, as long as you don't display anything for that page 🙂

Why would it matter if he displays anything for that page? Any sane browser will follow the location header regardless of whether or not there's a body. And pushVTEC: so long as you've got a handy thing like php around the location header is definitely the preferred way to do the job. Good find.

IRC, it won't let you send out headers more than once. The first time for the begining of that page, the second time when you try to redirect. It just can't output anything first.
 
Originally posted by: stndn
By the way, just a heads up ...
by Yep that would work, as long as you don't display anything for that page, it also means that there should not be any blank space after the PHP closing ?> tag.. If you have any, then it will send output already, and the header() command will fail with headers already sent error..

There should not be any blank space before the opening <? tag, after doesn't matter. The reason is that the spaces are interpreted as part of the http body and as soon as the interpreter sees that it sends all of the headers that it wants to send and then starts in with the body. Once that is done you can't go back and send more headers. But it's a good point, none-the-less. I wouldn't have seen it coming.

 
Originally posted by: kamper
Originally posted by: stndn
By the way, just a heads up ...
by Yep that would work, as long as you don't display anything for that page, it also means that there should not be any blank space after the PHP closing ?> tag.. If you have any, then it will send output already, and the header() command will fail with headers already sent error..

There should not be any blank space before the opening <? tag, after doesn't matter. The reason is that the spaces are interpreted as part of the http body and as soon as the interpreter sees that it sends all of the headers that it wants to send and then starts in with the body. Once that is done you can't go back and send more headers. But it's a good point, none-the-less. I wouldn't have seen it coming.

Exactly, otherwise you couln't make images with it. You have to send the header first then the output.
 
Oops, looks like I misread your post, screw3d. Sorry. I didn't see that you were thinking about him sending the header after having already done some output.
 
Ooopss... my bad.
i meant no white spaces before the opening <?php tag that contains the call to header() function..
not white space after the closing ?> tag...

let me rephrase that ...

if you are putting header in php tag:
<?php
header ("Location: http://mysite/hi.php");
?>

make sure that if you include anything before you get to the header part (be it from require_once(), include_once(), print, or whatever else), make sure that you don't have white spaces lying around your source code.
otherwise, the blank space will be sent to the browser, and after that no header() can be sent to the browser, resulting in header already sent error.

after the header, it won't matter if you put tens of thousands of millions of white spaces .. they won't affect the header()s

sorry for the confusion ... ><;;

[ disoriented morning ]
 
Back
Top