要实现记住密码自动登录的功能我们大多数据都是利用了客户端的cookies来实现,我们利用php也不例外,有需要的朋友可以参考一下.
php制作记住密码自动登录的解决思路,其实也就是对session,cookies的操作,实例代码如下:
-
- 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");
- }
- }
- }
当用户点退出时,清出记录登录状态,退出登录,代码如下:
- 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);
- }
- }
|