登录时记录密码

登录页面从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:

例子 1. setcookie() send example

<?php
$value
= 'something from somewhere';

setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
?>

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
// Print an individual cookie
echo $_COOKIE["TestCookie"];
echo
$HTTP_COOKIE_VARS["TestCookie"];

// Another way to debug/test is to view all cookies
print_r($_COOKIE);
?>

 

 

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:

例子 2. setcookie() delete example

<?php
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);
?>

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:

例子 3. setcookie() and arrays

<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
    foreach (
$_COOKIE['cookie'] as $name => $value) {
        echo
"$name : $value <br />/n";
    }
}
?>

which prints

three : cookiethree
two : cookietwo
one : cookieone

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值