登录页面从cookie里面取值:
<td width="77" height="30" align="right" valign="middle"><span class="STYLE1">用户名:</span></td>
<td width="148" valign="middle">
<input name="username" id=username type="text" class="TextStyle" size="12" value="<?php if(isset($_COOKIE['cookie_uname'])){echo $_COOKIE['cookie_uname'];} ?>"/>
</td>
</tr>
<tr>
<td height="30" align="right" valign="middle"><span class="STYLE1">密 码:</span></td>
<td valign="middle">
<input type="password" id=password name="password" class="TextStyle" size="12" value="<?php if(isset($_COOKIE['cookie_upwd'])){echo $_COOKIE['cookie_upwd'];} ?>"/>
</td>
提交页面保存cookie:
// cookie中存放用户信息方便用户的下次登录 ------start
$persistence=$_POST['persistence'];
if($persistence == "true")
{
$cookie_time = time()+60*60*24*30;//cookie页面有效时间为一个月
setcookie("cookie_uname", $username,$cookie_time);
setcookie("cookie_upwd", $passwd,$cookie_time);
}
else
{
if(isset($_COOKIE['cookie_uname']) || isset($_COOKIE['cookie_upwd']))
{
$cookie_time = time()-3600;//cookie失效
setcookie("cookie_uname", "",$cookie_time);
setcookie("cookie_upwd", "",$cookie_time);
}
}
// cookie中放 -------------------------------------------------end
setcookie()函数例子(摘自php使用手册):
Some examples follow how to send cookies:
Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name. If you don't want this, you can use setrawcookie() instead if you are using PHP 5. To see the contents of our test cookie in a script, simply use one of the following examples:
<?php |
When deleting a cookie you should assure that the expiration date is in the past, to trigger the removal mechanism in your browser. Examples follow how to delete cookies sent in previous example:
You may also set array cookies by using array notation in the cookie name. This has the effect of setting as many cookies as you have array elements, but when the cookie is received by your script, the values are all placed in an array with the cookie's name: