Password protecting a php file and logging when it is accessed
How do i force users to login to see a file with Php? Basically I am logging user history and I want to protect it. Its nothing critical, just like searches of things they perform, so a simple login/pa开发者_开发技巧ss is good.
Also, on my server, i want to append this file with user history. So I need to be able to write to this file.
THanks!
Youl could use a basic authentication thats built into most http servers.
for example:
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>";
}
see more at: http://php.net/manual/en/features.http-auth.php
You store the user id into a session variable and then check against that. If the session is not expired and the user is logged in, he will have a session id associated.
精彩评论