欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php使用cookie实现记住用户名和密码实现代码

程序员文章站 2023-10-30 08:22:16
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<form id="form1" name="form1" method="post" action="check_remember.php">
<table width="300" border="1" align="center" cellpadding="0" cellspacing="0">
<thead>
 <tr> 
 <td colspan="2" align="center"><b>记住用户名和密码</b></td> 
 </tr>
</thead> 
 <tr align="center">
 <td>用 户 名:</td>
 <td><input type="text" value="<?php echo $_cookie['name'];?>" name="name"></td>
 </tr>
 <tr align="center">
 <td>密码:</td>
 <td><input type="password" name="password" value="<?php echo $_cookie['password']?>"></td>
 </tr>
 <tr align="center">
 <td>记住用户名和密码</td>
 <td><?php if($_cookie['remember'] == 1){?><input type="checkbox" name="remember" value="1" checked><?php }else{($_cookie['remember'] == "")?><input type="checkbox" name="remember" value="1"><?php }?></td>
 </tr>
 <tr align="center">
 <td colspan="2"><input type="submit" name="submit" value="提交" /></td>
 </tr>
</table>
</form>
 
check_remember.php
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php
 $name = $_post['name'];
 $password = $_post['password'];
 $remember = $_post['remember'];
 if($remember == 1){
 setcookie('name',$name,time()+3600);
 setcookie('password',$password,time()+3600);
 setcookie('remember',$remember,time()+3600);
 }else{
 setcookie('name',$name,time()-3600);
 setcookie('password',$password,time()-3600);
 setcookie('remember',$remember,time()-3600);
 }
 echo "<a href=\"remember.php\">返回</a>";
?>