無法讓我的會話正常運行。過去幾個小時我一直在查看我的代碼,我看不到它發生了什麼問題。我遇到的問題是,我每次輸入用戶名和密碼時,它都會將我重定向到登錄頁面,以便在顯示securedpage.php時再次鍵入信息。簡單登錄會話php
這是我的代碼:
loginproc.php頁 - 本頁面if語句步驟,並通過直接轉到別的
<?php
// Inialize session
session_start();
// Include database connection settings
include('../../model/database.php');
// Retrieve username and password from database according to user's input
$login = mysql_query("SELECT * FROM user WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string($_POST['password']) . "')");
// Check username and password match
if (mysql_num_rows($login) == 1) {
// Set username session variable
$_SESSION['username'] = $_POST['username'];
// Jump to secured page
header('Location: securedpage.php');
}
else {
// Jump to login page
header('Location: index.php');
}
?>
securedpage.php頁
<?php
// Inialize session
session_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['username'])) {
header('Location: index.php');
}
?>
<html>
<head>
<title>Secured Page</title>
</head>
<body>
<p>This is secured page with session: <b><?php echo $_SESSION['username']; ?></b>
<br>You can put your restricted information here.</p>
<p><a href="logout.php">Logout</a></p>
</body>
</html>
database.php中頁面
<?php
$dsn = 'mysql:host=localhost;dbname=sports_db';
$username = '';
$password = '';
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
try {
$db = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
$error_message = $e->getMessage();
include 'errors/db_error_connect.php';
exit;
}
function display_db_error($error_message) {
global $app_path;
include 'errors/db_error.php';
exit;
}
?>
確認是否有前科,'DB'或取不是給定的用戶名和密碼 –
你可以爲'$ login'做一個var_dump嗎? – Cjueden
那麼'mysql_num_rows($ login)'返回什麼值?打印它以進行調試 –