Session variable not displaying

前端 未结 3 1415
小鲜肉
小鲜肉 2021-01-28 10:41

So I create a session on one page and create a new variable called uName and assign a value. I go to another page and try to echo the value but I get an error:\"Undefined variab

相关标签:
3条回答
  • 2021-01-28 10:53

    You need to call session_start() again at beginning of the new file, like so:

    <?php 
    session_start();
    
    echo "My Name is " . $_SESSION['uName'];
    
    0 讨论(0)
  • 2021-01-28 11:01

    page1.php

    <?php
    session_start();
    $_SESSION['uName'] = "Mike";
    ?>
    

    page2.php

    <?php
    session_start();
    echo "My Name is " . $_SESSION['uName'];
    //eventually
    unset($_SESSION['uName']);
    ?>
    
    0 讨论(0)
  • 2021-01-28 11:08

    You need to add session_start(); to all files that use sessions.

    0 讨论(0)
提交回复
热议问题