gotFusion.com....... Where your adventure begins
Full Service Web HostingNetObjects Fusion 7 Websites Portal

 


 

Note: for this tutorial, we'll assume that Register Globals is set to Off, as it is by default in current PHP versions.

If your web site is on an Apache web server, you can capture the user name from the login and then convert it to a session variable. When an .htaccess login is successful, the user name goes into an array named $_SERVER. ($HTTP_SERVER_VARS prior to PHP 4.1). The value is $_SERVER['PHP_AUTH_USER']. Note: the caps do matter. These arrays and element names are case sensitive.

Using the Auth User Variable "as is"

If all of the pages your authorized user will be accessing are in the directory or directories protected by the .htaccess file, you can simply use the $_SERVER['PHP_AUTH_USER'] variable. It's available until the user navigates away from the protected area.

Converting to a Session Variable.

To use the login user name with pages outside the protected area, you need to convert it to a session variable.

The code is simple:

<?php

echo $_SERVER["PHP_AUTH_USER"]; // to see the user name if you wish

$_SESSION['AuthUser'] = $_SERVER['PHP_AUTH_USER'];

/* This variable assignment only needs to be on the page where you do the conversion. Afterwards, you just use the session variable. */

$username = $_SESSION['AuthUser']; //give short name

echo "Logged in as: $username";

?>

Remember, you need a session start at the top of your pages that will use the session variable. See the Basic Session Tutorial for the "HowTo".

Taking it Further.

Once you've "captured" the user name, you can do a database lookup or do a hard-coded IF/THEN to display a more "friendly" name for the user.

this tutorial written and maintained by Chuck Joslin

Return to the TOP of this page

Posted 31 Aug, 2003


|  Fusion  |  Web Design  |  Hosting  |  Resources  |  gotFusion Store  | 

Problems with this page?  

All content copyright © 2002-2003 gotFusion LLC. The name gotFusion and the gotFusion ® logo are registered trademarks of gotFusion LLC
Copyright, legal notice & privacy statement