bassdog65 Posted October 21, 2008 Share Posted October 21, 2008 Ok, so still new to PHP, but having a little trouble grasping the process for this, so any help would be appreciated. I basically have three images, and what I want to do is redirect the user to a given page when one of the images is clicked, and for it to enter a session variable for whatever choice they pick. Example: Image1.gif links to index2.php and enters a session value of 1 Image2.gif links to index3.php and enters a session value of 2 Image3.gif links to index4.php and enters a session value of 3 Then I can reference the choice they picked on page one anywhere else on the site as long as the session is active. So when on any subsequent page i could use isset to call up options for when each variable is set. I know this will make alot of sense to all of you, because clearly it can be done, I'm just really going in circles trying to get it done. Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 21, 2008 Share Posted October 21, 2008 probably the best way to get this done is using first a GET, which then is put into a Session Variable. Page with Image Link... <a href="index.php?image=1"><img src="Image1.gif"></img></a> Now index.php... <?php if(isset($_GET['image'])) { $_SESSION['image'] = "Image".$_GET['image'].".gif"; echo "Image".$_GET['image'].".gif was chosen!"; } else { echo "No Image was chosen!"; } ?> That should give you a start, and idea of how to get it done. Regards ACE Quote Link to comment Share on other sites More sharing options...
bassdog65 Posted October 21, 2008 Author Share Posted October 21, 2008 Perfect! I always say "duh" when i read the answer. Now I have another question. If I pass id "image=1" to index.php, how can I enter that into a session to be used throughout the rest of the site? I tried $_SESSION['image'] = $_GET['image']; but then to test I tried echo $_SESSION['image']; It should print a 1, 2, or 3 depending on which image they clicked, but it always prints a 1. When I do this... echo $_GET['image']; it shows the correct ID for the image clicked, so the ID is passing correctly, but it is not getting entered into the session. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 21, 2008 Share Posted October 21, 2008 <?php session_start(); $_SESSION['image'] = $_GET['image']; ?> Are you using session_start at the top? Quote Link to comment Share on other sites More sharing options...
bassdog65 Posted October 21, 2008 Author Share Posted October 21, 2008 I have session_start() at the top, but then I have the HTML tags and everything, and then i go back into PHP to do the interpreting, its just a test script right now, but here is what it looks like. <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Test</title> </head> <body> <?php if(isset($_GET['lid'])) { $_SESSION['lid'] = $_GET['lid']; if ($_SESSION['lid'] = "1") { echo $_GET['lid']; echo $_SESSION['lid']; } elseif ($_SESSION['lid'] = "2") { echo $_GET['lid']; echo $_SESSION['lid']; } elseif ($_SESSION['lid'] = "3") { echo $_GET['lid']; echo $_SESSION['lid']; } else { echo 'Failed'; } } else { echo "No Location ID was chosen!"; } ?> </body> </html> So when I go to the actual page, it should display 11 or 22 or 33 if everything is working corectly, and it always says 11, 21, or 31...meaning the SESSION value isn't getting set. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 21, 2008 Share Posted October 21, 2008 Do you have access to your php.ini file, and can you verify your session save path? Can you make 2 simple pages, one that stores the session, and a 2nd that reads it, nothing more? Are you using a relative link to call the 2nd page? Quote Link to comment Share on other sites More sharing options...
bassdog65 Posted October 21, 2008 Author Share Posted October 21, 2008 first page is index.php and the links in it are: index2.php?lid=1 index2.php?lid=2 index2.php?lid=3 then index2.php is this script i just posted. I don't understand what you are syaing about the two simple pages. index2.php should be a simple script that stores whatever the get pulled from index.php and store it into a session so i can use it on any other subsequent page, right? Quote Link to comment Share on other sites More sharing options...
revraz Posted October 21, 2008 Share Posted October 21, 2008 Launch page1.php, then page2.php page1.php <?php session_start(); $_SESSION['set']=1; echo "Session Set"; ?> page2.php <?php session_start(); echo $_SESSION['set']; ?> Quote Link to comment Share on other sites More sharing options...
bassdog65 Posted October 21, 2008 Author Share Posted October 21, 2008 That worked. Then I changed the value for 1 to 156 on page1.php and it worked. Could it be that I start my session and then do the HTML tags that is interfering? I'm confused. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 21, 2008 Share Posted October 21, 2008 You just need session_start() at the top of every page so it will carry the session data from one page to the next. Anything else on that page (HTML/whatever) wouldn't affect anything. Quote Link to comment Share on other sites More sharing options...
bassdog65 Posted October 21, 2008 Author Share Posted October 21, 2008 thats what I thought, but I jsut cant figure out why the information isn't getting stored into the session on teh script i posted above. Quote Link to comment Share on other sites More sharing options...
bassdog65 Posted October 21, 2008 Author Share Posted October 21, 2008 I think I solved my own problem, for those who are interested, I realized that in my "if" statement, where i said, if ($_SESSION['lid'] = "1" I was overriding where the SESSION was set earlier and setting it to a value of "1" Stupid mistake, but no one else seemed to catch it either. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 21, 2008 Share Posted October 21, 2008 Ah, the 'ol == monster Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.