php使用cookie保存用户登录的用户名实例_PHP

WBOY
Release: 2016-05-31 13:17:48
Original
822 people have browsed it

本文实例讲述了php使用cookie保存用户登录的用户名的方法。分享给大家供大家参考。具体实现方法如下:

用户登录文件:login.php

代码如下:



用户登录


function getCookieUsername(){
 if(empty($_COOKIE['username'])){
  return "";
 }else{
  return $_COOKIE['username'];
 }
}
?>

用户名:

密码:

是否保存用户名:




后台文件:admin.php

代码如下:

if(!empty($_POST['sub']) && $_POST['username']=="admin"){
 echo "欢迎".$_POST['username']." 登录成功";
 if(!empty($_POST['yes'])){
  setCookie("username",$_POST['username'],time()+3600*24*30);
 }else{
  setCookie("username","",time()-10);
 }
}else{
 echo "你的账号错误,请重新输入
";
}
echo "返回登录页面";
?>

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!