Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP会话在登录后似乎无法工作!Wamp-创建/登录4个会话?_Php_Mysql_Windows_Session_Wamp - Fatal编程技术网

PHP会话在登录后似乎无法工作!Wamp-创建/登录4个会话?

PHP会话在登录后似乎无法工作!Wamp-创建/登录4个会话?,php,mysql,windows,session,wamp,Php,Mysql,Windows,Session,Wamp,这是我第一个被问到的问题,所以如果我违反了一些发帖礼仪,请原谅——我会尽我所能清楚地解释我的问题,我已经搜索了以前的问题,但据我所知,没有一个与我的问题相匹配 背景:使用Apache 2.4.4和PHP 5.4.12在WAMP服务器2.4上运行-如果您需要任何详细信息,请告诉我 我一直在做一个新的webapp项目,似乎在尝试让PHP会话正常工作时遇到了问题。我的登录过程如下 一旦用户提交了详细信息,并与Mysql数据库中存储的详细信息进行交叉检查,就会创建一个会话,并将其重定向到临时受保护的页面

这是我第一个被问到的问题,所以如果我违反了一些发帖礼仪,请原谅——我会尽我所能清楚地解释我的问题,我已经搜索了以前的问题,但据我所知,没有一个与我的问题相匹配

背景:使用Apache 2.4.4和PHP 5.4.12在WAMP服务器2.4上运行-如果您需要任何详细信息,请告诉我

我一直在做一个新的webapp项目,似乎在尝试让PHP会话正常工作时遇到了问题。我的登录过程如下

一旦用户提交了详细信息,并与Mysql数据库中存储的详细信息进行交叉检查,就会创建一个会话,并将其重定向到临时受保护的页面

临时页面检查用户是否有有效会话,如果有,则显示欢迎消息

如果用户没有有效的会话,则会收到错误

问题:无论何时我成功登录,我都会被重定向并收到错误消息,您无权访问此页面

以下是登录过程\u login.php的代码:

<?php
include_once 'db_connect.php';
include_once 'functions.php';

sec_session_start(); // Our custom secure way of starting a PHP session.

if (isset($_POST['email'], $_POST['p'])) {
$email = $_POST['email'];
$password = $_POST['p']; // The hashed password.


//Form data error handling.
if ($email == "" || $password == ""){
    echo "login failed";
    exit();

} else {
 //DB stuff.
 $stmt = $mysqli->prepare("SELECT id, username, password, salt 
 FROM members
   WHERE email = ?
    LIMIT 1");  
$stmt->bind_param('s', $email);  // Bind "$email" to parameter.
$stmt->execute();    // Execute the prepared query.
$stmt->store_result();

// get variables from result.
$stmt->bind_result($user_id, $username, $db_password, $salt);
$stmt->fetch();
// hash the password with the unique salt.
$password = hash('sha512', $password . $salt);
    if ($stmt->num_rows == 1) {
        // If the user exists we check if the account is locked
        // from too many login attempts 

        if (checkbrute($user_id, $mysqli) == true) {
            // Account is locked 
            // Send an email to user saying their account is locked
            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;
                // XSS protection as we might print this value
                $username = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $username);

                $_SESSION['username'] = $username;
                $_SESSION['login_string'] = hash('sha512', $password . $user_browser);      

                header('Location: ../protected_page.php');
            } else {
                // Login failed 
                // Password is not correct
                // We record this attempt in the database
                $now = time();
                $mysqli->query("INSERT INTO login_attempts(user_id, time)
                                VALUES ('$user_id', '$now')");

                header('Location: ../index.php?error=1');
            }
        } 
}
这是我的临时测试代码protected_page.php; -请注意,我是新手,似乎在发布html时遇到了问题

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Secure Login: Protected Page</title>
        <link rel="stylesheet" href="styles/main.css" />
    </head>
    <body>
        <?php if (login_check($mysqli) == true) {?>
            <p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>
            <p>
                This is an example protected page.  To access this page, users
                must be logged in.  At some stage, we'll also check the role of
                the user, so pages will be able to determine the type of user
                authorised to access the page.
            </p>
            <p>Return to <a href="index.php">login page</a></p>
        <?php } else {?>
            <p>
                <span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
            </p>
        <?php }?>
    </body>
</html>    
至于其他可能产生影响的细节,登录表单通过侧栏加载并发送

非常感谢您的帮助!我对这玩意儿还不太熟悉,我已经花了5个多小时摆弄它,似乎弄不明白。登录工作,会话代码,据我所知是有意义的,应该是工作的-啊哈,暂停我

添加说明:我已经检查了我的C:/wamp/etc/文件并清除了会话,只需登录就可以创建4个会话文件?我想这一定与此有关


好吧,我最终解决了我自己的问题,但我将把答案留在这里,以防有人对WAMP有类似的问题。我想是因为我用的是WAMP

在自定义会话函数sec_session_start中,我启用了$secure选项。虽然我相信只有当您在使用HTTPS的生产服务器上使用它时,它才起作用。不是在我本地的机器上。我的推理可能是错误的,但我改变了

$secure = true;

to

$secure = false;

成功了!巨大的成功

你能输出由session_get_cookie_params生成的数组吗?@dehtron5000 Hi,我想我只是通过添加:如果是,这就是输出;lifetime=>0path=>/domain=>secure=>1httponly=>1puu.sh/71Mqo.png感谢您的帮助btw.print\r会话\u get\u cookie\u参数;也行;。
$secure = true;

to

$secure = false;