There were a couple of things in your script that I havent seen before.
I have have seen many php codes ....
So, I have learned a couple of new things 🙂
There were like 2 serious things, that would make your script not work.
Like for example
if ($_POST['site_KEY'] != $result1)
{die('Incorrect password, please try again.');}
The posted KEY should not be compared to one md5KEY in database.
The second point I would tell you is
$hour = time() + 3600;
setcookie('site_ID', $_POST['site_ID'], $hour);
setcookie('site_KEY', $_POST['site_KEY'], $hour);
We never put posted PASSWORD in COOKIE.
This way it is easy for anyone to read and get hold of it.
Put the md5 PASWORD there. It is useless to try to login with.
I have changed a lot in your script.
But I have used your nice code. It is a good work you have done.
The Script now:
1. check for SESSION, if already logged in
2. check for COOKIE to log in with
3. check the POSTED for to log in with
I can not promise it will work now. Because I have not tried it.
But there should be a very good chance!
If there is something, we will see, as you have error_reporting activated.
Very good.
<?php // login script, o0110o, suggestion by halojoy phpbuilder
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once"../includes/connect_db.php";
// --------------------------------------------------------------------------------------------------
if(isset($_SESSION['site_ID'] , $_SESSION['site_KEY']))
{ // you are already logged in
header("location: index.php");
exit();
}
// --------------------------------------------------------------------------------------------------
//Checks if there is a login cookie
if(isset($_COOKIE['site_ID'] , $_COOKIE['site_KEY']))
{
//if there is, it logs you in and directes you to index.php
$sql = "SELECT TRUE FROM admin WHERE username='$site_ID' and password='$site_KEY'";
$result =mysql_result($sql) or die(mysql_error());
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
if($count==1)
{
// Register $site_ID, $site_KEY and redirect to file "index.php"
session_start();
$_SESSION['site_ID'] = $_COOKIE['site_ID'];
$_SESSION['site_KEY'] = $_COOKIE['site_KEY'];
header("location: index.php");
exit();
}
else
{
// Bad cookie, user not found, Delete cookie maybe???
}
}
// --------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------
// If form has been submitted
if (isset($_POST['site_ID'] , $_POST['site_KEY']))
{
// username and password sent from form
$site_ID = (isset($_POST['site_ID']) ? trim($_POST['site_ID']) : '');
$site_KEY = (isset($_POST['site_KEY']) ? trim($_POST['site_KEY']) : '');
$md5_KEY = md5($site_KEY);
// makes sure they filled it in
if(!$site_ID || !$site_KEY)
{die('You did not fill in a required field.');}
//gives error if the password is wrong
$sql1 = "SELECT TRUE FROM admin
WHERE username='$site_ID' password='$md5_KEY'";
$result1 =mysql_result($sql1) or die(mysql_error());
$count1 = mysql_num_rows($result1);
//Gives error if user dosen't exist
if($count1 == 0)
{die('That user does not exist in our database.');}
// If result matched $site_ID and $site_KEY, table row must be 1 row
if($count1 == 1)
{
// if login is ok then we add a cookie
$hour = time() + 3600;
setcookie('site_ID', $site_ID, $hour);
setcookie('site_KEY', $md5_KEY, $hour);
// Register $site_ID, $site_KEY and redirect to file "index.php"
session_start();
$_SESSION['site_ID'] = $site_ID;
$_SESSION['site_KEY'] = $md5_KEY;
// you are now logged in
header("location: index.php");
exit();
}
else
{
// $count1 >=2 (two or more admins in table) or less $count1 < 0
}
}
else
{
?>
<table width='300' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#CCCCCC'>
<tr>
<form name='form1' method='post' action="<?php echo $_SERVER['PHP_SELF']?>">
<td>
<table width='100%' border='0' cellpadding='3' cellspacing='1' bgcolor='#FFFFFF'>
<tr>
<td colspan='3'><strong>Login </strong></td>
</tr>
<tr>
<td width='78'>Username</td>
<td width='6'>:</td>
<td width='294'><input name='site_ID' type='text' id='site_ID' maxlength='40'></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name='site_KEY' type='password' id='site_KEY' maxlength='50'></td>
</tr>
<tr>
<td><input type='submit' name='Submit' value='Login'></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
}
?>