CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    1

    Question Session passing through URL

    Hi, I'm struggling with the exact same thing (http://www.codeguru.com/forum/showthread.php?t=419897) so I thought I'd post here.

    I want to preserve the session id, via URL transmission, across three pages, but I can only get it to last for two pages, even though the code is basically the same.

    Once I go from the second page to the third, I get a new session id no matter what. Any help would be appreciated!

    php v5.2.14 , session.auto_start is off


    Here's page 1 (pledge1.php):
    Code:
    <?php
    ini_set('session.use_cookies',0);
    session_start();
    ?>
    
    <html>
    <body>
    
    
    <?php
    
    //if form has been submitted, validate data, store it in session, go to pledge review page
    if (isset($_POST['ReviewPledge'])) {
    
    $_SESSION['amount'] = $_POST['amount'];
    $url="Location: pledge2.php?".htmlentities(session_name().'='.session_id());
    session_write_close();
    header("$url");
    exit();
    }
    ?>
    
    <?php
    echo "session_id(): ".session_id();
    echo "<br>SID: ".htmlspecialchars(SID);
    echo  "<p>"
    ?>
    
    
    <form method="post" action="<?=$_SERVER['PHP_SELF']?>">
      Monthly pledge amount: 
      <label>
        <input type="text" name="amount" id="amount" size="2" maxlength="2"  value="<?php echo htmlspecialchars($_SESSION['amount']); ?>">
      </label>
    
    <p>
      <label>
        <input style="font-size:20"; type="submit" name="ReviewPledge" value="Submit pledge data">
      </label>
      </form>
    
    
    </body>
    </html>
    page 2 (pledge2.php):
    Code:
    <?php
    ini_set('session.use_cookies',0);
    session_id($_GET[htmlentities(session_name())]);
    session_start();
    ?>
    
    
    <?php
    //if the 'Submit my pledge' button was clicked, proceed
    // for some reason the stupid link action method isn't working! :S
     if(isset($_POST['SubmitPledge'])) {
    $url="Location: pledge3.php?".htmlentities(session_name().'='.session_id());
    session_write_close();
    header("$url");
    exit();
    }
    ?>
    
    <html>
    <body>
     
    <?php
    echo "session_id(): ".session_id();
    echo "<br>SID: ".htmlspecialchars(SID);
    echo  "<p>"
    ?>
    
       You have pledged: $
      
    <?php
    echo $_SESSION['amount'];
    ?>
      
      
    <FORM METHOD="LINK" ACTION="pledge1.php">
     <INPUT TYPE="submit" VALUE="Go back and change pledge info.">
     </FORM>
    
    <form method="post" action="<?=$_SERVER['PHP_SELF']?>">
    <input style="font-size:20"; type="submit" name="SubmitPledge" value="Submit my pledge">
    </form>
    
    <!-- 
     <FORM METHOD="LINK" ACTION="<?php echo 'pledge3.php?'.htmlentities(session_name().'='.session_id()).'/'; ?>">
     <INPUT TYPE="submit" VALUE="Submit my pledge">
     </FORM>
    -->  
    
    </body>
    </html>

    and here's page 3 (pledge3.php):
    Code:
    <?php
    ini_set('session.use_cookies',0);
    session_id($_GET[htmlentities(session_name())]);
    session_start();
    ?>
    
    
    <html>
    <body>
     
    <?php
    echo "session_id(): ".session_id();
    echo "<br>SID: ".htmlspecialchars(SID);
    echo  "<p>"
    ?>
    
       Thank you for your monthly pledge of: $
      
    <?php
    echo $_SESSION['amount'];
    ?>
    
    <p>e-mail confirmation to user</p>  
      
      
    </body>
    </html>
    Last edited by PeejAvery; June 14th, 2011 at 09:02 AM.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Session passing through URL

    [ split thread ]
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: Session passing through URL

    Welcome to the forums, psyke. Please don't resurrect old threads. If you have a question, create a new thread.

    To answer your question...Stop wasting your time trying to pass a URL variable across the pages. Just give your session a name before starting it. A cookie will automatically be generated to store your session data.

    PHP Code:
    <?php
    session_name
    ('mySessionName');
    session_start();
    ?>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured