Forum Moderators: coopster

Message Too Old, No Replies

Header Location Function Not Working

Header Location Function

         

EricEC

5:46 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



Here is the jist of my script (some brackets are not there because this is not the entire script).

elseif ($action == "checkpassword")
{
$id = $_POST['update_link_id'];
$checkpassword = $_POST['update_link_password'];

//START
if(empty($checkpassword)){
$_SESSION['updatelinkstatus'] = "Invalid Password - Please Try Again<br><br>";
$_SESSION['allow_update_link_id'] = "";
$goback = "".$base_url."index.php?action=editlink&id=".$id."";
header("Location: $goback");
exit;
}

Is there a problem I am not seeing in the script here? The page stays on ...php?action=checkpassword and does not go to
"".$base_url."index.php?action=editlink&id=".$id."".

BarryStCyr

7:45 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



The most likely cause of header(location) failing is any output before the header statement is executed.

Check your script to make sure that nothing is output before the header statement. No echo, print or error messages.

EricEC

8:32 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



Now would that only matter in the { } brackets or throughout the entire page of script?

surrealillusions

11:13 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



It would matter throughout the entire page.

You cant output anything to the browser before the header. Which you are doing in that script in this bit

$_SESSION['updatelinkstatus'] = "Invalid Password - Please Try Again<br><br>";
$_SESSION['allow_update_link_id'] = "";
$goback = "".$base_url."index.php?action=editlink&id=".$id."";
header("Location: $goback");

janharders

11:21 pm on Jan 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure, someone please correct me if I'm wrong, but does php automatically send a 3xx-status header if a Location-header is encountered?
Usually, php would send a 200 (aka "OK" aka "Hey, I found what you were looking for, here's the result of your request"). In a 200 OK-Response, a Location-Header is of no use, because that's only needed for redirects (3xx status). Maybe you'll have to send a 3xx status, for example a 301 ...

header("Status: 301 Moved Permanently");
header("LOcation: " . $goback);

EricEC

2:56 am on Jan 17, 2009 (gmt 0)

10+ Year Member



I'll give it a whirl and see what happens. I'll post the results in a few

coopster

1:17 pm on Jan 17, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



PHP's header function with a "Location" is a 302 redirect. Any other type of response code requires a header sent before the "Location" header as demonstrated by janharders.

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless some 3xx status code has already been set.

You really should be sending an absolute URI, including scheme and host in that Location header. See more details on the header [php.net] manual page.

EricEC

5:18 pm on Jan 17, 2009 (gmt 0)

10+ Year Member



Thank you. I'll will be sure to check that out.

EricEC

9:23 pm on Jan 17, 2009 (gmt 0)

10+ Year Member



Excellent reference. Thank you very much.

Simply added...

<?php
ob_start();
?>

to the beginning of my script and the headers now work perfectly. Thanks for the help.