假设用户输入他/她的用户名和密码并单击提交按钮,该按钮使用表单上的 $_POST 方法。要成功登录,显然用户名和密码必须与我的 mysql 数据库中的内容匹配,这是通过一系列“if”语句完成的。如果凭据错误,表单和“if”语句必须位于 html 标记内以显示正确的错误消息。在用户名和密码成功满足位于 html 标记内的所有“if”语句后,我显然想设置一个 cookie。但是,我无法在 html 标记中设置 cookie。
/*setcook() function can ONLY be placed HERE before the <html> tags, but that does not work with my php code*/
<html>
<head><title></title><body>
<?php
if (isset($_POST['username']) OR isset($_POST['password']))
{
/*bunch of "if" statements go here to confirm the credentials are correct and match what's in the database. if the username and password are correct, all of the "if" statements here are passed, and then i WANT to set a cookie HERE so the user is logged in but i can't*/
}
else echo <<<_END
<form action="login.php" method="post">
Username: <input type="text" name="username"/>
Password: <input type="password" name="password" />
<input type="submit" value="Submit" />"; //this is the form that the user fills out and submits
</form>
_END;
?>
</body>
</html>
但是,setcookie() 函数仅在 html 标记之前有效。在位于 html 标记内的 PHP 代码中验证所有用户名和密码凭据后,如何设置 cookie?