如何使用mysqli和php使用数据库中的字段初始化Session

时间:2017-06-09 13:08:29

标签: javascript php mysql mysqli session-variables

我想在我的程序中创建会话但代码不起作用。请任何想法。用户已成功登录,但随后会立即使用check.php注销,因为会话未初始化。我想存储applicant_id(数据库中的自动增量值)并在系统中使用它。

check.php(检查用户的页面已登录)

<?php
include('rpazdb.php');
session_start();
$user_check = $_SESSION['applicant_id'];

$sql = mysqli_query($conn,"SELECT applicant_id FROM applicants WHERE applicant_id ='$user_check' ");

$row=mysqli_fetch_array($sql, MYSQLI_ASSOC);

$_SESSION['applicant_id'] = $row['applicant_id'];

if(!isset($user_check))
{
    header("Location: index.html");
}
?>

其他代码

<?php
session_start();
include 'rpazdb.php';
$email = $_POST['email'];
$pass = $_POST['password'];
$email = stripslashes($email);
$pass = stripslashes($pass);
$email = mysqli_real_escape_string($conn, $email);
$pass = mysqli_real_escape_string($conn, $pass);

$queryy = mysqli_query($conn, "SELECT * FROM applicants WHERE email='".$email."'");
if(mysqli_num_rows($queryy) < 1){
    echo "email address does not exist! Please use a valid email address";}
else {
    $queryy = mysqli_query($conn, "SELECT * FROM applicants WHERE email='".$email."'");
    //mysqli_query($conn, $queryy);
    $rw = mysqli_fetch_array($queryy);
    $hash_pass = $rw['password'];
    $hash = password_verify($pass, $hash_pass);

    if ($hash == 0) {
        header("location: test.html");

    }else {

        $sql="SELECT * FROM applicants WHERE email='".$email."' and password='".$hash_pass."'";
        $result=mysqli_query($conn,$sql);
        $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
        $rww = mysqli_fetch_array($sql);

        if(mysqli_num_rows($result) == 1)
        {
            $_SESSION['name'] = $rww['name']; // Initializing Session
            //$_SESSION['applicant_id'] = $applicant_id;
            $_SESSION['applicant_id'] = $rww['applicant_id'];

            header("location: good.php"); // Redirecting To Other Page
        }else {
            $error = "Incorrect username or password.";
        }
    }
}
?>

0 个答案:

没有答案