richardjh Posted December 1, 2008 Share Posted December 1, 2008 Guys, I have a small script that should store four session variables and output them into a link but two of the variables are causing a problem. This script stores a category ID and name in two variables (cat_name & cat_id) and two genre variables (gen_name & and gen_id). The output is a link containing those four variables within a link which should lead back to the original 'genre' page. It works once or twice but if the user goes back and picks another genre in the SAME category then the cat_name & cat_id are lost. This causes the link to have these two variables missing (and hence the link breaks). I think I need a way to hold the cat_name & cat_id in place if the user only chooses a new genre but I'm at a loss how to achieve this. The bit of code i use is: if (empty($_SESSION['gen_name'])) { echo 'No Categories or Genres viewed yet!'; }else { // genres $_SESSION['gen_name'] = array_unique($_SESSION['gen_name']); $_SESSION['gen_id'] = array_unique($_SESSION['gen_id']); // categories $_SESSION['cat_name'] = array_unique($_SESSION['cat_name']); $_SESSION['cat_id'] = array_unique($_SESSION['cat_id']); for ( $x=0 ; $x < 6 ; $x++ ) { if (!empty($_SESSION['gen_name'][$x])) { # krsort($_SESSION['cat_id'][$x]); echo 'Genre Viewed: <a href = "../genre.php?cat=' . $_SESSION['cat_id'][$x] . '&catname=' . $_SESSION['cat_name'][$x] . '&genre=' . $_SESSION['gen_id'][$x] . '&genname=' . $_SESSION['gen_name'][$x] . '">' . $_SESSION['gen_name'][$x] . '</a> '. $_SESSION['cat_name'][$x] .'<br>'; if ($x == 5) { // genres unset($_SESSION['gen_name']); unset($_SESSION['gen_id']); // categories unset($_SESSION['cat_name']); unset($_SESSION['cat_id']); } } } // end the for loop } // end the session empty statement The category session variables are picked up when the user visits the category page: // session variables session_register ("cat_name"); // Create a session variable called job session_register ("cat_id"); // Create a session variable called job $HTTP_SESSION_VARS ["cat_name"] []= $catname; $HTTP_SESSION_VARS ["cat_id"] []= $cat; then when they choose a genre in that page they are lead to the genre page which has the genre session variables: // gen session variables session_register ("gen_name"); // Create a session variable called job session_register ("gen_id"); // Create a session variable called job #$HTTP_SESSION_VARS ["pages_visited"] []= $catname; $HTTP_SESSION_VARS ["gen_name"] []= $val2; $HTTP_SESSION_VARS ["gen_id"] []= $val4; the code at the top is contained in a side block which outputs the last 5 genres (and categories) To me it looks like each visit refreshes the genre but because the category is not updated then the varialbe is stored as an empty value (I think). any help much appreciated. R Link to comment https://forums.phpfreaks.com/topic/135019-session-variable-storing-problem/ Share on other sites More sharing options...
ScotDiddle Posted December 1, 2008 Share Posted December 1, 2008 richardjh, How are $catname, $cat, $val2 and $val4 being sent and intercepted. $_SESSION['whatEver'] type vars are EXACTALLY like ALL other PHP vars; they take on the value of the var (or constant) assigned to it. Check the $_GET and / or $_POST form values each time a user submits the form ( either with a " foreach() {echo $_GET / $_POST " or a " print_r() function " as soon as the values are supposed to be available, and make sure they are all there and within the range of values you are expecting. Scot L. Diddle, Richmond VA Link to comment https://forums.phpfreaks.com/topic/135019-session-variable-storing-problem/#findComment-703327 Share on other sites More sharing options...
kenrbnsn Posted December 1, 2008 Share Posted December 1, 2008 Your code seems to be based on very old code. Do not use then session_register function. It is obsolete. Instead use <?php $_SESSION['varname'] = 'value' ?> to set the value. Put <?php session_start(); ?> at the start of all scripts that access the session variables. Use $_SESSION instead of $HTTP_SESSION_VARS. Ken Link to comment https://forums.phpfreaks.com/topic/135019-session-variable-storing-problem/#findComment-703339 Share on other sites More sharing options...
richardjh Posted December 1, 2008 Author Share Posted December 1, 2008 Many thanks for your advice so far. It's much appreciated. I've take your advice Ken and altered my obsolete code. The variables (cat_name, cat_id, gen_name, gen_id) come for the URL. After further investigating I can see what's happening (although i can't fathom a solution). The variables ARE being stored correctly but what is causing the problem is the 'array_unique' part of the script here: if (empty($_SESSION['gen_name'])) { echo 'No Categories or Genres viewed yet!'; }else { $_SESSION['gen_name'] = array_unique($_SESSION['gen_name']); $_SESSION['gen_id'] = array_unique($_SESSION['gen_id']); // categories $_SESSION['cat_name'] = array_unique($_SESSION['cat_name']); $_SESSION['cat_id'] = array_unique($_SESSION['cat_id']); When the user visits the category and genre pages they pick up the correct FOUR variables fine and these get out put in my block correctly. However, if, say the user visits 'fiction' (cat_name & cat_id) and 'History' (gen_name & gen_id) the four variables each have a correct value. Now if the same user visits fiction again then chooses say 'crime' then the category 'fiction' is duplicated and not unique whereas 'crime' is new and unique. Therefore the script won't output the duplicate (fiction) because i have requested 'array_unique'. So the Four variables are now made up of two new one (crime - name and ID) and two duplicate ones (fiction - name and ID , neither one of these will be output because they ain't unique). Hence from that point on all the four variables are scrambled. I hope I explained that okay. AND I hope there is a solution. I have tried various combinations of array_unique (or not) but so far the solution eludes me. thanks for your time. R Link to comment https://forums.phpfreaks.com/topic/135019-session-variable-storing-problem/#findComment-703483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.