有人可以检查我的会话吗?

webghufk  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(313)

请有人检查我的会话是否正常。我不确定,因为我还是初学者。
login.php是用户登录用户名和密码的主页面:

<body>

        <form action="" method="post">
            <div class="imgcontainer">
            <img src="KBR2xN6.jpg" alt="Avatar" class="avatar">
            </div>

            <div class="container">
            <label><b>Username</b></label>
            <input type="text" placeholder="Enter Username" name="name" required>
            <br />

            <label><b>Password</b></label>
            <input type="password" placeholder="Enter Password" name="pass" required>

            <button type="submit">Login</button>
            <button type="reset" class="cancelbtn">Reset</button>
            </div>
        </form>

    </body>

至于connections.php是连接到本地服务器的:

$host = "localhost";
$username = "root";
$password = "";
$database = "netbook 1 malaysia";

try {
    $connect = new PDO("mysql:host=$host; dbname=$database", $username, $password);
    $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

catch(PDOException $ex) {
    echo 'Connection Failed : '.$ex->getMessage();
}

至于session.php,我不确定:

session_start();
include('connections.php');

$username = $_POST['name'];
$password = $_POST['pass'];

$sql = "SELECT * FROM pengguna WHERE username = '$username' AND password = '$password'";
$result = $connect->query($sql);

if($result->rowcount()>0){
    foreach($result AS $data){
        $_SESSION['name'] = $data['name'];
        $_SESSION['pass'] = $data['pass'];

    echo "<script>alert('Login Success');
            window.location.href='view.php';
            </script>";
    }
}

else {
    echo "<script>alert('Login Failed');
            window.location.href='login.php';
            </script>"; 
}

请帮我查一下。

qoefvg9y

qoefvg9y1#

只需将此代码添加到view.php文件中。

session_start();
print_r($_SESSION);

如果它打印您在会话中保存的值,那么它将工作。

相关问题