doushi6864 2014-11-18 01:11
浏览 24
已采纳

Mysql用户身份验证不起作用[关闭]

I'm working around with some php + mysql and for user authentication. I've implemented this:

<?php

    if (!isset($_SERVER['PHP_AUTH_USER']))
    {
            header('WWW-Authenticate: Basic realm="Administrator"');
            header('HTTP/1.0 401 Unauthorized');
            echo  'Username and password are required to view this page';
            exit;
    }
    else
    {
            if(isset($_ENV["OPENSHIFT_MYSQL_DB_HOST"]))
            {

                    $conn = mysql_connect($_ENV["OPENSHIFT_MYSQL_DB_HOST"].':'.$_ENV["OPENSHIFT_MYSQL_DB_PORT"],
                                          $_ENV["OPENSHIFT_MYSQL_DB_USERNAME"],
                                          $_ENV["OPENSHIFT_MYSQL_DB_PASSWORD"]) or die(mysql_error());
            }
            else
            {
                    $conn = mysql_connect("localhost", "user", "pass") or die(mysql_error());
            }
            mysql_select_db("db", $conn) or die(mysql_error());
            $sqlquery = "SELECT * FROM admin_users";
            $result = mysql_query($sqlquery, $conn) or die(mysql_error());
            $access_granted = 0;
            while($user = mysql_fetch_array($result))
            {
                    if ($_SERVER['PHP_AUTH_USER'] == $user['user_name'] &&
                        $_SERVER['PHP_AUTH_PW'] == $user['password'])
                    {
                            $access_granted++;
                    }
            }
            if(!$access_granted)
            {
                    header('WWW-Authenticate: Basic realm="Administrator"');
                    header('HTTP/1.0 401 Unauthorized');
                    echo "Invalid username and/or password";
                    exit;
            }
    }
?>

The user, pass, table, etc are correct but it's not working properly, no redirect but no errors also

Am I doing something wrong?

  • 写回答

1条回答 默认 最新

  • doue8385 2014-11-18 02:56
    关注

    The Problem is about my based CGI server and the way it deals with header auth

    adding this into .htaccess solves the issue

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
    </IfModule>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题