PHP web redirect code help

pushVTEC

Senior member
Aug 30, 2003
265
0
0
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?
 

pushVTEC

Senior member
Aug 30, 2003
265
0
0
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"); ?
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
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 :)
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
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:
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
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");
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
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.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
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.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
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.

 

AFB

Lifer
Jan 10, 2004
10,718
3
0
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.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
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.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
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 ]