Session problem

On the index page I got this:

session_start();

and this form:

<form action="2.php" method="post">
<input name="fnamn" type="text" id="fnamn" />
<br />
<input name="enamn" type="text" id="enamn" />
<br />
<input name="knapp" type="submit" id="knapp" value="Klicka" />
</form>

On page 2.php I got this:

session_start();
$_SESSION['fnamn'] = $_POST['fnamn'];
$_SESSION['enamn'] = $_POST['enamn'];
echo $_SESSION['fnamn'] . " " . $_SESSION['enamn'];

This prints out the two session variables on 2.php. I also have a link to page 3.php on 2.php:

<a href="3.php">3</a>

This is 3.php:

session_start();
$_SESSION['fnamn'] = $_POST['fnamn'];
$_SESSION['enamn'] = $_POST['enamn'];
echo $_SESSION['fnamn'] . " " . $_SESSION['enamn'];

Clicking the link on 2.php takes me to 3.php, but the session variables will not be transferred over to the third page. What am I doing wrong?:confused:

Ok, thank you all for your highly valuable comments!:slight_smile:

You must be clear that the 2.php file should only be possible to access via a form submission. If you want to access 2.php in other actions apart from the form submission then you should check something like this while assigning the values in the sessions variables.


session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $_SESSION['fnamn'] = $_POST['fnamn'];
    $_SESSION['enamn'] = $_POST['enamn'];
}
if(isset($_SESSION['fnamn']) && isset($_SESSION['enamn'])){
    echo $_SESSION['fnamn'] . " " . $_SESSION['enamn'];
}
else{
    echo 'No sessions are set yet.';
}

So not the second if condition here to check whether the sessions variables are set with some values or not. This prevents from notices in error reporting E_ALL case.

2.php
There was something in the session vars, but you just wiped them out with:

$_SESSION[‘fnamn’] = $_POST[‘fnamn’];
$_SESSION[‘enamn’] = $_POST[‘enamn’];

You have to go 1.php -> 2.php to make sure the post variables are set, you cannot do 3.php -> 2.php without having some conditional code checking if the post vars are there. So 2.php might look like:


if( isset( $_POST ) ){

  $_SESSION['fnamn'] = $_POST['fnamn']; 
  $_SESSION['enamn'] = $_POST['enamn'];

}

*untested

But there should be something in the $_SESSION variables from the foregoing page?

Thank you, it works. I have one more question. Here’s my php files:

index.php:

session_start();
echo $_SESSION['fnamn'] . " " . $_SESSION['enamn'];
<form action="2.php" method="post">
<input name="fnamn" type="text" id="fnamn" />
<br />
<input name="enamn" type="text" id="enamn" />
<br />
<input name="knapp" type="submit" id="knapp" value="Klicka" />
</form>

2.php:

session_start();
$_SESSION['fnamn'] = $_POST['fnamn'];
$_SESSION['enamn'] = $_POST['enamn'];
echo $_SESSION['fnamn'] . " " . $_SESSION['enamn'];
<a href="index.php">1</a>
<a href="3.php">3</a>

3.php:

session_start();
echo $_SESSION['fnamn'] . " " . $_SESSION['enamn'];

<a href="index.php">1</a>
<a href="2.php">2</a>
<a href="4.php">4</a>

4.php:

session_start();
echo $_SESSION['fnamn'] . " " . $_SESSION['enamn'];
<a href="index.php">1</a>
<a href="2.php">2</a>
<a href="3.php">3</a>
<a href="5.php">5</a>

5.php:

session_start();
echo $_SESSION['fnamn'] . " " . $_SESSION['enamn'];
<a href="index.php">1</a>
<a href="2.php">2</a>
<a href="3.php">3</a
<a href="4.php">4</a

Using the links I can go from one php file to another and the session variables are printed out as expected with one exception. I can go from 1 to 5 and backwards from 5 to 3, but when I click the link that take me backwards to 2.php the page, 2.php, does not print the session variables.

Any clue why?

It’s the same thing. There’s nothing in the $_POST-variables so you’re overwriting the $_SESSION variables with nothing.

3.php


session_start();
// $_SESSION['fnamn'] = $_POST['fnamn'];
// $_SESSION['enamn'] = $_POST['enamn'];
echo $_SESSION['fnamn'] . " " . $_SESSION['enamn'];

As you are not posting to 3.php, these variables are not set, or set to nothing. Comment them out and try again.

If you had error reporting turned on you should see a big warning, else go look in your logfiles.

Turn error reporting on when developing.