I'm having problems keeping a variable I need across multiple pages. I'm making a simple web for where I work that is comprised of 8 html pages containing forms. The reason why 8 seperate pages are needed is to break it down into easy to manage components for both myself and users. After the user enters information on the first page(using form fields such as pull down menus and text fields), that data is sent to a php script(on another file) using the POST method. That php code then writes the post data to a database. After running the php section in that file, the <body> tag of the HTML section uses the "onload" attribute to bring the user to the next page so that more information can be written. This process of brining up an html form page to enter data, followed by writing the data to a database with PHP, then bringing up another html form page, continues until the final page. But it doesn't work. After it finishes writing the second page and calls up the 3rd to enter data,it appears to loose the session and the session variable no longer has its value. The 3rd page, and all after don't write anything because the value of the primary key is not kept. I have verified this by no loading the rest of the pages and working page by page.

So, why is my sesion being dropped after the 3rd time its called? Is there an easier way to store a variable that has to be accessed across multiple php pages without using sessions? I thought about using a hidden field in the <FORM> section, but it will not work to suit my needs. I'm new at session so feel free to add any additional help that I might find usefull.

I'm not a programmer and this is the first I've used PHP, so if my method sounds difficult or confusing, feel free to recommend something better. From what I've read so far, this sounded like the easiest way to accomplish this. Sessions appear not to be working as my book or any web texts describe.

Thanks for all your help.

Ben

    on each page you need to have the following

    <?session_start();
    $_SESSION['variablename'];

    if (!$_SESSION['variablename']){ //if not present assuming this is not the setting page
    header("location:../login.php"); //redirect to login page
    }else{

    //rest of code for page

    hth

      Hi there,

      I have the same problem. I "was" using hidden fields, but that was too much... so I changed to session variables. From page1 to page2, no problem, I get all the variables. My problem is that in page3 I need to get the info from page1 and page2. Page2 variables are there, but I lose the first ones.... what gives? Here is some code...
      Page 1

      <form action="page2.php" method="POST">
      ......
      <input size=25 type=text name="FirstName">
      .......
      

      Page 2

      <?php
      /* This page receives and handles the data generated by "page1.php". */
      session_start();
      // Assign the post variables to session variables.
      $_SESSION['FirstName'] = $_POST['FirstName'];
      .......
      <form action=page3.php method=post>
      .......
      <input type=text size=5 name=Waist> Inches.
      

      Page3

      <?php
      session_start();
      // Assign the post variables to session variables.
      $_SESSION['FirstName'] = $_POST['FirstName'];
      $_SESSION['Waist'] = $_POST['Waist'];
      

      If I print/echo both variables in page 3, I only get the one for $_POST['Waist'].....
      Any ideas?

        Write a Reply...