How to use session inside webpages?

后端 未结 3 1294
清歌不尽
清歌不尽 2020-12-11 13:50

I am trouble with session; I know it theoretically but I encountered with it how to use session , transferring username to

相关标签:
3条回答
  • 2020-12-11 14:01

    After the line

    $result=mysql_query($sql);
    

    add

    if ($data = mysql_fetch_array($result)) {
        $_SESSION['user'] = $data['usermail'];
    }
    

    Now session created.Call this session in jcte/index.php page as:

    <?php
    session_start();
    echo "welcome $_SESSION['user']";
    ?>
    

    Unset the session in logout.php page as:

    <?php
    session_start();
    unset($_SESSION['user']);
    ?>
    
    0 讨论(0)
  • 2020-12-11 14:06
    <?php
        session_start();
        $_SESSION['user']="Varma"; //intializing the session['user'];
        echo $_SESSION['user'];   // displaying the data  
        unset($_SESSION['user']); // destroying the session data.
    ?>
    

    but you have to initialize session_start(); in all web pages where you have need to access that session variables.

    0 讨论(0)
  • 2020-12-11 14:13

    Always start session page with session_start().If you want to use session first assign session a value like this :

    session_start();
    $_SESSION['username'] = 'Mahmood';
    

    And when you want to access get it like this :

    echo $_SESSION['username'];
    OR
    $username = $_SESSION['username'];
    

    And unset this session like this :

    unset($_SESSION['username']);
    

    Some Details are here.

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