Connecting Tech Pros Worldwide Forums | Help | Site Map

Using Sessions to pass a value from one page to another in PHP

Newbie
 
Join Date: Jul 2008
Posts: 21
#1: Aug 12 '08
I use PHP sessions to pass a value from one page to another.



In my 1st page I pass a value called 'id' to my 2nd page using a hyperlink.

explained:

<a href="abc.php?id=<?php print"$image_id";?>"></a>

so in my 2nd page (abc.php) I capture the value for id using $_GET.

I want to know how I can do the same using session variables.

coz when I navigate away from abc.php and back it seems that the value in 'id' have disapeared.

I tried using this but doesnt seem to work. :(

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php session_start();
  3. session_start();    
  4. session_register ("pid");
  5. $pid = $_GET['id']; 
  6. $HTTP_SESSION_VARS ["pid"] = $pid;
  7. ?>
  8.  

Thanks in advance and pls help soon. I'm totally stuck without this.

By the way I hp I didnt giv a heartattack to the moderators again!

Needs Regular Fix
 
Join Date: Mar 2008
Posts: 311
#2: Aug 12 '08

re: Using Sessions to pass a value from one page to another in PHP


This should work better for you.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. session_start();
  4. $pid = $_GET['id']; 
  5. $_SESSION["pid"] = $pid;
  6.  
  7. ?>
  8.  
Unless you have a very old php installation, the above should work instead of the code you presented. This starts the session, then puts the value for the id in a PHP variable $pid, and in the next line, it creates a session variable (by adding an element to the $_SESSION array) with key "pid" and sets it to the value of the id stored in $pid. Note that these two lines can be combined, to $_SESSION["pid"] = $_GET['id'] without needing to use the intermediate variable $pid.

You may want to call trim() on the value of id first, to get rid of any leading or trailing spaces in case they occur.

Note that you also need to call session_start() in any later pages that wish to access the value of $_SESSION["pid"].
Newbie
 
Join Date: Jul 2008
Posts: 21
#3: Aug 12 '08

re: Using Sessions to pass a value from one page to another in PHP


Quote:

Originally Posted by coolsti

This should work better for you.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. session_start();
  4. $pid = $_GET['id']; 
  5. $_SESSION["pid"] = $pid;
  6.  
  7. ?>
  8.  
Unless you have a very old php installation, the above should work instead of the code you presented. This starts the session, then puts the value for the id in a PHP variable $pid, and in the next line, it creates a session variable (by adding an element to the $_SESSION array) with key "pid" and sets it to the value of the id stored in $pid. Note that these two lines can be combined, to $_SESSION["pid"] = $_GET['id'] without needing to use the intermediate variable $pid.

You may want to call trim() on the value of id first, to get rid of any leading or trailing spaces in case they occur.

Note that you also need to call session_start() in any later pages that wish to access the value of $_SESSION["pid"].




Hey thanks so much for your answer. but even this way i keep loosing the value when I navigate to another page and back from abc.php.

Well im thinkin may be cookies is the solution

like:

setcookie("cookname", $_SESSION["pid"], time()+60*60*24*100, "/");

if there's someother way smone pls let me know. thanks in advance!!


See what I want is to have a constant value for 'id' throughout my PHP pages.
I want the value for 'id' to be consistent throughout my pages (until the user selects another id)
Familiar Sight
 
Join Date: Sep 2007
Posts: 211
#4: Sep 7 '08

re: Using Sessions to pass a value from one page to another in PHP


Hi
Just make sure you use session_start() at the begining of both pages.

I don't see anything wrong with this code.

And there is one other possiblity. PHP should have complete access to the folder it stores the sessions on.
Reply