php 利用 记住用户登录名与密码方法
php实现记住密码自动登录方法不止一个,下面出现有二个emptyempty,其实是一个,那是因为代码高亮有bug,希望对大家有帮助.
一,用户登录的check,代码如下:
-
-
- function checklogin(){
- if(emptyempty($_session['user_info'])){
- if(emptyempty($_cookie['username']) || emptyempty($_cookie['password'])){
- header(”location:login.php?req_url=”.$_server['request_uri']);
- }else{
- $user = getuserinfo($_cookie['username'],$_cookie['password']);
- if(emptyempty($user)){
- header(”location:login.php?req_url=”.$_server['request_uri']);
- }else{
- $_session['user_info'] = $user;
- }
- }
- }
- }
-
- function checklogin(){
- if(emptyempty($_session['user_info'])){
- if(emptyempty($_cookie['username']) || emptyempty($_cookie['password'])){
- header(”location:login.php?req_url=”.$_server['request_uri']);
- }else{
- $user = getuserinfo($_cookie['username'],$_cookie['password']);
- if(emptyempty($user)){
- header(”location:login.php?req_url=”.$_server['request_uri']);
- }else{
- $_session['user_info'] = $user;
- }
- }
- }
- }
在访问后台的每个页面时,都要先进行上面的检查.
二,用户提交登录信息,当用户填写用户名和密码后就提交到这儿来,代码如下:
- $username = trim($_post['username']);
- $password = md5(trim($_post['password']));
- $validatecode = $_post['validatecode'];
- $ref_url = $_get['req_url'];
- $remember = $_post['remember'];
-
- $err_msg = ”;
- if($validatecode!=$_session['checksum']){
- $err_msg = “验证码不正确”;
- }elseif($username==” || $password==”){
- $err_msg = “用户名和密码都不能为空”;
- }else{
- $row = getuserinfo($username,$password);
-
- if(emptyempty($row)){
- $err_msg = “用户名和密码都不正确”;
- }else{
- $_session['user_info'] = $row;
- if(!emptyempty($remember)){
- setcookie(”username”, $username, time()+3600*24*365);
- setcookie(”password”, $password, time()+3600*24*365);
- }
- if(strpos($ref_url,”login.php”) === false){
- header(”location:”.$ref_url);
- }else{
- header(”location:main_user.php”);
- }
- }
- }
- $username = trim($_post['username']);
- $password = md5(trim($_post['password']));
- $validatecode = $_post['validatecode'];
- $ref_url = $_get['req_url'];
- $remember = $_post['remember'];
-
- $err_msg = ”;
- if($validatecode!=$_session['checksum']){
- $err_msg = “验证码不正确”;
- }elseif($username==” || $password==”){
- $err_msg = “用户名和密码都不能为空”;
- }else{
- $row = getuserinfo($username,$password);
-
- if(emptyempty($row)){
- $err_msg = “用户名和密码都不正确”;
- }else{
- $_session['user_info'] = $row;
- if(!emptyempty($remember)){
- setcookie(”username”, $username, time()+3600*24*365);
- setcookie(”password”, $password, time()+3600*24*365);
- }
- if(strpos($ref_url,”login.php”) === false){
- header(”location:”.$ref_url);
- }else{
- header(”location:main_user.php”);
- }
- }
- }
关于$ref_url简单解释一下,假如:用户a访问b.php,但是a用户没有登录,跳转到登录页面login.php,在登录页面填完用户和密码后,确定后又跳转到b.php这个页面,而不是跳转一个默认的页面main_user.php,因为b.php是用户a想去的那个页面,所以用户体验会好一点.
三,当用户点退出时,清出记录登录状态
为什么要这样做呢,因为如果有别人用你的电脑的话,他们有可能会浏览到你的个人隐私,所以当用户特意点了退出时,最好是把记录登录状态取消掉,代码如下:
-
- function logout(){
- unset($_session['user_info']);
- if(!emptyempty($_cookie['username']) || emptyempty($_cookie['password'])){
- setcookie(”username”, null, time()-3600*24*365);
- setcookie(”password”, null, time()-3600*24*365);
- }
- }