我只是剛剛開始學習PHP,並且目前爲止確實已經完成,但是我對會話感到困惑。php會話用戶名/密碼
如果有人可以看看我的代碼,並讓我知道我哪裏出錯了,我該如何解決這個問題太棒了!
按照說明將用戶名/密碼硬編碼到管理員123。
感謝
HTML
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>if statements</title>
</head>
PHP
<body>
Login
<form action="username.php" method="post">
<p>
<label for="username">Enter your username</label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="password">Enter your password</label>
<input type="text" name="password" id="password" />
</p>
<p>
<label for="GO"></label>
<input type="submit" name="GO" id="GO" value="Submit" />
</p>
</form>
</body>
</html>
<html>
<body>
<?php
session_start();
If($username==」admin」 && $password==」123」)
$username = isset($_POST['username']) ;
$password = isset($_POST['password']) ;
if ($name != 'admin' || $pass != '123')
{
elseif ($username === '')
die("ERROR: Please enter the username!");
elseif ($password === '')
die("ERROR: Please enter the password!");
elseif ($name == "test" && $pass == "test") {
// authentication successful, save username in session:
$_SESSION['user'] = 'admin';
// redirect to index.php to welcome the logged in user:
header('Location: index.html');
exit;
}
else {
die("ERROR: Incorrect username or password!");
}
}
?>
</body>
</html>
編輯
<html>
<body>
<?php
session_start();
If($username==」admin」 && $password=="123」)
$username = isset($_POST['username']) ;
$password = isset($_POST['password']) ;
if ($username == "admin" && $password == "123") {
// authentication successful, save username in session:
$_SESSION['username'] = 'admin';
$_SESSION['password'] = '123';
// redirect to welcome.html to welcome the logged in user:
header('Location: welcome.html');
exit;
}
else {
die("ERROR: Incorrect username or password!");
}
// no submitted data, display form:
?>
</body>
</html>
再次編輯:
<html>
<body>
<?php
session_start();
if(isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if($username === 'admin' && $password === '123')
$_SESSION['username'] = 'admin';
$_SESSION['password'] = '123';
session_write_close();{
// redirect to welcome.html to welcome the logged in user:
header('Location: welcome.html');
exit;
}
}
else {
die("ERROR: Incorrect username or password!");
}
// no submitted data, display form:
?>
</body>
</html>
這樣的工作了,但即使是錯誤的用戶名/密碼,它會welcome.html
我怎麼會得到2拇指下來?我已經說過我剛剛開始學習php – Mikey
我編輯了代碼,也許它更好一點,我可以得到一些幫助? – Mikey
你的username.php文件的代碼在哪裏?你使用mysql數據庫來確認密碼嗎? – SreYash