-1

I'm trying to understand session variables by trying this code. For the purpose of the test, I created 3 pages and tried to pass the session variables around. I noticed that, the session variable is not populated in the 3rd page. I have searched to see where the problem is and I'm yet to figure it out.

page1

<!DOCTYPE html>
<html lang="en">    
    <head>
        <meta charset="UTF-8">
        <title>Bio Data</title>
    </head> 
    <body>
        <form action="process.php" method="post">
            <label for="fst_name">
                First Name
                <input type="text" name="fst_name" id="fst_name">
            </label>

            <label for="lst_name">
                Last Name
                <input type="text" name="lst_name" id="lst_name">
            </label>

            <input name="submit" type="submit" value=" Login ">
        </form>
    </body>
</html>

page2

<?php
session_start();

if (isset($_POST["submit"])){           
    $_SESSION['fst_name'] = $_POST['fst_name'] ;
    $_SESSION['lst_name' ] = $_POST['lst_name'] ;
    header("location:display.php");
}else{
    echo "Error";
}

page3

<?php
session_start();
echo $_SESSION['fst_name'];
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Biodata Display</title>
    </head>
    <body>
        <table>
            <tr>
                <td><?php echo "Hello " .  $_SESSION['fst_name']; ?></td>
                <td><?php echo $_SESSION['lst_name']; ?></td>
            </tr>
        </table>    
    </body>
</html>
Jo_whiteX
  • 1
  • 2
  • this looks allright to me.. have you tested if you have `$_SESSION['fst_name']` in page2 with the expected value, or does $_POST have the expected values? – Jeff Oct 15 '16 at 22:52
  • *"the session variable is not populated in the 3rd page"* - Which one, you have two and can't see where you've assigned anything to `$_SESSION['last']`. – Funk Forty Niner Oct 16 '16 at 02:37
  • @Jeff `$_SESSION['fst_name`] have values on page2. @Fred-ii- I have corrected the code. None of the session variable appears on page3. – Jo_whiteX Oct 16 '16 at 07:08

1 Answers1

1

Sessions in PHP work by saving a data file server side referenced by a special cookie (by default named PHPSESSID), so when your browser sends the proper cookie PHP fetches from the saved data files the previous $_SESSION value. This means that:

  1. Your server must be able to write files (usually in tmp dir, check PHP INI settings related to session save path
  2. Your browser must accept cookies (you can easily check this by dumping $_COOKIE)

To simplify your test, create a single page like this:

<?php
session_start();
header("Content-Type: text/plain");
var_dump($_COOKIE);
var_dump($_SESSION);
$_SESSION['hello'] = "world";

After you load this page TWICE, you should see the right data in $_SESSION and $_COOKIE. If it doesn't work, your problem is in your server/environment. If that works, I'll look again at your code but it looked fine on first sight.

Johnny
  • 1,770
  • 12
  • 16