0

I have just started using session variables and I'm running into some problems.

I'm trying to echo out a variable from my form processing script back onto another page but currently I'm not getting any results.

Before starting let me say that I have already checked out other questions including this one:

Session variables not working php

I checked everything off that first answer list and still nothing.
The only error i am getting is:

Notice: Undefined index: id

Which I'm assuming is because when the page first starts it doesn't know what id is but even after submitting the error persists.

index.php

<?php
    session_start();
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
?>
<!DOCTYPE html>
<html>
<head>
    <title>PHP Sessions</title>
</head>
<body>
    <?php echo $_SESSION['id']; ?>

    <form action="submit.php" method="post">
        <input type="text" name="number" id="number">
        <button type="submit">Submit</button>
    </form>
</body>
</html>

submit.php

<?php
    session_start();

    try {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $id = $_POST['number'];
        };

        header('Location: http://home.com/');
        exit();
    } catch (Exception $e) {
        echo $e->getMessage();
    }
?>
Community
  • 1
  • 1
Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95
  • 3
    the notice is obvious, isn't it? you didn't assign anything to it. `if(isset($_SESSION['id'])) { echo "It's set"; } else{ echo "Sorry"; }` or `if(isset($_SESSION['id'])) { echo "It's set"; } else{ $_SESSION['id'] = "ID123"; }` – Funk Forty Niner May 23 '15 at 15:39
  • I'm a bit confused by your comment. $id is supposed to equal whatever the user inputs into the box and then when they hit submit it redirects back to the same page with the form and displays what they input. – Mr.Smithyyy May 23 '15 at 15:44
  • then you need to assign the session array to the post array – Funk Forty Niner May 23 '15 at 15:44
  • 1
    so is it supposed to be $_SESSION['id'] = $_POST['number'] in the submit.php file? – Mr.Smithyyy May 23 '15 at 15:45
  • 1
    you got it Pontiac ;-) – Funk Forty Niner May 23 '15 at 15:53
  • Thanks for the guidance! Everytime I learn something new I always miss something. – Mr.Smithyyy May 23 '15 at 15:56
  • BTW if you are learning/testing PHP sessions, your example does not demonstrate good usage of sessions. This looks more like an example for $_POST. – Ulver May 23 '15 at 15:57
  • @Ulver if you have any docs/articles you recommend feel free to post them. I would like to learn more but the traditional php docs put me to snooze. – Mr.Smithyyy May 23 '15 at 16:00
  • On reflection this example is ok actually. – Ulver May 23 '15 at 16:22
  • @Mr.Smithyyy I was going to post an answer earlier but didn't have time to put something together properly, as I had to head off somewhere. I've posted something for you below if you wish to accept it in order to close the question, *cheers* – Funk Forty Niner May 23 '15 at 20:50

2 Answers2

1

As mentioned in my comment, you didn't assign anything to it, which is why you're getting that notice. Therefore, use a conditional statement, using isset().

if(isset($_SESSION['id'])) { 
   echo "The session is set."; 
} else{ 
   echo "Sorry, it's not set."; 
} 

or

if(isset($_SESSION['id'])) { 
   echo "The session is set.";
} 
   else{ 
    $_SESSION['id'] = "ID123"; 
}
  • Pass your session array to a POST array:

From your initial page, you can use a ternary operator (see reference note) to echo if it's set or not.

<?php
    session_start();
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
?>
<!DOCTYPE html>
<html>
<head>
    <title>PHP Sessions</title>
</head>
<body>
    <?php echo isset($_SESSION["id"]) ? $_SESSION["id"] : '' ?>

    <form action="submit.php" method="post">
        <input type="text" name="number" id="number">
        <button type="submit">Submit</button>
    </form>
</body>
</html>
  • Then in your "submit.php" page, passing the session array to the POST array:

Sidenote: I commented these lines out, and when putting those back in, comment echo $id;; otherwise it will thrown a notice.

// header('Location: http://home.com/');
// exit();

submit.php and using a conditional !empty() for the POST array.

<?php
    session_start();

    try {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {


    if(!empty($_POST['number'])){

       $_SESSION['id'] = $_POST['number'];

    // $id = $_POST['number'];

       $id = $_SESSION['id'];

        echo $id;

    }

        };

// header('Location: http://home.com/');
// exit();
    } catch (Exception $e) {
        echo $e->getMessage();
    }
?>

The '' at the end of isset($_SESSION["id"]) ? $_SESSION["id"] : '' can be used to put a default text/number if you wish.

I.e.:

isset($_SESSION["id"]) ? $_SESSION["id"] : 'default value'

Reference(s):

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

The $_SESSION variables, like all variables, have to be created before. On your form you try to display a variable but it doesn't exists yet.

If you try to initialize the session just before, it works.

//If the variable exists, display it.
if ( isset($_SESSION['id'] )
    echo $_SESSION['id'];
Anthony
  • 2,014
  • 2
  • 19
  • 29