简体   繁体   English

用户通过$ _POST方法使用登录凭据(密码和用户名)提交表单后,如何设置Cookie?

[英]How to set cookie after user submits form via $_POST method with login credentials (password and username)?

Suppose a user inputs his/her username and password and clicks on the submit button, which utilizes $_POST method on the form. 假设用户输入他/她的用户名和密码,然后单击“提交”按钮,该按钮利用表单上的$ _POST方法。 To log in successfully, obviously the username and password have to match what's in my mysql database, which is done through a series of "if" statements. 要成功登录,显然用户名和密码必须与我的mysql数据库中的用户名和密码匹配,这可以通过一系列“ if”语句来完成。 The form and "if" statements have to lie within the html tags to display the correct error messages if the credentials are wrong. 如果凭据错误,则form和“ if”语句必须位于html标记内才能显示正确的错误消息。 After the username and password successfully satisfy all of the "if" statements, which are located within the html tags, I obviously want to set a cookie. 用户名和密码成功满足html标记中的所有“ if”语句后,我显然想设置一个cookie。 However, I can't set a cookie within the html tags. 但是,我无法在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>

HOWEVER, the setcookie() function only works BEFORE the html tag. 但是,setcookie()函数仅在html标记之前起作用。 How can I set a cookie AFTER all the username and password credentials are verified in my PHP code that lies inside the html tags? 在所有HTML标记内的PHP代码中验证了所有用户名和密码凭据之后,如何设置Cookie?

You shouldn't be putting logic like this mixed in with your HTML. 您不应在HTML中混入这样的逻辑。 Put all of your PHP to validate credentials before any output is sent. 在发送任何输出之前,将所有PHP都用于验证凭据。 Then, you can set any cookies you want. 然后,您可以设置任何所需的Cookie。

The reason you can't set the cookie later is that cookies are set as part of headers, which are done being sent by the time output begins. 以后不能设置cookie的原因是cookie被设置为标头的一部分,这些标头在输出开始时就已完成发送。 You could work around this by enabling output buffering... but don't do it. 您可以通过启用输出缓冲来解决此问题,但是不要这样做。 It's bad practice, isn't always enabled on other servers, and has the potential to slow things down a hair. 这是一种不好的做法,并非总是在其他服务器上启用,并且有可能减慢速度。

I also recommend using PHP sessions. 我还建议使用PHP会话。 If you do, you can set data in them anywhere you want, as the data is stored server-side. 如果这样做,您可以在所需的任何位置设置数据,因为数据存储在服务器端。 You just have to be sure to start your session right off the bat, so that the cookie is set and the session data is available to your applicatoin. 您只需要确保立即开始会话,就可以设置cookie并向您的应用程序提供会话数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM