最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php 用户cookie登录验证与mysql数据登录验证的方法

时间:2014-06-09 16:40来源: 作者: 点击: 次
php用户cookie登录验证与mysql数据登录验证的方法: ?php unset( $username ); if ( $_COOKIE [ login ]){ list( $c_username , $cookie_hash )=split( , , $_COOKIE [ login ]); if (md5( $c_username . $secret_word )== $cookie_hash ){ $username =

php用户cookie登录验证与mysql数据登录验证的方法:

  1. <?php 
  2. unset($username); 
  3. if ($_COOKIE['login']) { 
  4.     list($c_username,$cookie_hash) = split(',',$_COOKIE['login']); 
  5.     if (md5($c_username.$secret_word) == $cookie_hash) { 
  6.         $username = $c_username; 
  7.     } else { 
  8.         print "You have sent a bad cookie."; 
  9.     } 
  10. } 
  11.  
  12. if ($username) { 
  13.     print "Welcome, $username."; 
  14. } else { 
  15.     print "Welcome, anonymous user."; 
  16. } 
  17. ?> 

看个完整的与数据库连接登录的代码:

  1. <html> 
  2.  
  3.  <head> 
  4.   <title>Log-In Page</title> 
  5.  </head> 
  6.  
  7.  <body> 
  8.  Please enter your user details to log-in here... 
  9.  
  10.  <form action = "authenticate.php" method = "post"> 
  11.  Username:<br> 
  12.  <input type = "text" name = "username"> 
  13.  <br><br> 
  14.  Password:<br> 
  15.  <input type = "text" name = "password"> 
  16.  <br><br> 
  17.  <input type = "submit" value = "Log In"> 
  18.  </form> 
  19.  
  20.  </body> 
  21.  
  22. </html> 

File:authenticate.php

  1. <?php 
  2.  
  3. $username = $_POST['username']; 
  4. $password = $_POST['password']; 
  5. $self =     $_SERVER['PHP_SELF']; 
  6. $referer =  $_SERVER['HTTP_REFERER']; 
  7.  
  8. if( ( !$username ) or ( !$password ) ) 
  9. { header( "Location:$referer" ); exit(); } 
  10.  
  11. $conn=@mysql_connect( "localhost", "userName", "password" ) or die( "Could not connect" ); 
  12.  
  13. $rs = @mysql_select_db( "my_database", $conn ) or die( "Could not select database" ); 
  14. $sql = "select * from users where user_name="$username" and password = password( "$password" )"; 
  15.  
  16. $rs = mysql_query( $sql, $conn ) or die( "Could not execute query" ); 
  17.  
  18. $num = mysql_numrows( $rs ); 
  19.  
  20. if( $num != 0 ) 
  21. {  
  22.   $msg = "<h3>Welcome $username - your log-in succeeded!</h3>"; 
  23. } 
  24. else 
  25. { 
  26.   header( "Location:$referer" ); exit();  
  27. } 
  28. ?> 
  29.  
  30. <html> 
  31.  
  32.  <head> 
  33.   <title>Log-In Authenticated</title> 
  34.   </head> 
  35.  
  36.   <body> 
  37.    <?php echo( $msg ); ?> 
  38.   </body> 
  39.  
  40. </html> 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容