当前位置:首页 > PHP教程 > php应用 > 列表

php用户登录代码,cookie,数据库

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-11 12:51:47 浏览: 评论:0 

下面我们要讲到二种用户登录方法一种是利用window验证加数据库密码验证实现登录,后者是利用cookie记录用户信息进行登录,有需要的朋友可以试下.

  1. <?php 
  2.    function authenticate_user() { 
  3.       header('WWW-Authenticate: Basic realm="Secret Stash"'); 
  4.       header("HTTP/1.0 401 Unauthorized"); 
  5.       exit
  6.    } 
  7.  
  8.    if (! isset($_SERVER['PHP_AUTH_USER'])) { 
  9.       authenticate_user(); 
  10.    } else { 
  11.       mysql_pconnect("localhost","authenticator","secret"or die("Can't connect to database server!"); 
  12.       mysql_select_db("java2s"or die("Can't select authentication database!"); 
  13.  
  14.       $query = "SELECT username, ps教程wd FROM user WHERE username='$_SERVER[PHP_AUTH_USER]' AND pswd=MD5('$_SERVER[PHP_AUTH_PW]')"
  15. //开源代码phpfensi.com 
  16.       $result = mysql_query($query); 
  17.  
  18.       // If nothing was found, reprompt the user for the login www.111cn.net information. 
  19.       if (mysql_num_rows($result) == 0) { 
  20.          authenticate_user(); 
  21.       } 
  22.    } 
  23. ?> 

利用cookie登录方法,实例代码如下:

  1. <html> 
  2.   <head> 
  3.   <title>Enter Password</title> 
  4.   </head> 
  5.   <body> 
  6.   <form name="forml" method="POST" action="CookieBasedPasswordLogin.php"
  7.     <table> 
  8.       <tr> 
  9.        <td colspan="2" > 
  10.          <div align="center"><b>Please Specify the Password</b></div> 
  11.        </td> 
  12.      </tr> 
  13.    <tr>> 
  14.      <td> 
  15.        <div align="right">Customer ID</div> 
  16.      </td> 
  17.      <td> 
  18.        <input type="text" name="username"
  19.      </td> 
  20.    </tr> 
  21.    <tr> 
  22.      <td> 
  23.        <div align="right">Password</div> 
  24.      </td> 
  25.      <td> 
  26.        <input type="password" name="password"
  27.      </td> 
  28.    </tr> 
  29.    <tr> 
  30.      <td colspan="2"
  31.        <center> 
  32.          <input type="submit" name="Submit" value="Login"
  33.        </center> 
  34.      </td> 
  35.     </tr> 
  36.    </table> 
  37.   </form> 
  38.   </body> 
  39.   </html> 
  40. <!-- CookieBasedPasswordLogin.php 
  41. <?php 
  42.     $now = getdate(); 
  43.     $storetime$now["weekday"] . " " . $now["month"] ." " . $now["year"] ; 
  44.     $storetime.=" Time : "
  45.  
  46.     if ($now["hours"] < 10) { 
  47.       $storetime.= "0" . $now["hours"]; 
  48.     } else { 
  49.       $storetime.= $now["hours"]; 
  50.     } 
  51.    
  52.     $storetime.= ":"
  53.     if ($now["minutes"]<10) { 
  54.       $storetime.= "0" . $now["minutes"]; 
  55.     } else { 
  56.       $storetime.= $now["minutes"]; 
  57.     } 
  58.      
  59.     $storetime.= ": "
  60.     if ($now["seconds"] <10) { 
  61.       $storetime.= "0" . $now["seconds"]; 
  62.     } else { 
  63.       $storetime.= $now["seconds"]; 
  64.     } 
  65.     if (isset($data)) { 
  66.        $counter=++$data[l]; 
  67.         setcookie("data[0]",$storetime,time() + (60*60*24)); 
  68.         setcookie("data[l]"$counter,time() + (60*60*24)); setcookie("data[2]",$username,time() + (60*60*24)); 
  69.         echo "<b><center>Hi " . $data[2] . " ! !</center></b><br>n"
  70.         echo "<b><center>Last Login Time :" .$data[0] . "</center></b><br>n"
  71.         echo "<b><center>Current Date :" .$storetime"</center></b><br>n"
  72.         echo "<b><center>Page View Count :" . $data[l]. "</center></b><br>n"
  73.         echo "<b><center>You have successfully logged in!</center></b>"
  74.         echo ("<b><contor>You can access this area without entering a password for the next 24 hours!</center></b>"); 
  75.    } else { 
  76.     if (isset($username) && isset($password)) { 
  77.      if ($password=="superpass") { 
  78.           $counter=0; 
  79.           setcookie("data[0]",$storetime,time() + (60*60*24)); 
  80.           setcookie("data[l]",$counter,time() + (60*60*24)); 
  81.           setcookie("data[2]",$username,time() + (60*60*24)); 
  82.           $url="Location: cookieimp.php"
  83.           header($url); 
  84.      }else
  85.           echo "<hl><center>INVALID PASSWORD!!!</center></hl>"
  86.      } 
  87.     } 
  88.   } 
  89.   ?>

Tags: php用户登录 cookie用户登录

分享到: