欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP网页安全认证的实例详解

程序员文章站 2022-08-13 23:38:20
php网页安全认证的实例详解  不基于数据库:

php网页安全认证的实例详解

 不基于数据库:

<?php
    //unset($_server['php_auth_user']);
    $strauthuser= $_server['php_auth_user'];      
    $strauthpass= $_server['php_auth_pw'];

 if (! ($strauthuser == "a" && $strauthpass == "a")) {
  header('www-authenticate: basic realm="wly"');
  header('http/1.0 401 unauthorized');
  echo "用户验证!!";
  exit;
 } else {
  echo "验证通过";
  
  header("location:http://www.baidu.com");
  //unset($_server['php_auth_user']);  
 }
?>

基于数据库:

<?php
  function authenticate_user() {
    header('www-authenticate: basic realm="secret stash"');
   header("http/1.0 401 unauthorized");
    exit;
  }
 
  if (! isset($_server['php_auth_user'])) {
    authenticate_user();
  } else {
    mysql_pconnect("localhost","authenticator","secret") or die("can't connect to database server!");
    mysql_select_db("java2s") or die("can't select authentication database!");
 
   $query = "select username, pswd from user where username='$_server[php_auth_user]' and pswd=md5('$_server[php_auth_pw]')";
 
    $result = mysql_query($query);
 
    // if nothing was found, reprompt the user for the login information.
    if (mysql_num_rows($result) == 0) {
     authenticate_user();
    }
  }
 ?>

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!