Php 以编程方式清除cookie
我有一个名为Php 以编程方式清除cookie,php,mysql,cookies,Php,Mysql,Cookies,我有一个名为signin.php的登录页面,用户可以在其中输入电子邮件和密码。单击提交按钮,页面将指向连接\u validate.php。此页面使用数据库验证用户输入的数据。如果是注册用户,页面将指向calendar.php。如果输入的数据不正确,则应重定向到signin.php。如果输入的数据不正确,我会这样放置cookie: //action to be done if e mail id and password matches with database records
signin.php
的登录页面,用户可以在其中输入电子邮件和密码。单击提交按钮,页面将指向连接\u validate.php
。此页面使用数据库验证用户输入的数据。如果是注册用户,页面将指向calendar.php
。如果输入的数据不正确,则应重定向到signin.php
。如果输入的数据不正确,我会这样放置cookie:
//action to be done if e mail id and password matches with database records
if(mysql_num_rows($result)>0)
{
header('location:calendar.php');
}
//action to be done if e mail id and password does not matches with database records
else
{
setcookie('message','incorrect login data');
header('location:signin.php');
}
<?php
include("include/minfooter.php");
if(isset($_COOKIE["message"]))
{
if(!($_COOKIE["message"]==" "))
{
echo "<script>
alert('Incorrect login information');
</script>";
setcookie("message"," ",time()-3600);
}
}
?>
在signin.php中,我编写了在登录信息不正确时显示警报的代码,如下所示:
//action to be done if e mail id and password matches with database records
if(mysql_num_rows($result)>0)
{
header('location:calendar.php');
}
//action to be done if e mail id and password does not matches with database records
else
{
setcookie('message','incorrect login data');
header('location:signin.php');
}
<?php
include("include/minfooter.php");
if(isset($_COOKIE["message"]))
{
if(!($_COOKIE["message"]==" "))
{
echo "<script>
alert('Incorrect login information');
</script>";
setcookie("message"," ",time()-3600);
}
}
?>
如下更新您的signin.php
<?php
include("include/minfooter.php");
if (isset($_COOKIE["message"]))
{
echo "<script>
var delete_cookie = function(name) {
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};
var msg = '" . $_COOKIE["message"] . "';
if (msg != '')
alert('Incorrect login information');
delete_cookie('message');
</script>";
}
?>
如果使用会话,则可以使用$\u会话变量而不是cookie值。此外,在输出内容后不能使用setcookie(),因为setcookie()将发送一个HTTP头,该头必须在发送任何内容之前发送
session_start();
//action to be done if email id and password matches with database records
if (mysql_num_rows($result) > 0)
{
header('Location: calendar.php');
exit;
}
//action to be done if email id and password does not matches with database records
else
{
$_SESSION['message'] = 'incorrect login data';
header('Location: signin.php');
exit;
}
然后:
确定可能最好使用会话,在$\u会话数组上使用索引['messages'],然后清除,当用户离开页面后需要引用某些信息时,应使用cookie。我使用Cookie制作了代码,但是考虑使用session:
<?php include("include/minfooter.php");
if(isset($_COOKIE["message"]) && !empty($_COOKIE["message"])
{
echo "<script>
var msg = '<?php echo $_COOKIE["message"];?>';
if (msg != "")
alert('Incorrect login information');
</script>";
unset($_COOKIE["message"]);
}
?>
这是错误的,setcookie()设置了一个HTTP头,在您已经将内容输出到浏览器后无法放入该头。你的语法也全错了。我想说75%正确。你有两次if(isset($\u COOKIE[“message”]),这仍然不是正确的方法。更好,但不是做OP想要的事情的正确方法。OP希望显示登录错误,这意味着他们正在创建一个登录会话,这正是$会话变量的用途。不需要javascript,不需要额外的cookies,所有这些都只是简单的PHP。