cookie不会传递的问题

redoffice 2009-05-31 05:17:19
5-3-2.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>5-3-2</title>
</head>

<body>
<form name="form1" method="post" action="5-3action.php">
<table width="280" height="96" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">
<tr>
<td colspan="2" align="center" bgcolor="#ffffff">用户登陆</td>
</tr>
<tr>
<td align="right" bgcolor="#ffffff">用户名:</td>
<td align="left" bgcolor="#ffffff">
<input type="text" name="user_name" size="12">
</td>
</tr>
<tr>
<td align="right" bgcolor="#ffffff">口令:</td>
<td align="left" bgcolor="#ffffff">
<input type="password" name="user_pw" size="12">
</td>
</tr>
<tr>
<td colspan="2" align="right" bgcolor="#ffffff">
<input type="submit" name="Submit" value="提交" size="12">
<input type="reset" name="Submit2" value="重置" size="12">
</td>
</tr>
</table>
</form>
</body>
</html>


5-3action.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>COOKIE实现用户登陆</title>
</head>

<body>
<?php
$_COOKIE["user_name"]=$_POST["user_name"];
$_COOKIE["user_pw"]=$_POST["user_pw"];
if($_COOKIE["user_name"]=="php" && $_COOKIE["user_pw"]== "php5")
{
echo "恭喜您,用户名和口令正确,登陆成功!";
}
else
{
echo "您输入的用户名为:".$_COOKIE["user_name"];
echo "口令为:".$_COOKIE["user_pw"];
echo "但是不正确,请尝试:php和php5";
}

?>

<br><a href = "5-3action-check.php">单击检测Cookie的值是否可以页间传递</a>
</body>
</html>

5-3-check.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>5-3-check</title>
</head>
<?php
if ($_COOKIE["user_name"]!="" && $_COOKIE["user_pw"]!= "")
{
echo "cookie页间传递成功!<br>";
echo "您输入的用户名为:".$_COOKIE["user_name"];
echo "口令为:".$_COOKIE["user_pw"];
}
else
{
echo "cookie页间传递失败!<br>";
}
?>
<body>
</body>
</html>

如程序所示:我在第一个页面中填写用户名密码,第二个页面中可以显示出来我填写的内容和对错,这个传递没有问题,但是到第三个页面的时候发现传递不过来了
为什么?cookie等级我选择的是低。
本人初学者,请高手指点
...全文
109 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2009-06-01
  • 打赏
  • 举报
回复
cookie 需要用 setcookie 函数设置
$_COOKIE 数组仅用于读取
  • 打赏
  • 举报
回复
没看见你的setcookie呀
Exbox 2009-05-31
  • 打赏
  • 举报
回复
cookie变量没有定义就直接赋值了,自然是传递不过去的,把第二个页面(5-3action.php )修改一下
<?php
setcookie("user_name",$_POST["user_name"],time()+60);
setcookie("user_pw",$_POST["user_pw"],time()+60);
if($_COOKIE["user_name"]=="php" && $_COOKIE["user_pw"]== "php5")
{
echo "恭喜您,用户名和口令正确,登陆成功!";
}
else
{
echo "您输入的用户名为:".$_COOKIE["user_name"];
echo "口令为:".$_COOKIE["user_pw"];
echo "但是不正确,请尝试:php和php5";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>COOKIE实现用户登陆</title>
</head>

<body>


<br><a href = "c.php">单击检测Cookie的值是否可以页间传递</a>
</body>
</html>

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧