php专区

 首页 > php专区 > PHP应用 > 常用功能 > php 用户cookie登录验证与mysql数据登录验证的方法

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

分享到:
【字体:
导读:
          php用户cookie登录验证与mysql数据登录验证的方法:?phpunset($username);if($_COOKIE[ 39;login 39;]){list($c_username,$cookie_hash)=...

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

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

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

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

  1.  
  2.  
  3.   
  4.   Log-In Page 
  5.   
  6.  
  7.   
  8.  Please enter your user details to log-in here... 
  9.  
  10.  "authenticate.php" method = "post"
  11.  Username:
     
  12.  "text" name = "username"
  13.  

     
  14.  Password:
     
  15.  "text" name = "password"
  16.  

     
  17.  "submit" value = "Log In"
  18.   
  19.  
  20.   
  21.  
  22.  

File:authenticate.php

  1.  
  2. $username = $_POST['username']; 
  3. $password = $_POST['password']; 
  4. $self =     $_SERVER['PHP_SELF']; 
  5. $referer =  $_SERVER['HTTP_REFERER']; 
  6.  
  7. if( ( !$username ) or ( !$password ) ) 
  8. { header( "Location:$referer" ); exit(); } 
  9.  
  10. $conn=@mysql_connect( "localhost""userName""password" ) or die"Could not connect" ); 
  11.  
  12. $rs = @mysql_select_db( "my_database"$conn ) or die"Could not select database" ); 
  13. $sql = "select * from users where user_name="$username" and password = password( "$password" )"
  14.  
  15. $rs = mysql_query( $sql$conn ) or die"Could not execute query" ); 
  16.  
  17. $num = mysql_numrows( $rs ); 
  18.  
  19. if$num != 0 ) 
  20. {  
  21.   $msg = "

    Welcome $username - your log-in succeeded!

    "
  22. else 
  23.   header( "Location:$referer" ); exit();  
  24. ?> 
  25.  
  26.  
  27.  
  28.   
  29.   Log-In Authenticated 
  30.    
  31.  
  32.    
  33.    echo$msg ); ?> 
  34.    
  35.  
  36.  
分享到:
php中变量作用域和超级全局变量介绍 - ph...
php中变量作用域和超级全局变量介绍 一、变量的赋值 变量声明后就可以给其赋值,有两种方式:值赋值和引用赋值。  1、值赋值  $color = "red" ;  $sum = 12+"15" ; /* $sum = 27 */ 2、若希望两个变量指向一个值的同一个副本,则需要通过引用赋值. 引用赋值  所创建的变量与另一个变...
PHP 常量 dirname(__file__)获取路径方法...
PHP 常量 dirname(__file__)获取路径方法 __FILE__:被称为PHP魔术常量,返回当前执行PHP脚本的完整路径和文件名,包含一个绝对路径. 1)dirname(__FILE__) 函数返回的是脚本所在在的路径,更新网络,比如文件 b.php 包含如下内容:   我做的测试得到结果: E:websiteothertestcms,这个...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……