elrayyes Posted August 5, 2008 Share Posted August 5, 2008 As with alot of people on this forum, I am new to PHP. I managed to solve alot of problems (with trial and error of course). But my next question proved to be a bit of a challange to me; so I am reverting to you, the more experienced programmers, for some help on this. The situation: :-\ The site comprised of a product catalogue with various "child" categories. When you reach a specific product, the page comprises of product photo, details, and links to some PDFs (related literature). Some of these PDFs do not exist, so I managed to forward it to a 404 template using the .htaccess, while it still maintains the .pdf link in the address bar. The problem: ??? In the 404 template, I want to be able to post the product_id within my error message, but don't know how to retrieve the product_id variable from the previous page. Any help on the matter will be very much appreciated. Note: each page comprises of two parts, the PHP code, and the HTML template (which is called within the PHP). Link to comment https://forums.phpfreaks.com/topic/118286-retrieve-variable-from-a-previous-page/ Share on other sites More sharing options...
obsidian Posted August 5, 2008 Share Posted August 5, 2008 You have to pass the value to the 404 page in some way: whether than be a session variable or another global ($_POST or $_GET) is really up to you, but you cannot really "retrieve" a value from a page that is not part of your current page. Another possible solution is to check your $_SERVER['HTTP_REFERER'] and see what page sent them. This will allow you to parse out the data you need. However, as the PHP manual mentions, not all user agents set the HTTP_REFERER value, so it is not extremely reliable. Link to comment https://forums.phpfreaks.com/topic/118286-retrieve-variable-from-a-previous-page/#findComment-608698 Share on other sites More sharing options...
elrayyes Posted August 5, 2008 Author Share Posted August 5, 2008 Thank you obsidian for your reply. You have to pass the value to the 404 page in some way: whether than be a session variable or another global ($_POST or $_GET) is really up to you, but you cannot really "retrieve" a value from a page that is not part of your current page. Another possible solution is to check your $_SERVER['HTTP_REFERER'] and see what page sent them. This will allow you to parse out the data you need. However, as the PHP manual mentions, not all user agents set the HTTP_REFERER value, so it is not extremely reliable. I read a few articles in which it describes session as the best method, and tried to implement it, but nothing seems to happen. This is what I have done so far. In current page I added the following line: $_SESSION['no_page']=$_GET['product_id']; While in the next page I added the following: $error_product=$_SESSION['no_page']; I also added the following line in the HTML template of the error page: <?php echo $error_product; ?> I really don't know what seems to be wrong. Link to comment https://forums.phpfreaks.com/topic/118286-retrieve-variable-from-a-previous-page/#findComment-608860 Share on other sites More sharing options...
obsidian Posted August 5, 2008 Share Posted August 5, 2008 Before you can do anything with session variables, you must start the session on both pages. Try adding a call to session_start() to the top of both scripts and see if that clears things up. Link to comment https://forums.phpfreaks.com/topic/118286-retrieve-variable-from-a-previous-page/#findComment-608864 Share on other sites More sharing options...
elrayyes Posted August 5, 2008 Author Share Posted August 5, 2008 Before you can do anything with session variables, you must start the session on both pages. Try adding a call to session_start() to the top of both scripts and see if that clears things up. I tried this method, but still. Below is the first part of the source file: <?php include('../php_library/inc.php'); db_connect(); session_start(); $header=checkTemplate('head'); $content=checkTemplate('product_content'); $footer=checkTemplate('foot'); $product_id=$_GET['product_id']; $source=$_GET['source']; $level2=$_GET['l2']; $level3=$_GET['l3']; $level4=$_GET['l4']; $_SESSION['no_page']=$_GET['product_id']; $query="SELECT * FROM Products WHERE ID='$product_id'"; $result=mysql_query($query); if(mysql_num_rows($result)!=0) ... And here is the first part of the second code: <?php //php library inc php //database connect include('../php_library/inc.php'); db_connect(); session_start(); $header = checkTemplate("head"); $content = checkTemplate("no_content"); $footer = checkTemplate("foot"); $error_product=$_SESSION['no_page']; $query="SELECT * FROM Products WHERE ID='$product_id'"; $result=mysql_query($query); if(mysql_num_rows($result)!=0) { ... Did I place them in the wrong place? Link to comment https://forums.phpfreaks.com/topic/118286-retrieve-variable-from-a-previous-page/#findComment-608929 Share on other sites More sharing options...
DjMikeWatt Posted August 5, 2008 Share Posted August 5, 2008 Someone correct me if I'm wrong here... but wouldn't it be WAYYY easier to just include a hidden form on the product page that submits to the 404 page? You could use a javascript to submit on_blur or on_change or something like that... then, the product_id (in the hidden form field) gets passed to the 404 page every time... you could use $_POST or $_GET, but I would think that a $_GET would be easiest in this case (so that the product_id appears in the URL... Again, I'm still learning, too - but it seems like this would work well and be simple to implement. You could set the product_id by hand, or, if the product page obtains it from a DB then you could just fill it automatically from the DB... Thoughts? Link to comment https://forums.phpfreaks.com/topic/118286-retrieve-variable-from-a-previous-page/#findComment-608985 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.