Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP redirect won't work

Status
Not open for further replies.

GKChesterton

Programmer
Aug 17, 2006
278
US
I built some code around redirects:

header( "Location: http://localhost/mainmenu.php" );

and it worked fine on my localhost workstation. But when I uploaded them to the ISP, using of course a new location like

header( "Location: http://www.mydomain.com/mainmenu.php" );

the redirects don't work at all. The rest of the script runs. Any idea why? Is it a php.ini setting? Is there an alternative to header()?

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
when you say "won't work" i assume that there are no errors being thrown that you can see? and you absolutely definitely 100% have display_errors set to on and error_reporting set to E_ALL in php.ini? check phpinfo() if you are unsure.

there is no reason why header will not work to redirect output UNLESS you are somhow sending data to the browser before the header call. this would normally throw an error but see above.

 
I am having a similar problem. Have you got any answers to this question?
 
please post back the results of this script

Code:
<?php
echo "Value for display errors: " .ini_get("display_errors") ."<br/>";
echo "value for error reporting: " . ini_get("error_reporting");
?>
 
Value for display errors:
value for error reporting: 2047

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
so ...
turn display errors to on and turn error reporting to E_ALL. currently it is set as E_STRICT but not E_ERROR (which i think is an odd setup). these settings are most easily made in your php.ini file.

likely as not, when you do the above, you will find that php spits a bunch of errors out about not being able to send headers because data has already been sent. this will allow you to debug the problem.
 
I don't see where my ISP will let me change php.ini. I searched for any available toggles on their site, but no. I found a download for an error log; the few records for today did not look relevant (File does not exist, directory index forbidden ... )

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
create a blank file locally called php.ini and add the following to it

Code:
display_errors = On
error_reporting = E_ALL

upload it to the directory in which you are testing your scripts.
 
Done. No changes in behavior reported at the start.

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
and I get the same

Value for display errors:
value for error reporting: 2047

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
You do realize that this will not be the same in your machine and in your hosting server right?


This would probably point to the index page of your hosting company. I would suggest relative redirects instead of absolute. something like:

./somefolder/mainmenu.php instead.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
My breakthrough came from a different direction -- jpadie got me off of header(). So I am moving on. Vacunita, thanks for the tip, I was succeeding in not mixing that up I believe.

jpadie, you asked about phpinfo() -- here are some lines (yes, from the domain name!):[tt]
display_errors Off Off
display_startup_errors Off Off
error_log no value no value
error_prepend_string no value no value
error_reporting 2047 2047
[/tt]
So I infer that the inserted php.ini should override these values? But no ... I certainly will check on this thread again, but the felt need is answered.

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
yes, php.ini should have overriden them.

if you do not have parse errors in your script this code should do the same

Code:
ini_set("display_errors", "on");
error_reporting(E_ALL);
 
I have finally got the solution for the problem. My header redirects were also refusing to work and it has something to do with buffering caused by having other code before the header line.
Go to php.ini and change the setting for output_buffering. Set it to:
output_buffering = 4096;
It should work. Mine is now working!!
 
i think your issue (jomuga) is specific to you. output buffering is not always used by developers (in fact these days I never use it other than to force an evaluation of an external file). the problem you were having is that before you'd performed the header redirect you have filled up the buffer and so php had sent it to the browser. this means that the headers had already been sent and you could not then send another to force the redirect.

To my mind, a well designed script should not send any information to the browser (or the output buffer) if there is a chance of a redirect until such time as the decision has crystallised one way or another. I don't personally feel that output buffering is the right way to achieve this goal. Either the logical tests should be done earlier in the script or you should store your output in a programmatic variable until you are ready to use it.

 
Thank you jpadie, your comment has explained to me the concept of buffering. I may have to redesign my script. In fact my script was checking the user login credentials and if successful would establish a session variable and then redirect the user back to the page he/she came from. There after there would be no need to login.
I think i should have to redesign my script.
GK's problem must be because he has different settings in the php.ini on his development machine and the hosting server.
Thanks once again.
 
Pete
It works if you set output_buffering on. An example of a script where it works is below:

<?php

if ( (!isset($_REQUEST['username'])) || (!isset($_REQUEST['password'])) )
{
echo 'You must enter your username and password to proceed';
exit;
}

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

if (login($username, $password))
{
$_SESSION['auth_user'] = $username;
header('Location: }
else
{
echo 'The password you entered is incorrect';
exit;
}
?>

The script checks whether the two variables are set and if not it calls the login form. If the two variables are present the script then calls the login function to validate the user and if it returns true the user is directed to the specified url.
 

If you have a deadline and header location is not working, you can use HTML meta refresh which always works, anytime anywhere in the code, as follows:

HTML version:
Code:
<meta http-equiv="refresh" content="0;url=menu.php>


PHP version (if you need to send data to another php file):
Code:
echo '<meta http-equiv="refresh" content="0;url=menu.php?member_id=' . $mv_member_id . ' " />';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top