Jump to content

[SOLVED] Header redirect problem


KevinM1

Recommended Posts

This is actually an old problem that I've brought up here before, but no one was able to help me solve it.

 

I have a "Go Back" form button in a script that I want to redirect the user back to the index page in most cases, but to a different page if they came from that different page.  I've tried using $_SERVER[HTTP_REFERER] and checking for the values passed by $_GET (inventory categories/database table names), and neither method works.  Instead, I always get redirected back to the index page.  My code is below:

 

<?php

#viewcat.php script

session_start();
ob_start();

include('../php_config/config.php');
include('../dbconnect.php');
include('../templates/sub_header.inc');
include('../templates/isSafe.php');

if(isset($_GET['cat']) && isSafe($_GET['cat'])){
   $tableName = $_GET['cat'];
}

else{
   $_SESSION['ip'] = urlencode(serialize($ip));
   $_SESSION['myCart'] = urlencode(serialize($myCart));
   header("Location: http://www.thinkingmachinestore.com/");
   exit();
}

//This is the offending block of code ----------------------------
if(isset($_POST['submit'])){
   if($_SERVER[HTTP_REFERER] == "http://www.thinkingmachinestore.com/thinkingmachine.php"){
      $_SESSION['ip'] = urlencode(serialize($ip));
      $_SESSION['myCart'] = urlencode(serialize($myCart));
      error_reporting(0); //someone told me that putting an error_reporting statement may help.  I dunno why, but unsurprisingly, it didn't.
      header("Location: http://www.thinkingmachinestore.com/thinkingmachine.php");
      exit();
   }
//--------------------------------------------------------------

   else{
      $_SESSION['ip'] = urlencode(serialize($ip));
      $_SESSION['myCart'] = urlencode(serialize($myCart));
      header("Location: http://www.thinkingmachinestore.com/");
      exit();
   }
}

$query = "SELECT * FROM $tableName WHERE availability='y' ORDER BY price ASC";
$result = mysql_query($query);

echo "<div style='margin-left: auto; margin-right: auto; text-align: center;'><a href='viewcart.php'><img src='images/store/storefront_02.jpg' alt='' /></a><img src='../images/store/{$tableName}_banner.jpg' alt='' style='margin-top: 5px;' /><a href='checkout.php'><img src='../images/store/storefront_02a.jpg' alt='View Cart' /></a>\n<br /><br />\n";

if(mysql_num_rows($result) == 0){
   echo "All items of this category are currently out of stock.  Please check here again at a later time for product availability.<br />We apologize for any inconvenience this may cause.";
}

else{
   $count = 0;

   while($row = mysql_fetch_assoc($result)){
      $id = $row["$tableName" . "_id"];
      $pic = $row["pic_url"];
      echo "<a href='viewitem.php?cat=$tableName&id=$id'><img src='$pic' alt='' /></a>";
      $count++;

      if($count == 2){
         echo "<hr /><br />\n";
         $count = 0;
      }
   }
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit" value="Go Back" />
</form></div>

<?php

include('../templates/sub_footer.inc');

?>

 

Please help.

Link to comment
Share on other sites

try the javascript back button

 

<FORM><INPUT TYPE="BUTTON" VALUE="Go Back" 
ONCLICK="history.go(-1)"></FORM>

 

I might try that if I can't get the PHP version to work.

 

Are header redirects in PHP just generally troublesome?  I mean, almost every time I try having two different redirects in a script, one will work and the other won't, even though the syntax is correct.  Case in point:  I'm currently trimming down some of my other scripts.  I have two back buttons -- one to the previous page, one to the index.  The button that brings the user to the index works fine.  The other button doesn't, as it also brings the user back to the index.

 

(the code is:

<?php

if(isset($_GET['cat']) && isSafe($_GET['cat'])){
   $tableName = $_GET['cat'];
}

.
.
.

if(isset($_POST['back'])){
   $_SESSION['ip'] = urlencode(serialize($ip));
   $_SESSION['myCart'] = urlencode(serialize($myCart));
   header("Location: http://www.thinkingmachinestore.com/viewcat2.php?cat=$tableName");
   exit();
}

if(isset($_POST['restart'])){
   $_SESSION['ip'] = urlencode(serialize($ip));
   $_SESSION['myCart'] = urlencode(serialize($myCart));
   header("Location: http://www.thinkingmachinestore.com/");
   exit();
}

?>

.
.
.

<form name="iteminfo" action="<?php echo $_SERVER[php_SELF]; ?>" method="post" style="margin-left: auto; margin-right: auto; text-align: center;">
   <input type="submit" name="back" value="Go Back" />
   <input type="submit" name="restart" value="Go Back to the Beginning" />
</form>

)

 

Does PHP not allow two different redirects in one script?  Or does its header handling abilities more or less stink?  I honestly at a loss when it comes to this.

Link to comment
Share on other sites

The header info can only be sent to the browser once. So if the header info is already set then you can't do another redirect.

 

Right, but since I'm using an if/else conditional to specify which header info I want to send, I shouldn't be running into any problems, right?  That's why I'm so stumped on this -- I'm only sending one header, which is determined by the if/else conditions.

Link to comment
Share on other sites

I was having this problem recently and found that $_SERVER[HTTP_REFERER] is unstable and not to be trusted.

 

I now use the following on all my pages to achieve exactly the effect you seem to be after:

 

if ($_SESSION['currpage']) {
$_SESSION['prevpage']=$_SESSION['currpage'];
$_SESSION['prevquery']=$_SESSION['currquery'];
}
$_SESSION['currpage']=$_SERVER['PHP_SELF'];
$_SESSION['currquery']=$_SERVER['QUERY_STRING'];

 

The buttons then use a simple href command. Header is fairly troublesome (you have to have a full URL name, you can't pass in variables and you can't output any html before you use it (like echo).

 

I've steered clear.

Link to comment
Share on other sites

I figured it out!

 

Since I'm using a sticky form, the $_GET values weren't being set when the script was refreshed because the $_GET values were passed by the referring script only.  Simply putting a hidden input in the form and an extra elseif conditional fixed it.

 

Hooray! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.