会话以及如何将变量分配给会话数组?

时间:2017-08-03 20:20:13

标签: php session

基本上我知道会话是如何工作的,但我正在努力设置init : (Model, Cmd Msg) init = ( Model "" [] ) ! [fetchCmd, Ports.messageToJs "Hello JS"] 数组中的变量。我尝试将布尔值true和$ username分配给$_SESSION,但在这两种情况下,我都检查$_SESSION ["login"]是否已设置,它是否为空并重定向到登录页面。我检查了几个关于会话的现有问题,但仍然找不到我的问题的解决方案。以下是login.php中会话变量分配给$_SESSION["login"]

的部分
$username

欢迎来到这里.php:

<?php
include("db.php");
session_start();
$error = '';
if(!isset($_SESSION["login"])){
    if (isset($_POST["submit"])) {
        if (empty($_POST["username"]) or empty($_POST["password"])) {
            $error = "Please enter your login details";
        } else {

            $username = mysqli_real_escape_string($conn, $_POST["username"]);
            $password = md5($_POST["password"]);



            $query = mysqli_prepare($conn, "SELECT password FROM workers WHERE user_name=?");
            mysqli_stmt_bind_param($query,"s",$username);
            mysqli_stmt_execute($query);
            mysqli_stmt_bind_result($query,$pass);
            if(mysqli_stmt_fetch($query)){
                if ($password == $pass){
                    $_SESSION["login"] = $_POST["username"];
                    echo "<script type='text/javascript'> document.location = 'welcome.php'; </script>";


                }else {
                    $error = "You typed the wrong password";
                    unset($username, $password);
                }
            }else{
                $error = "User Login doesn't exists";
                unset($username,$password);
            }

        }
    }
}else{

    echo "<script type='text/javascript'> document.location = 'welcome.php'; </script>";
    exit();
}
?>

我如何解决问题,为什么会这样?

1 个答案:

答案 0 :(得分:0)

只要您想要声明它们,就必须启动会话,而不仅仅是在您要使用它时。您需要在声明会话变量的页面上启动会话