KevinM1 Posted March 14, 2007 Share Posted March 14, 2007 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 https://forums.phpfreaks.com/topic/42683-url-redirect-issues/ Share on other sites More sharing options...
KevinM1 Posted March 14, 2007 Author Share Posted March 14, 2007 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 https://forums.phpfreaks.com/topic/42683-url-redirect-issues/#findComment-207104 Share on other sites More sharing options...
KevinM1 Posted March 14, 2007 Author Share Posted March 14, 2007 I hate to bump this up, but I really need to get this working correctly. Link to comment https://forums.phpfreaks.com/topic/42683-url-redirect-issues/#findComment-207130 Share on other sites More sharing options...
sayedsohail Posted March 14, 2007 Share Posted March 14, 2007 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 Link to comment https://forums.phpfreaks.com/topic/42683-url-redirect-issues/#findComment-207134 Share on other sites More sharing options...
KevinM1 Posted March 14, 2007 Author Share Posted March 14, 2007 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 https://forums.phpfreaks.com/topic/42683-url-redirect-issues/#findComment-207137 Share on other sites More sharing options...
flappy_warbucks Posted March 14, 2007 Share Posted March 14, 2007 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 Link to comment https://forums.phpfreaks.com/topic/42683-url-redirect-issues/#findComment-207142 Share on other sites More sharing options...
KevinM1 Posted March 16, 2007 Author Share Posted March 16, 2007 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 https://forums.phpfreaks.com/topic/42683-url-redirect-issues/#findComment-208845 Share on other sites More sharing options...
wolfcry Posted October 27, 2007 Share Posted October 27, 2007 Not sure if you solved this or not, but I noticed in your second attempt the following code: ($tableName == "gaming_peripherals) is missing the second quotation marks. Maybe that could be your conflict. Link to comment https://forums.phpfreaks.com/topic/42683-url-redirect-issues/#findComment-379066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.