Link to home
Start Free TrialLog in
Avatar of chrispont
chrispontFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Cannot see REMOTE_USER variable in Apache/2.2.3 (CentOS) using PHP 5.1.6.

Hi,

I'm usually an IIS, .NET windows type developer, but at the moment I'm trying to do some PHP.

I'm trying to get the REMOTE_USER server variable so that I can see who is logged on to the company Intranet and I'm using Apache/2.2.3 (CentOS) , PHP 5.1.6.

I've used the CheckPHP function to get all the config settings and I can see many of the server variables but not REMOTE_USER.

Can anyone help? Please bear in mind that I'm very new to linux, apache and PHP, and only really found a few console commands.

Many Thanks

Chris
Avatar of hampus_b
hampus_b
Flag of Sweden image

The way to access remote_user is like this:

<?
echo $_SERVER['REMOTE_USER'];
?>


Try also to create a php-file with the following content:

<?

phpinfo();

?>

save it as info.php and point your browser to it.

Search for "Apache Environment" in the result. Does REMOTE_USER show up?
Good morning,

Here is the list of Server Variables:
http://us2.php.net/manual/en/reserved.variables.server.php

For your environment, write a quick php script and put just this in it:

<?php
phpinfo();
?>

Run that and it'll show you all the variables available to you on your server.

I think the ones you are looking for are either

$_SERVER['REMOTE_USER']
$_SERVER['REMOTE_ADDR']  (for the ip)
or
$_SERVER['PHP_AUTH_USER']

Hope that all helps.
Avatar of chrispont

ASKER

Sorry guys, I've tried phpinfo(); and cannot see the REMOTE_USER there (athough I can see REMOTE_ADDR and this shows the IP address of the client, not the authenticated user name).

I've tried $_SERVER['REMOTE_USER'] in some scripts and this is always empty.
If you force the login using php's extension to HTTP_AUTH, you can output the username using $_SERVER['PHP_AUTH_USER']

for example, try this script:

<?php
if (!isset($_SERVER['PHP_AUTH_USER']))
{
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
}
else
{
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}


Avatar of Tobias
Hi !

You need to create a .htaccess and .htpasswd file.

The variable $_SERVER['PHP_AUTH_USER'] is used when you have put in place an authentication method.

Follow this tutorial and you would be able to get the user.

http://www.freewebmasterhelp.com/tutorials/htaccess/3


Best Regards
Well, that'll get you the same thing as what I have proposed, just with more files. If you are already using php for your page, you can do the HTTP_AUTH via php and then use $_SERVER['PHP_AUTH_USER'] to get the username.
Yes I don't have say that is different. Just trying to explain more how it's working. I don't care about get more points, just help people.
Right. Same here. :)

My point was just that he is used to IIS and not apache, so he may not want to deal with extra files (.htaccess, .htpasswd) in setting this up.
ASKER CERTIFIED SOLUTION
Avatar of DarkFish
DarkFish

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial