I have a page that I have protected using a simple PHP. It also emails me if an incorrect password is entered. Although the Password works as expected, I still get an email notification. It is as if this line of code isn't working as expected:
if(stristr($alert,"@")!==false)
I fully accept I haven't written or understood this code, but everyone has to start somewhere!
Please could you tell me where this is falling down, and how I can rectify the problem.
Thanks!
Code:
<?
session_start();
//--------------------------
// user definable variables:
//--------------------------
// maximum number of seconds user can remain idle without having to re-login:
// use a value of zero for no timeout
$max_session_time = 0;
// type of alert to give on incorrect password:
// $alert = ""; If no alert
$alert = "me@email.com";
// acceptable passwords:
// $cmp_pass = Array();
// $cmp_pass[] = md5("default");
// add as many as you like
$cmp_pass[] = "password";
// maximum number of bad logins before user locked out
// use a value of zero for no hammering protection
$max_attempts = 3;
// url to go to if ok
$formurl = " ;
//-----------------------------
// end user definable variables
//-----------------------------
// save session expiry time for later comparision
$session_expires = $_SESSION['mpass_session_expires'];
// have to do this otherwise max_attempts is actually one less than what you specify.
$max_attempts++;
if(!empty($_POST['mpass_pass']))
{
// store md5'ed password
$_SESSION['mpass_pass'] = md5($_POST['mpass_pass']);
}
if(empty($_SESSION['mpass_attempts']))
{
$_SESSION['mpass_attempts'] = 0;
}
// if the session has expired, or the password is incorrect, show login page:
if(($max_session_time>0 && !empty($session_expires) && mktime()>$session_expires) || empty($_SESSION['mpass_pass']) || !in_array($_SESSION['mpass_pass'],$cmp_pass))
{
if(!empty($alert) && !in_array($_SESSION['mpass_pass'],$cmp_pass))
{
// user has submitted incorrect password
// generate alert:
$_SESSION['mpass_attempts']++;
$alert_str = $_SERVER['REMOTE_ADDR']." entered ".htmlspecialchars($_POST['mpass_pass'])." on page ".$_SERVER['PHP_SELF']." on ".date("l dS of F Y h:i:s A")."\r\n";
if(stristr($alert,"@")!==false)
{
// email alert
@mail($alert,"Bad Login on ".$_SERVER['PHP_SELF'],$alert_str,"From: ".$alert);
} else {
// textfile alert
$handle = @fopen($alert,'a');
if($handle)
{
fwrite($handle,$alert_str);
fclose($handle);
}
}
}
// if hammering protection is enabled, lock user out if they've reached the maximum
if($max_attempts>1 && $_SESSION['mpass_attempts']>=$max_attempts)
{
exit("Too many login failures.");
}
// clear session expiry time
$_SESSION['mpass_session_expires'] = "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
WEBPAGE!
</body>
</html>
<?
// and exit
exit();
}
// if they've got this far, they've entered the correct password:
// reset attempts
$_SESSION['mpass_attempts'] = 0;
// update session expiry time
$_SESSION['mpass_session_expires'] = mktime()+$max_session_time;
// end password protection code
header( "Location: $formurl" );
exit;
?>
jimlad
"There's this thing called being so open-minded your brains drop out." - Richard Dawkins
if(stristr($alert,"@")!==false)
I fully accept I haven't written or understood this code, but everyone has to start somewhere!
Please could you tell me where this is falling down, and how I can rectify the problem.
Thanks!
Code:
<?
session_start();
//--------------------------
// user definable variables:
//--------------------------
// maximum number of seconds user can remain idle without having to re-login:
// use a value of zero for no timeout
$max_session_time = 0;
// type of alert to give on incorrect password:
// $alert = ""; If no alert
$alert = "me@email.com";
// acceptable passwords:
// $cmp_pass = Array();
// $cmp_pass[] = md5("default");
// add as many as you like
$cmp_pass[] = "password";
// maximum number of bad logins before user locked out
// use a value of zero for no hammering protection
$max_attempts = 3;
// url to go to if ok
$formurl = " ;
//-----------------------------
// end user definable variables
//-----------------------------
// save session expiry time for later comparision
$session_expires = $_SESSION['mpass_session_expires'];
// have to do this otherwise max_attempts is actually one less than what you specify.
$max_attempts++;
if(!empty($_POST['mpass_pass']))
{
// store md5'ed password
$_SESSION['mpass_pass'] = md5($_POST['mpass_pass']);
}
if(empty($_SESSION['mpass_attempts']))
{
$_SESSION['mpass_attempts'] = 0;
}
// if the session has expired, or the password is incorrect, show login page:
if(($max_session_time>0 && !empty($session_expires) && mktime()>$session_expires) || empty($_SESSION['mpass_pass']) || !in_array($_SESSION['mpass_pass'],$cmp_pass))
{
if(!empty($alert) && !in_array($_SESSION['mpass_pass'],$cmp_pass))
{
// user has submitted incorrect password
// generate alert:
$_SESSION['mpass_attempts']++;
$alert_str = $_SERVER['REMOTE_ADDR']." entered ".htmlspecialchars($_POST['mpass_pass'])." on page ".$_SERVER['PHP_SELF']." on ".date("l dS of F Y h:i:s A")."\r\n";
if(stristr($alert,"@")!==false)
{
// email alert
@mail($alert,"Bad Login on ".$_SERVER['PHP_SELF'],$alert_str,"From: ".$alert);
} else {
// textfile alert
$handle = @fopen($alert,'a');
if($handle)
{
fwrite($handle,$alert_str);
fclose($handle);
}
}
}
// if hammering protection is enabled, lock user out if they've reached the maximum
if($max_attempts>1 && $_SESSION['mpass_attempts']>=$max_attempts)
{
exit("Too many login failures.");
}
// clear session expiry time
$_SESSION['mpass_session_expires'] = "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
WEBPAGE!
</body>
</html>
<?
// and exit
exit();
}
// if they've got this far, they've entered the correct password:
// reset attempts
$_SESSION['mpass_attempts'] = 0;
// update session expiry time
$_SESSION['mpass_session_expires'] = mktime()+$max_session_time;
// end password protection code
header( "Location: $formurl" );
exit;
?>
jimlad
"There's this thing called being so open-minded your brains drop out." - Richard Dawkins