Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
使用本地托管的sqlite3 db登录时创建PHP$_会话[';x';]_Php_Sqlite_Authentication_Localhost_Session Variables - Fatal编程技术网

使用本地托管的sqlite3 db登录时创建PHP$_会话[';x';]

使用本地托管的sqlite3 db登录时创建PHP$_会话[';x';],php,sqlite,authentication,localhost,session-variables,Php,Sqlite,Authentication,Localhost,Session Variables,我正在编写一个包含一个登录页面(作为索引页面)的应用程序,我希望在登录有效时为该页面分配一个$_SESSION['user']变量,以便使用auth脚本保护受限制的页面。在没有设置会话的情况下,我让一切都正常工作,但是只要我尝试从index.php启动会话,一切都停止工作。有人能给我解释一下发生了什么事吗?2天来,我一直在阅读和尝试不同的东西 db.php <?php $con = new SQLite3("db/test.db"); if(!

我正在编写一个包含一个登录页面(作为索引页面)的应用程序,我希望在登录有效时为该页面分配一个$_SESSION['user']变量,以便使用auth脚本保护受限制的页面。在没有设置会话的情况下,我让一切都正常工作,但是只要我尝试从index.php启动会话,一切都停止工作。有人能给我解释一下发生了什么事吗?2天来,我一直在阅读和尝试不同的东西

db.php

<?php
    $con = new SQLite3("db/test.db");
    
    if(!$con){
        echo "Failed to connect to the Hosts Database";
    }
?>
<?php
    if(!isset($_SESSION['user'])){
        header('Location: index.php');
    }
?>
<?php
    session_start();
?>

<!DOCTYPE html>
<head>
    <title>App</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <?php
        require("db/db.php");
        if(isset($_POST["login"])){
            $username = $_POST["username"];
            $passwd = $_POST["upass"];

            $sql = "SELECT * FROM access WHERE (user='".$username."' AND pass='".md5($passwd)."');";
            $r = $con->query($sql);
            if($r){
                $_SESSION['user'] = $username;
                echo "<script>window.location.replace('access.php');</script>";
            }
        } else {
    ?>
    <form name="loginForm" id="loginForm" method="POST" action="">
        <center>
            <input type="text" name="username" id="username" placeholder="Username" required/>
            <input type="password" name="upass" id="upass" placeholder="Password" required />
                <br />
            <input type="submit" name="login" value="Login" />
        </center>
    </form>
<?php
    }
?>
</body>
</html>

auth.php

<?php
    $con = new SQLite3("db/test.db");
    
    if(!$con){
        echo "Failed to connect to the Hosts Database";
    }
?>
<?php
    if(!isset($_SESSION['user'])){
        header('Location: index.php');
    }
?>
<?php
    session_start();
?>

<!DOCTYPE html>
<head>
    <title>App</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <?php
        require("db/db.php");
        if(isset($_POST["login"])){
            $username = $_POST["username"];
            $passwd = $_POST["upass"];

            $sql = "SELECT * FROM access WHERE (user='".$username."' AND pass='".md5($passwd)."');";
            $r = $con->query($sql);
            if($r){
                $_SESSION['user'] = $username;
                echo "<script>window.location.replace('access.php');</script>";
            }
        } else {
    ?>
    <form name="loginForm" id="loginForm" method="POST" action="">
        <center>
            <input type="text" name="username" id="username" placeholder="Username" required/>
            <input type="password" name="upass" id="upass" placeholder="Password" required />
                <br />
            <input type="submit" name="login" value="Login" />
        </center>
    </form>
<?php
    }
?>
</body>
</html>

index.php

<?php
    $con = new SQLite3("db/test.db");
    
    if(!$con){
        echo "Failed to connect to the Hosts Database";
    }
?>
<?php
    if(!isset($_SESSION['user'])){
        header('Location: index.php');
    }
?>
<?php
    session_start();
?>

<!DOCTYPE html>
<head>
    <title>App</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <?php
        require("db/db.php");
        if(isset($_POST["login"])){
            $username = $_POST["username"];
            $passwd = $_POST["upass"];

            $sql = "SELECT * FROM access WHERE (user='".$username."' AND pass='".md5($passwd)."');";
            $r = $con->query($sql);
            if($r){
                $_SESSION['user'] = $username;
                echo "<script>window.location.replace('access.php');</script>";
            }
        } else {
    ?>
    <form name="loginForm" id="loginForm" method="POST" action="">
        <center>
            <input type="text" name="username" id="username" placeholder="Username" required/>
            <input type="password" name="upass" id="upass" placeholder="Password" required />
                <br />
            <input type="submit" name="login" value="Login" />
        </center>
    </form>
<?php
    }
?>
</body>
</html>

应用程序

如果没有显式地启动会话,那么您为什么不省略
会话\u start()
?可能是在PHP配置中启用了会话自动启动。原因有二,比如我说检查auth脚本内部,如果不是会话中的用户,则重定向,这样我就可以在应用程序的其他地方引用$\u session['user']。检查配置,session.auto_start设置为0,已禁用。您是否确保在启动会话之前没有输出任何内容(甚至没有空白)?这是一个常见的陷阱,因为此时无法发送会话cookie。是的,这两个脚本之前都没有任何内容。重新安排一些代码,我现在无法登录,同时在脚本中包含sessionstart&分配变量,这是我希望auth.php所做的,但是$\u session['user']中没有设置值,因此我无法登录。我试过使用$\u SESSION['user']=$username以及$\u SESSION['user']=$\u POST['username'],但都不管用..我想出来了。解决方案不是在索引中调用session_start,而是将其移动到db.php文件中,但仍然在索引中的login sql语句之后设置变量。还可以在每个文件中使用一行代码进行身份验证检查。现在我可以登录,在不登录的情况下阻止查看页面&在每个文件中调用$\u会话['user']。