Reputation: 1279
i am using the following password code http://www.zubrag.com/scripts/password-protect.php
it is working fine but when i try to echo out the logged in user's info, i am only able to do it once, at the time of login. after that if i refresh the page, the page is still accessible but the logged in user's name is not echoed out can someone help me with this i have the following code on the first line of the pages that i want to protect
<?php require("/home/abcd/abcd/password_protect.php"); ?>
i am echoing out the name by using
echo "Welcome $login";
Upvotes: 1
Views: 384
Reputation: 479
What the script basically does is that it has a pre-initialised list of login and passwords stored in the array $LOGIN_INFORMATION
.
$LOGIN_INFORMATION = array(
'zubrag' => 'root',
'test' => 'testpass',
'admin' => 'passwd'
);
Now it takes a login/password combination from the user and matches with the stored values. As per your question , you want to show the name of the authenticated user.
For that you can use a session
At the start of the page(Very first line) write
`session_start();`
then
$_SESSION['username'] = $username
//the authenticated username
Now at the beginning of every page where you want to print the name ,write
session_start();
then
echo $_SESSION['username'];
After you are done and you want to log out ( or cancel the session) write
session_unregister('username');
Upvotes: 1
Reputation:
at the start of each page:
session_start();
when user authenticaed succesfully:
$_SESSION['keyname'] = 'value';
wherever you need to access it:
echo $_SESSION['keyname']
Upvotes: 0
Reputation: 7946
That script stores the hash of the username and password to a cookie called verify
:
setcookie("verify", md5($login.'%'.$pass), $timeout, '/');
As such you can't extract the $login
directly, however you can iterate through the possibilities from $LOGIN_INFORMATION
until you find it:
$login = null;
$found = false;
foreach($LOGIN_INFORMATION as $key=>$val) {
$lp = (USE_USERNAME ? $key : '') .'%'.$val;
if ($_COOKIE['verify'] == md5($lp)) {
$found = true;
$login = $key;
}
}
Upvotes: 0