Php 会话变量消失了吗?

Php 会话变量消失了吗?,php,security,session,session-variables,password-encryption,Php,Security,Session,Session Variables,Password Encryption,我试图了解更多关于密码散列和安全登录等方面的信息,出于这个原因,我尝试在这里复制这个示例。我没有100%地重复这个 我遇到的问题是,当我输入登录凭据时,表单会转到验证密码等的进程\u login.php脚本,并设置$\u会话变量。成功后,它应该重定向到protected.php,这是一个只有用户登录后才能访问的站点 对我来说,它不起作用仅仅是因为$\u会话变量消失了 我的进程\ u login.php脚本显示设置了$\会话,然后我使用标题(“Location:protected.php”)然后告

我试图了解更多关于密码散列和安全登录等方面的信息,出于这个原因,我尝试在这里复制这个示例。我没有100%地重复这个

我遇到的问题是,当我输入登录凭据时,表单会转到验证密码等的
进程\u login.php
脚本,并设置
$\u会话
变量。成功后,它应该重定向到
protected.php
,这是一个只有用户登录后才能访问的站点

对我来说,它不起作用仅仅是因为
$\u会话
变量消失了

我的
进程\ u login.php
脚本显示设置了
$\会话,然后我使用
标题(“Location:protected.php”)
然后告诉我
$\u会话
数组为空。这怎么可能?我错过了这里的船

以下是代码的相关部分:

process\u login.php

process_login.php

include_once 'connect.php';
include_once 'functions.php';
sec_session_start();

if (isset($_POST['eml'], $_POST['h'])) {
    $email = $_POST['eml'];
    $pwd_hash = $_POST['h'];
    if (login($email, $pwd_hash, $mysqli) == true) {
        // in my situation, this returns true
        // and the redirect to "protected.php" happens
        header('Location: protected.php');
    } else {
        header("Location: error?err=Wrong password");
    }
} else {
    exit('Invalid Request');
}
protected.php

<?php
include_once 'connect.php';
include_once 'functions.php';
sec_session_start();
// $return = login_check($mysqli);
print_r(get_defined_vars());
// this outputs an empty $_SESSION array
exit;
登录(
函数

function login($email, $password, $mysqli) {
    if ($stmt = $mysqli->prepare("SELECT id, email, pwd, salt FROM public WHERE email=? LIMIT 1")) {
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($user_id, $email, $db_password, $salt);
        $stmt->fetch();
        $password = crypt($password, $salt);
        if ($stmt->num_rows == 1) {
            if (checkbrute($user_id, $mysqli) == true) {
                // Account is locked 
                $status = "3";
                $mysqli->query("INSERT INTO login_activity(user, status, ip)
                                VALUES ('$email', '$status', '{$_SERVER['REMOTE_ADDR']}')");
                sleep(8);
                header("Location: ../error?err=The account you try to access is currently blocked.");
                return false;
            } else {
                // Check if the password in the database matches
                // the password the user submitted.
                if ($db_password == $password) {
                    // Password is correct!
                    // Get the user-agent string of the user.
                    $user_browser = $_SERVER['HTTP_USER_AGENT'];
                    // XSS protection as we might print this value
                    $user_id = preg_replace("/[^0-9]+/", "", $user_id);
                    $_SESSION['user_id'] = $user_id;
                    $_SESSION['login_string'] = hash('sha512', $password . $user_browser);
                    $status = "1";
                    $mysqli->query("INSERT INTO login_activity (user, status, ip)
                                    VALUES ('{$_SESSION['user_id']}', '$status', '{$_SERVER['REMOTE_ADDR']}')");
                    return true;
                } else {
                    // Password is not correct
                    // We record this attempt in the database
                    $status = "2";
                    $mysqli->query("INSERT INTO login_activity(user, status, ip)
                                    VALUES ('$email', '$status', '{$_SERVER['REMOTE_ADDR']}')");
                    sleep(3);
                    header("Location: ../error?err=Password is not correct.");
                    return false;
                }
            }
        } else {
            // No user exists.
            sleep(2);
            header("Location: ../error?err=No user exists.");
            return false;
        }
        header("Location: ../error?err=You can't see this.");
        return false;
    } else {
        header("Location: ../error?err=DB fail: ".$mysqli->error);
        return false;
    }
}
protected.php

process_login.php

include_once 'connect.php';
include_once 'functions.php';
sec_session_start();

if (isset($_POST['eml'], $_POST['h'])) {
    $email = $_POST['eml'];
    $pwd_hash = $_POST['h'];
    if (login($email, $pwd_hash, $mysqli) == true) {
        // in my situation, this returns true
        // and the redirect to "protected.php" happens
        header('Location: protected.php');
    } else {
        header("Location: error?err=Wrong password");
    }
} else {
    exit('Invalid Request');
}
protected.php

<?php
include_once 'connect.php';
include_once 'functions.php';
sec_session_start();
// $return = login_check($mysqli);
print_r(get_defined_vars());
// this outputs an empty $_SESSION array
exit;
我试图看看如果我只是使用
sec\u session\u start()
启动会话会发生什么,结果如下:

include_once 'connect.php';
include_once 'functions.php';
sec_session_start();
$_SESSION["test"] = "works!";
header('Location: protected.php');
print_r(get_defined_vars())的输出protected.php
中的code>是:

[_SESSION] => Array ( )

这可能对许多会话问题有帮助。我已经使用PHP很多年了,我很喜欢它, 但这很奇怪

在本守则中—

page1.php

<?phpsession_start();?>

<?php
$_SESSION['roman']="kitty";
echo ('<a href="page2.php"> Go To Page 2</a>');
?>

page2.php

<?php session_start();?>

<?php
echo $_SESSION['roman'];
?>

请注意,第1页的“phpsession_start()”中没有空格, 但我确实在第2页的“php会话_start()”中使用了一个空格

在第2页中没有使用空格,会话变量就消失了。有一个空间,它工作得很好。 在第1页中,这不重要,不管有无空格,都可以。我有其他脚本在哪里 恰恰相反。只有在没有空间的情况下才能工作!因此,有一些东西需要尝试和检查

现在,许多人可能会粘贴此代码并使其正常工作,也可能不会像我所描述的那样正常工作,但这就是原因
这很奇怪

什么是
sec\u session\u start()
?@Stony我现在在我的问题中添加了这个函数。你为什么要睡觉?为什么盐在db里?使用密码\u散列和密码_verify@RonniSkansing
sleep()
作为防止暴力攻击的额外措施。Salt在DB中,基于我正在使用的网站的示例。不确定是否有更好的方法来做到这一点。不知道你还提到了什么。这与我的问题有关吗?我认为问题在于其中一个设置不允许写入会话。我会在php上仔细检查这个函数,看看它如何应用于您的环境。