在会话中存储用户信息

时间:2019-05-12 02:49:16

标签: php

session_start();
require_once 'config.php';

if(isset($_POST['username']) && isset($_POST['password'])){

    $uname = $_POST['username'];
    $upass = $_POST['password'];

    //select users information from database
    $buildsql = "SELECT * FROM umembersd WHERE uusername = '$uname' AND       upassword = '$upass'";
    $myexec = mysqli_query($conn, $buildsql) or die (mysqli_error($conn));

    if($myexec->num_rows > 0){
        while($data = $myexec->fetch_assoc()){

            $_SESSION['no'] = 'id';
            $_SESSION['firstname'] = 'ufname';
            $_SESSION['lastname']= 'ulname';
            $_SESSION['phonenumber'] = 'uphone';
            $_SESSION['emailaddress']= 'uemail';
            $_SESSION['datejoined'] = 'uregdate';
            $_SESSION['datereg'] = 'uusername';

            header('location: index.php?successful');
        }
    }else{
        header('location: login.php?nerror');
    }
    }

1 个答案:

答案 0 :(得分:0)

在会话中存储用户详细信息。

 session_start();
    $uname = $_POST['username'];
    $upass = $_POST['password'];

    //select users information from database
    $buildsql = "SELECT * FROM umembersd WHERE uusername = '$uname' AND       upassword = '$upass'";
    $myexec = mysqli_query($conn, $buildsql) or die (mysqli_error($conn));

    if($myexec->num_rows > 0){
        while($data = $myexec->fetch_assoc()){
            //Store the user data in session
            $_SESSION['no'] = $data['id'];
            $_SESSION['firstname'] = $data['ufname'];
            $_SESSION['lastname']= $data['ulname'];
            $_SESSION['phonenumber'] = $data['uphone'];
            $_SESSION['emailaddress']= $data['uemail'];
            $_SESSION['datejoined'] = $data['uregdate'];
            $_SESSION['datereg'] = $data['uusername'];
            header('location: index.php?successful');
        }
    }else{
        header('location: login.php?nerror');
    }

成功登录后,您可以在index.php文件中获取会话值。

//After successful login you can get the session value in index.php file.
session_start();
if($_SESSION['no']){
    $no = $_SESSION['no'];
    $ufname = $_SESSION['firstname'];
    $lastname = $_SESSION['lastname'];
    $phonenumber = $_SESSION['phonenumber'];
}