0

it seems to be simple but anyhow i can't pass my sessionvariable from the first page to the second page. I have searched for solutions but can't find any. As far as i know i am starting the session on bothe pages before sending any headers.

code on page1

<?php session_start()?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>


<form action="page2.php" method="POST">
<input type="text" name="username" id="username">
<input type="submit" name="submit" id="submit">


<?php if(isset($_POST['submit'])) {

$username = $_POST['username'];

    $_SESSION['username'] = $username;

}

?>
</form>

</body>
</html>

Code on page 2

<?php session_start()?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<?php 

    echo "This is from my session: "." ". $_SESSION['username'];

?>


</body>
</html>

So on the second page (page2.php) i get this error:

Notice: Undefined variable: username in C:\xampp\htdocs\page2.php on line 12

This is from my session:

Armando
  • 1
  • 4
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Masivuye Cokile Nov 06 '18 at 10:09
  • 2
    The php code you have on page 1 must be on page 2 – Masivuye Cokile Nov 06 '18 at 10:10

3 Answers3

9

Your form posts to page2.php, so the session-setting bit isn't being reached on page1.php You need to add that code to page2.php instead:

if(isset($_POST['submit'])) {
    $username = $_POST['username'];
    $_SESSION['username'] = $username;
}
treyBake
  • 6,440
  • 6
  • 26
  • 57
lawra
  • 137
  • 8
  • 1
    just a note: better to use `!empty` in place of `isset()` - `isset()` just checks if the var/key exists - not if the value doesn't equal an empty value (e.g. '') – treyBake Nov 06 '18 at 10:13
  • 1
    Wowwww this was my first question on stackoverflow and i had reply's immediately. Thnx Lawra your answer worked. – Armando Nov 06 '18 at 10:16
  • @ThisGuyHasTwoThumbs: thnx for the tip will keep this in mind. – Armando Nov 06 '18 at 10:18
0

Looking at your code - the mistake is on page2.php, you submit the form to page2.php but you didn't set any value in page2.php.

It should be like this:

page1

<?php session_start()?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>    

<form action="page2.php" method="POST">
  <input type="text" name="username" id="username">
  <input type="submit" name="submit" id="submit">
</form>

</body>
</html>

page 2

    <?php session_start()?>
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>

    <?php 

    if(!empty($_POST['username'])) {
        $username = $_POST['username'];
        $_SESSION['username'] = $username;
    }
    echo "This is from my session: "." ". $_SESSION['username'];

    ?>


    </body>
    </html>
Gabriel
  • 970
  • 7
  • 20
-1

There upvoted answer works fine. The below should also work. I think the basic thing is to check if a session can be passed from one page to another. Please see the below code. Two things basically - 1. Form action should be set to same page & 2. Redirect after session is set

Note: No change is needed for page2.php

<?php session_start()?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form action="" method="POST">
        <input type="text" name="username" id="username">
        <input type="submit" name="submit" id="submit">
        <?php 
        if(isset($_POST['submit'])) {
            $username = $_POST['username'];
            $_SESSION['username'] = $username;
            header("Location: page2.php");
        }

        ?>
    </form>
</body>
</html>
lawra
  • 137
  • 8