www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 03-04-2007, 08:22 AM
    Aarchaic Aarchaic is offline
    Registered User
     
    Join Date: Feb 2007
    Posts: 30
    Question Session Variables disapears

    Hello i have problem my session variables seem to disapear as i go along i've created this code to ilustrate whats happening

    First off i just post 3 detials like a name a age and a favourite number.

    that in turn goes to the next page where its being picked up by the php code and put into the Global Variable.

    the page uses the variables with no problem and you can select a link to go onto the next page where the number is being reworked using simple maths.

    and so it goes on when it comes to the last page it concats all the variables into a single global varible and send back to the 2nd page where it displays the all the numbers. but my posted values dispears at this stage. and if you run the same procedure again it shows the values is nothing and the number value is 0.

    Could some one please assist me in helping me retain these values. i think when i load the 2nd page again it creates either a new session ID or just loses the info completely cause the values are being reasigned in that page.

    Here is the code....

    Start.php
    PHP Code:

    <?php session_start()
        
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <form action="session.php" method="post" name="form1" id="form1">
        Enter your name:
        <input name="NAME" type="text" />
        <br />
        Enter your Age:
        <input name="AGE" type="text" />
        <br />
        Enter Your Favourite number:
        <input name="NUMBER" type="text" />
        <input name="submit" type="submit" value="Submit" />
    </form>    
    <body>
    </body>
    </html>
    session.php
    PHP Code:

    <?PHP
        header
    ("Expires: Thu, 17 May 2001 10:17:17 GMT");    // Date in the past
        
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
        
    header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
        
    header ("Pragma: no-cache");                          // HTTP/1.0

        
    session_start();
        
        
    $_SESSION['Begin']= "here";
        
    $_SESSION['Mid']= "we go";
        
    $_SESSION['End']= "again";

        
    $_SESSION['NUM']= $_POST['NUMBER']; // these are the values that gets lost the second round.
        
    $_SESSION['NAME']= $_POST['NAME'];     // these are the values that gets lost the second round.
        
    $_SESSION['AGE']= $_POST['AGE'];    // these are the values that gets lost the second round.
        
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Session testing</title>
    </head>

    <body>
            <?php echo 'Hello '.$_SESSION['NAME'].' how are you? i see you are '.$_SESSION['AGE']; ?>
            <br />
            <a href="page1.php">Page1.php</a>
            <br />
            <div align="center">
                <?php echo $_SESSION['total']; ?>
              <br />
              <br />
                <?php echo $_SESSION['CHANGES']; ?>          
            </div>
            
            
    </body>
    </html>
    Page1.php
    PHP Code:

    <?PHP
        header
    ("Expires: Thu, 17 May 2001 10:17:17 GMT");    // Date in the past
        
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
        
    header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
        
    header ("Pragma: no-cache");                          // HTTP/1.0

        
    session_start();
        
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Session testing</title>
    </head>

    <body>
            <p><?php echo $_SESSION['Begin']; ?>
                <br />
              <a href="page2.php">Page2.php</a></p>
              <?php $_SESSION['PAGE1'] = ($_SESSION['NUM'] / 2); ?>
            
    </body>
    </html>
    Page2.php

    PHP Code:

    <?PHP
        header
    ("Expires: Thu, 17 May 2001 10:17:17 GMT");    // Date in the past
        
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
        
    header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
        
    header ("Pragma: no-cache");                          // HTTP/1.0

        
    session_start();
        
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Session testing</title>
    </head>

    <body>
            <?php echo $_SESSION['Begin']; ?>
            <br />
            <?php echo $_SESSION['Mid']; ?>
            <br />
            <?php echo $_SESSION['PAGE1']; ?>
            <br />
            <a href="page3.php">Page3.php</a>
            <?php $_SESSION['PAGE2'] = ($_SESSION['NUM'] + 2); ?>
    </body>
    </html>
    Page3.php

    PHP Code:

    <?PHP
        header
    ("Expires: Thu, 17 May 2001 10:17:17 GMT");    // Date in the past
        
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
        
    header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
        
    header ("Pragma: no-cache");                          // HTTP/1.0

        
    session_start();
        
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Session testing</title>
    </head>

    <body>
            <?php echo $_SESSION['Begin']; ?>
            <br />
            <?php echo $_SESSION['Mid']; ?>
            <br />
            <?php echo $_SESSION['End']; ?>
            <br />
            <?php echo $_SESSION['PAGE1']; ?>
            <br />
            <?php echo $_SESSION['PAGE2']; ?>
            <br />
            <?php $_SESSION['total']= $_SESSION['Begin'].' '.$_SESSION['Mid'].' '.$_SESSION['End']; ?>
            <br />
            <a href="Session.php">Session.php</a>
            <?php $_SESSION['PAGE3'] = ($_SESSION['NUM'] * 9); ?>
            <?php $_SESSION['CHANGES'] = $_SESSION['PAGE1'].' '.$_SESSION['PAGE2'].' '.$_SESSION['PAGE3']; ?>
    </body>
    </html>
    I'm a complete noob to PHP and i'm trying to play around with the sessions to be able to apply them later.

    Thanks
    Reply With Quote
      #2  
    Old 03-04-2007, 09:32 AM
    stephan.gerlach stephan.gerlach is offline
    Registered User
     
    Join Date: Jan 2005
    Posts: 583
    Im not sure if this is really the problem but try this one.

    in every file move the
    PHP Code:
    session_start();
    at the very first line. even before the headers.

    hope this will work.
    Reply With Quote
      #3  
    Old 03-04-2007, 09:40 AM
    Aarchaic Aarchaic is offline
    Registered User
     
    Join Date: Feb 2007
    Posts: 30
    Quote:
    Originally Posted by stephan.gerlach
    Im not sure if this is really the problem but try this one.

    in every file move the
    PHP Code:
    session_start();
    at the very first line. even before the headers.

    hope this will work.
    Thanks but i dont think thats the problem the session does carry but once you reload the session.php file all the variable session variables disappears they have no value.

    Its like its trying to get the post from start.php but since that executed and moved onto the next page those values are lost and cant be recalled. so i need something that will be able to refer back to a previous page and retain the values.
    Reply With Quote
      #4  
    Old 03-04-2007, 10:50 AM
    carlh carlh is offline
    Registered User
     
    Join Date: Aug 2006
    Posts: 61
    try adding
    PHP Code:
    if(!isset($_SESSION['NUM'])) {
        
    $_SESSION['NUM']= $_POST['NUMBER']; // these are the values that gets lost the second round.
        
    $_SESSION['NAME']= $_POST['NAME'];     // these are the values that gets lost the second round.
        
    $_SESSION['AGE']= $_POST['AGE'];    // these are the values that gets lost the second round.
         
    }
    so the second time through it doesn't reassign from post values and dump the data
    Reply With Quote
      #5  
    Old 03-04-2007, 10:52 AM
    Aarchaic Aarchaic is offline
    Registered User
     
    Join Date: Feb 2007
    Posts: 30
    thanks i'll try that
    Reply With Quote
      #6  
    Old 03-04-2007, 10:57 AM
    Aarchaic Aarchaic is offline
    Registered User
     
    Join Date: Feb 2007
    Posts: 30
    Wink

    Quote:
    Originally Posted by carlh
    try adding
    PHP Code:
    if(!isset($_SESSION['NUM'])) {
        
    $_SESSION['NUM']= $_POST['NUMBER']; // these are the values that gets lost the second round.
        
    $_SESSION['NAME']= $_POST['NAME'];     // these are the values that gets lost the second round.
        
    $_SESSION['AGE']= $_POST['AGE'];    // these are the values that gets lost the second round.
         
    }
    so the second time through it doesn't reassign from post values and dump the data

    Thanks alot that help loads i doesnt lose the values now shows i'm still a complete noob :P
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:53 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.