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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nse111
    New Member
    • Jul 2008
    • 21

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

    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?i d=<?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. :(

    [CODE=PHP]

    <?php session_start() ;
    session_start() ;
    session_registe r ("pid");
    $pid = $_GET['id'];
    $HTTP_SESSION_V ARS ["pid"] = $pid;
    ?>
    [/CODE]


    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!
    Last edited by pbmods; Aug 12 '08, 11:19 AM. Reason: Fixed CODE tags.
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    This should work better for you.

    Code:
    <?php
    
    session_start();
    $pid = $_GET['id']; 
    $_SESSION["pid"] = $pid;
    
    ?>
    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"].

    Comment

    • nse111
      New Member
      • Jul 2008
      • 21

      #3
      Originally posted by coolsti
      This should work better for you.

      Code:
      <?php
      
      session_start();
      $pid = $_GET['id']; 
      $_SESSION["pid"] = $pid;
      
      ?>
      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("cook name", $_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)

      Comment

      • bnashenas1984
        Contributor
        • Sep 2007
        • 257

        #4
        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.

        Comment

        Working...