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

时间:2018-01-08 07:15:35

标签: php mysql session

请有人检查我的会话是否有效。 我不确定,因为我还是初学者。

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>"; 
}

请检查我。

1 个答案:

答案 0 :(得分:0)

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

session_start();
print_r($_SESSION);

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