1.

Solve : Login (php)?

Answer»

Code: [Select]<?php

###############################################################
#PagePasswordProtect2.13
###############################################################
#Visithttp://www.zubrag.com/scripts/forupdates
###############################################################
#
#Usage:
#Setusernames/passwordsbelowbetweenSETTINGSSTARTandSETTINGSEND.
#Openitinbrowserwith"help"parametertogetthecode
#toaddtoallfilesbeingprotected.
#Example:password_protect.php?help
#Includeprotectionstringwhichitgaveyouintoeveryfilethatneedstobeprotected
#
#AddfollowingHTMLcodetoyourpagewhereyouwanttohavelogoutlink
#<ahref="http://www.example.com/path/to/protected/page.php?logout=1">Logout</a>
#
###############################################################

/*
-------------------------------------------------------------------
SAMPLEifyouonlywanttorequestloginandpasswordonloginform.
Eachrowrepresentsdifferentuser.

$LOGIN_INFORMATION=array(
'zubrag'=>'root',
'test'=>'testpass',
'admin'=>'passwd'
);

--------------------------------------------------------------------
SAMPLEifyouonlywanttorequestonlypasswordonloginform.
Note:onlypasswordsarelisted

$LOGIN_INFORMATION=array(
'root',
'testpass',
'passwd'
);

--------------------------------------------------------------------
*/

##################################################################
#SETTINGSSTART
##################################################################

//Addlogin/passwordpairsbelow,likedescribedabove
//NOTE:allrowsexceptlastmusthavecomma","attheendofline
$LOGIN_INFORMATION=array(
'zubrag'=>'root',
'kvasten'=>'klonaminko1'
);

//requestlogin?true-showloginandpasswordboxes,false-passwordboxonly
define('USE_USERNAME',true);

//Userwillberedirectedtothispageafterlogout
define('LOGOUT_URL','http://web.comhem.se/nameless');

//timeoutafterNNminutesofinactivity.Setto0tonottimeout
define('TIMEOUT_MINUTES',1);

//ThisparameterisonlyusefulwhenTIMEOUT_MINUTESisnotzero
//true-timeouttimefromlastactivity,false-timeouttimefromlogin
define('TIMEOUT_CHECK_ACTIVITY',false);

##################################################################
#SETTINGSEND
##################################################################


///////////////////////////////////////////////////////
//donotchangecodebelow
///////////////////////////////////////////////////////

//showusageexample
if(isset($_GET['help'])){
die('Includefollowingcodeintoeverypageyouwouldliketoprotect,attheverybeginning(firstline):<br>&lt;?phpinclude("'.str_replace('\\','\\\\',__FILE__).'");?&gt;');
}

//timeoutinseconds
$timeout=(TIMEOUT_MINUTES==0?0:time()+TIMEOUT_MINUTES*60);

//logout?
if(isset($_GET['logout'])){
setcookie("verify",'',$timeout,'/');//clearpassword;
header('Location:'.LOGOUT_URL);
exit();
}

if(!function_exists('showLoginPasswordProtect')){

//showloginform
functionshowLoginPasswordProtect($error_msg){
?>
<html>
<HEAD>
<title>Skriv INN lösenord för att komma inn på denna sidan</title>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</head>
<body>
<style>
input { border: 1px solid black; }
</style>
<form method="post">
<h3>Logga inn</h3>
<font color="red"><?phpecho$error_msg;?></font><br />
<?phpif(USE_USERNAME)echo'Login:<br/><inputtype="input"name="access_login"/><br/>Password:<br/>';?>
<input type="password" name="access_password" /><p></p><input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

<?php
//stopatthispoint
die();
}
}

//userprovidedpassword
if(isset($_POST['access_password'])){

$login=isset($_POST['access_login'])?$_POST['access_login']:'';
$pass=$_POST['access_password'];
if(!USE_USERNAME&&!in_array($pass,$LOGIN_INFORMATION)
||(USE_USERNAME&&(!array_key_exists($login,$LOGIN_INFORMATION)||$LOGIN_INFORMATION[$login]!=$pass))
){
showLoginPasswordProtect("Incorrectpassword.");
}
else{
//setcookieifpasswordwasvalidated
setcookie("verify",md5($login.'%'.$pass),$timeout,'/');

//Someprograms(likeForm1Bilder)check$_POSTarraytoseeifparameterspassed
//Soneedtoclearpasswordprotectorvariables
unset($_POST['access_login']);
unset($_POST['access_password']);
unset($_POST['Submit']);
}

}

else{

//checkifpasswordcookieisset
if(!isset($_COOKIE['verify'])){
showLoginPasswordProtect("");
}

//checkifcookieisgood
$found=false;
foreach($LOGIN_INFORMATIONas$key=>$val){
$lp=(USE_USERNAME?$key:'').'%'.$val;
if($_COOKIE['verify']==md5($lp)){
$found=true;
//prolongtimeout
if(TIMEOUT_CHECK_ACTIVITY){
setcookie("verify",md5($lp),$timeout,'/');
}
break;
}
}
if(!$found){
showLoginPasswordProtect("");
}

}

?>

<b>Välkommen Kvasten^!</b>
<HR align=left color="black" size="1" width="230">
<br>
Du har inte fått något nytt MEDDELANDE :(
now. when i have logged in once (username: admin Password: adminpassword)
everytime i VISIT this page it is inlogged. on every computer. whats wrong? :SWhat you mean by inlogged? Have you implemented any log out features?

Are you setting cookies for sessions cause' if so that might be the problem of your description.Quote from: Carbon Dudeoxide on January 01, 2009, 05:49:45 AM

Check the date of the last post.....


Discussion

No Comment Found