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等级我选择的是低。
本人初学者,请高手指点

解决方案 »

  1.   

    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>
      

  2.   

    cookie 需要用 setcookie 函数设置
    $_COOKIE 数组仅用于读取