.htaccess cannot create or populate PHP session but you can get:
$_SERVER['PHP_AUTH_USER']
value in your PHP code and store in session.
题
Is it possible to get the username of someone who has succesfully authenticated through the htpasswd process and put that in a session variable?
User: mave
Pass: mave123
Successful login: write 'mave' to session?
解决方案
.htaccess cannot create or populate PHP session but you can get:
$_SERVER['PHP_AUTH_USER']
value in your PHP code and store in session.
其他提示
You can try $_SERVER['PHP_AUTH_USER']
, but I do not know if this works with htaccess, since it's not triggered by PHP.
As other users already answered, you can use the $_SERVER['PHP_AUTH_USER']
to reference the user. But, it might fail if PHP is running in CGI mode. To set teh User/Pass server variables in CGI mode; add the following in you authorization directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]
</IfModule>
Now, from PHP, you can use:
if( preg_match('/Basics+(.*)$/i', $_SERVER['REMOTE_USER'], $matches) ) {
list($name, $password) = explode(':', base64_decode($matches[1]));
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
}