当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP网页安全认证的实例详解

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

2017年12月08日  | 移动技术网IT编程  | 我要评论

苏燕君,阿根廷杜高犬价格,保山美食

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();
    }
  }
 ?>

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网