转到网站上的新页面后,PHP会话将被删除

奥利弗·尼布鲁(Oliver Nybroe)

当我转到网站上的新页面时,我的会话将被删除,或者这是我认为的错误,但是我不确定。
我在具有PHP和MYSQLI的登录系统中使用它。
我将在此处发布代码,因此如果有人愿意,他们可以查看它,甚至可以查看错误的出处。

这是位于根文件夹(/)中的index.php

<?php session_start(); ?>
<?php

include_once "Includes/Database/check_login.php";

if (login_check() == TRUE) : ?>
this is an protected page!
<?php   else : ?>
<!DOCTYPE html>
<html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
</head>
<body>
<script>location.href='loginpage.php';</script>
</body>
<?php endif; ?>

这是位于根文件夹(/)中的loginpage.php。

<?php  session_start();  // session starts with the help of this function 
include_once "Includes/Database/check_login.php";
?>

<!DOCTYPE html>
<html>
<head>
    <title>Hardcorefight.dk</title>
    <link rel="stylesheet" href="Includes/Layout/Index/loginlayout.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
</head>
<body>
<div class="fixedwebsitesize" id="fixedwebsitesize">
    <div class="outerlogin" id="outerlogin">
        <div class="login" id="login">
            <form action="Includes/Database/login.inc.php" method="post"   name="login_form">  <!-- This is the login form, that sends to login.inc.php.-->                    
                <div class="username" id="username">
                    <input type="text"
                    name="user" 
                    placeholder="user" 
                    class="user_login"
                    />
                </div>
                <div class="password" id="password">
                    <input type="password" 
                    name="pass" 
                    class="pass_login"
                    placeholder="Password"
                    />
                </div>
                <div class="loginbutton" id="loginbutton" >
                    <input type="submit" 
                    value="Login" 
                    class="login_input"
                    /> 
                </div>
      </form>
        </div> 
    </div>
    <div class="logoutbox"> <!-- This is an button that changes to register or log out depending if the user is logged in or not -->
        <input type="button"
        <?php if (login_check() == TRUE) : ?> 
        onclick="location.href='destroysession.php';" 
        value="Log Out"
        <?php else : ?>
        onclick="location.href='register.php';"
        Value="register"
        <?php endif; ?>"
        class="logout_button"
         />
    </div>
</div>
</body>
</html>

这是位于Database文件夹(/ Includes / Database /)中的login.inc.php。它检查输入的信息是否正确并进行会话。

<?php
session_start();  // session starts with the help of this function 
include_once "db_connect.php"; // include the connect file to the db.

$user_input = $_POST['user']; //Get's the post['user'] from loginpage.php
$pass_input = $_POST['pass'];  //Get's the post['pass'] from loginpage.php
if($result = $db_new->query("SELECT * FROM members WHERE username='$user_input'")){ // chooses the row from the DB that matches the username that the user wrote
    if($result->num_rows == 1){ //verify if there only is one user with that username
        $row = $result->fetch_assoc();
        if(password_verify($pass_input, $row["password"])){ //verify the password if it is the right password
            echo "password match";
            $_SESSION['username']=$row["username"]; //makes the session with the username
            $_SESSION['email']=$row["email"]; //makes the session with the email
            $_SESSION['id']=$row["id"]; //makes the session with the id
            $_SESSION['password']=$row["password"]; //makes the session with the password
            header("Location: /index.php"); // go to index
        }
        else { //if password is incorrect it will echo this.
            echo "password incorrect";
        }
    }
    else{ // if user doesn't exist it will echo this
        echo "user doesn't exist";
    }
} 
else {
    die($db_new->error);
}

这是位于Database文件夹(/ Includes / Database /)中的check_login.php。它读取会话并检查信息是否与DB相匹配,如果信息与功能匹配,则该函数为TRUE,否则为FALSE。

<?php
function login_check(){
session_start();  // session starts with the help of this function 
include_once "db_connect.php";
$id = $_SESSION['id']; 
$password = $_SESSION['password'];
$username = $_SESSION['username'];
$email = $_SESSION['email'];

if(isset($id, //checks if all the sesions exist.
         $password,
         $username,
         $email)){
if($result = $db_new->query("SELECT * FROM members WHERE username='$username'")){ //select the row that's equal the username from the session.
    if ($result->num_rows == 1) { //checks if there only is 1 row with the username
        $row = $result->fetch_assoc();
        $db_password = $row["password"]; 
        $db_id = $row["id"];
        $db_email = $row["email"];
        if ($password == $db_password) { // checks if the session password equal the DB password
            if ($id == $db_id) { // checks if the session ID equal the DB ID
                if ($email == $db_email) { // checks if the session email equal the DB email
                     //logged in
                     return TRUE;
                } else {
                    //not logged in (error in email verify)
                    return FALSE;
                }
            } else {
                //not logged in (error in id verify)
                return FALSE;
            }
        } else {
            //not logged in (error in password_verify)
            return FALSE;
        }
    } else {
        //not logged in (error in num_rows)
        return FALSE;
    }
} else {
    //not logged in (error in query)
    return FALSE;
}
    } else {
//not logged in (error in isset)
return FALSE;
}
}
奥利弗·尼布鲁(Oliver Nybroe)

抱歉,会话无法正常工作,在这里造成的所有麻烦。
我已经解决了这个问题,问题不在编程中,而是在我的PHP中。
服务器上的主驱动器空间不足,因此无法保存任何内容,因此无法保存会话。
感谢所有其他反馈,这将使我的代码更安全,对我有很大帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用PHP在我的网站上生成新页面?

来自分类Dev

会话关闭后,变量将被删除

来自分类Dev

删除后刷新页面

来自分类Dev

PushPad:网站刷新后,订阅将被删除

来自分类Dev

href =“#”转到新页面

来自分类Dev

刷新页面后,OneDrive会话丢失

来自分类Dev

PHP-刷新页面后会话丢失

来自分类Dev

PHP-刷新页面后会话丢失

来自分类Dev

页面闲置30秒后自动刷新页面并销毁php中的会话,而无需任何用户交互?

来自分类Dev

React删除按钮后刷新页面

来自分类Dev

Javascript 函数 - 转到新页面

来自分类Dev

检测cookies已被删除而无需刷新页面

来自分类Dev

刷新页面时会话被破坏

来自分类Dev

刷新页面后会话被破坏

来自分类Dev

会话登录不断刷新页面

来自分类Dev

尽管我删除了数据,但除非刷新页面,否则看不到它已被删除

来自分类Dev

页面不活动30秒后自动刷新页面并销毁php中的会话,而无需任何用户交互?

来自分类Dev

如何淡出页面然后直接转到新页面

来自分类Dev

如何从网站上删除PHP中的登录会话?

来自分类Dev

重定向到新页面后,会话变量为空

来自分类Dev

使用Ruby on Rails会话过期后如何重新加载或刷新页面

来自分类Dev

Bootstrap:是否可以创建一个可以导入到网站上每个新页面的模板?

来自分类Dev

登录后的角度刷新页面

来自分类Dev

登录后刷新页面

来自分类Dev

输入后更新页面?

来自分类Dev

提交表格后刷新页面

来自分类Dev

登录后导航到新页面

来自分类Dev

jQuery成功后刷新页面

来自分类Dev

退出后停止刷新页面