getenv('REMOTE_USER)不会将值插入数据库日志中

时间:2014-01-07 22:00:35

标签: php mysql

我有一个存储用户访问日志的mysql数据库。问题是数据库中的每个其他指定列都填充了相应的数据,但user_info列全部为空。有点像变量$ user不包含值。我现在已经有一段时间了,一切似乎都是对的,但它并没有完全实现我的目的。谢谢你的帮助。 这是MySql代码。

    //Database Connection
    require( 'db.php' );

/* The User Info to be gathered */

$time       = date( "M j, Y | G:i:s A" );
$user       = getenv( 'REMOTE_USER' );
$ip         = getenv( 'REMOTE_ADDR' );
$userAgent  = getenv( 'HTTP_USER_AGENT' );
$referrer   = getenv( 'HTTP_REFERER' );
$currentPage    = getenv( 'REQUEST_URI' );
$query      = getenv( 'QUERY_STRING' );

if( isset( $time, $ip, $userAgent, $referrer, $currentPage ) ) {
/* Log all the required information into the database */
    $prep = $db->prepare(
        "INSERT INTO renal_accessLog(
            ip_address,
            user_info,
            time,
            page,
            referrer,
            search,
            user_agent
        ) VALUES ( ?, ?, ?, ?, ?, ?, ? )
        ");
    $prep->bind_param(
            'sssssss',
            $ip,
            $user,
            $time,
            $currentPage,
            $referrer,
            $query,
            $userAgent
        );
}
$prep->execute();
$prep->close();

$db->close();

这是我在登录端的代码。确定登录的用户并获取该用户的信息。

//find out who we are dealing with
        if ( true || isset($HTTP_SERVER_VARS['PHP_AUTH_USER'])) {
            $username = $HTTP_SERVER_VARS['PHP_AUTH_USER'];
//          if ($HTTP_SERVER_VARS['HTTP_HOST'] == 'example.com' && (!$HTTP_SERVER_VARS['PHP_AUTH_USER']))  {
//              // this is a little kludge for development purposes
//              // if running in locsl mode and nobody has logged in, 
//              $username = "Teresa"; // Teresa, the developer
//          }

            //look for user record, and if found, remember his model
            $myQuery = "select people.FirstName, people.LastName, modelusername, people.MDFlag, people.ContactId from users left join people on people.ContactId = users.contactId where username = '$username'";
            $result = sql($myQuery);
            $row = mysql_fetch_row($result);
            $firstName = $row[0];
            $lastName = $row[1];
//          $initials = strtoupper("tm");
            $UserInitials = (substr($firstName,0,1) . substr($lastName,0,1));
            $userFullName = $firstName . ' ' . $lastName;
            if ($row[3] == 'Y') {
                $userFullName .= ", MD";
            }
            $modelusername = $row[2];
            $userId = $row[4];
            echo "<center>Hello, ". $firstName . " " . $lastName . " (" . $UserInitials . ")." ;
            if ($modelusername) {
                echo "You are authorized as '$modelusername'.  " ;
            }

1 个答案:

答案 0 :(得分:0)

尝试更换:

$user       = getenv( 'REMOTE_USER' );
$ip         = getenv( 'REMOTE_ADDR' );
$userAgent  = getenv( 'HTTP_USER_AGENT' );
$referrer   = getenv( 'HTTP_REFERER' );
$currentPage    = getenv( 'REQUEST_URI' );
$query      = getenv( 'QUERY_STRING' );

使用:

$user       = $_SERVER['REMOTE_USER'];
$ip         = $_SERVER['REMOTE_ADDR'];
$userAgent  = $_SERVER['HTTP_USER_AGENT'];
$referrer   = $_SERVER['HTTP_REFERER'];
$currentPage = $_SERVER['REQUEST_URI'];
$query      = $_SERVER['QUERY_STRING'];