Jump to content

SESSION help


bassdog65

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.