Jump to content

URL redirect issues


KevinM1

Recommended Posts

I currently have a script that redirects the user to another page if a form button is clicked.  This redirect works fine right now, but I need it to redirect the user to a different page based on certain conditions.  I've tried two things, and both have failed.  My problem will no doubt make more sense after I post the code of my two attempts.

 

Attempt #1:

<?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();
}

if(isset($_POST['submit'])){
   if($_SERVER['HTTP_REFERER'] == "http://www.thinkingmachinestore.com/thinkingmachine.php"){ //this if-block is the code in question
      $_SESSION['ip'] = urlencode(serialize($ip));
      $_SESSION['myCart'] = urlencode(serialize($myCart));
      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');

?>

 

Attempt #2:

<?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();
}

if(isset($_POST['submit'])){
   if(($tableName == "gaming_desktops") || ($tableName == "gaming_peripherals)){ //this if-block is the code in question
      $_SESSION['ip'] = urlencode(serialize($ip));
      $_SESSION['myCart'] = urlencode(serialize($myCart));
      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');

?>

 

Neither attempt redirected me back to thinkingmachine.php.  And yes, I made sure that I went to my viewcat.php script from thinkingmachine.php in the first case, and yes, I made sure that my $_GET['cat'] values were either "gaming_desktops" or "gaming_peripherals" in the second.

 

Any ideas on how I can get this to work?

Link to comment
Share on other sites

I can't find the edit post/modify post button, so sorry for this bump (EDIT: my OP doesn't have a modify button, but this post obviously does...strange).

 

I just checked to see if HTTP_REFERER was working properly on my server, and it is, so that's not an issue.

Link to comment
Share on other sites

Hi,

 

do u mind if i suggest you something like this, just use your form with a test.php file and try to redirect without using all your database connection and lengthy code, see if a simple redirection happens.

 

header("Location: http://www.thinkingmachinestore.com/thinkingmachine.php");

 

regards

 

Well, like I said before, I know that it's not a matter of sending the header as the current redirect (which sends everyone to thinkingmachinestore.com) works perfectly.  I also have other redirects in other scripts on the site that work perfectly.  It's just that when I try to use either HTTP_REFERER (which, like I said above, I've tested and confirmed is being set correctly) or the $tableName variable (which I also know is being set properly as my site would break if it wasn't) to redirect the user to a different page than the default, I get problems.

Link to comment
Share on other sites

i know this may sound stupid, but have your tried putting

 

error_reporting(0);

 

just above the header("Location: blablabla");

 

i have had problems with header references before and that sorted it, i dont know why tho.

 

try it and see how you get along :)

 

Sorry for the delay in responding, but I just tried this and it didn't work either.  Any other ideas?

Link to comment
Share on other sites

  • 7 months later...
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.