和 Alma Do Mundo 一样,我建议阅读有关会话的信息。以下是你将如何完成你想要的:
登录.php:
<!DOCTYPE html>
<html>
<head><title>Login</title></head>
<body>
<form id='loginform' action='checklogin.php' method='post'>
<div>Username: <input type='text' id='username' name='username' /></div>
<div>Password: <input type='password' id='password' name='password' /></div>
<div><input type='submit' id='submit' value='Login'></div>
</form>
</body>
</html>
让登录页面将数据发送到中间页面,例如 checklogin.php。这是代码:
<?php
session_start();
// check if the username and password are correct
// in real world scenario, this would be more complex as data
// is cleaned and then checked against the database where username
// and encrypted password (and salt) are stored in a *secured* manner
if ($_POST['username'] === 'hello' && $_POST['password'] === 'world')
{
$_SESSION['username'] = $_POST['username'];
header('location:welcome.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login issue</title>
</head>
<body>
<div class='badassredcolor'>Invalid username or password. Please <a href='login.php'>login again</a>.</div>
</body>
</html>
checklogin.php 将查看用户名和密码是否正确。如果他们是正确的,请记住他们的用户名并将此人发送到 welcome.php。否则请他们重新登录。当然,这个过程应该比这更优雅,但这只是一个例子。
然后,您的欢迎页面将向他们显示欢迎消息,如下所示:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome to the awesome website</title>
</head>
<body>
<h1>Welcome <?php echo $_SESSION['username']; ?></h1>
</body>
</html>
希望这可以帮助。
好吧,假设我们想使用单个页面来显示表单并检查登录名/密码,使用下面的示例。这是一个可以修改的快速草稿。在现实世界中,你会做很多不同的事情,但这个例子很适合学习:
<?php
session_start();
// let's hold a variable to check if the user POST'ed the form
// or if we got to this page without POST'ing to itself
$isPosted = false;
// let's first check if user POST'ed anything
if (isset($_POST['username']) && isset($_POST['password']))
{
// okay, we got something. Let's flag the variable
$isPosted = true;
// Let's check if the username and password are good. If good, let's set up
// a session variable appropriately
if ($_POST['username'] === 'hello' && $_POST['password'] === 'world')
{
$_SESSION['username'] = $_POST['username'];
}
}
else
{
// seems like the form was not posted with any interesting data
// or it is a fresh page-load. Let's ensure that no session variables
// are alive
if(isset($_SESSION['username']))
{
session_unset();
}
}
?>
<!DOCTYPE html>
<html>
<head><title>Login</title></head>
<body>
<?php
// if the form was posted and session was set, show welcome message
if (
$isPosted === true &&
(isset($_SESSION['username']))
)
{
?>
<div class='greetings'>Welcome, <?php echo htmlentities($_SESSION['username']); ?></div>
<div>more cool things here</div>
<?php
}
// oh, either the page was freshly loaded or session was not set. Cool, show form first
else
{
?>
<form id='loginform' action='newlogin.php' method='post'>
<div>Username: <input type='text' id='username' name='username' /></div>
<div>Password: <input type='password' id='password' name='password' /></div>
<div><input type='submit' id='submit' value='Login'></div>
</form>
<?php
// if the form was posted and we know that session is not set,
// let's show an error
if ($isPosted)
{
?>
<div class='badassREDcolor'>Invalid username or password</div>
<?php
}
}
?>
</body>
</html>