The Problem:
User cannot login at all, even with the right user/pass.
The PHP:
<?php
session_start();
header("Cache-control: private");
include('required/config.php');
include('required/language.php');
include('required/functions.php');
//Build required variables
$username = $_POST['username'];
$password = $_POST['password'];
$MD5 = md5($password);
//Check and see if a session exists
if($_COOKIE['loggedin'] == 'yes' && isset($_COOKIE['userid']))
{
//Attempt to login user
$sql = "SELECT * FROM users WHERE userid = '" . $_COOKIE['userid'] . "'";
$query = mysql_query($sql) or die(sql_error());
$row = mysql_fetch_array($query);
//If database finds a match
if (mysql_num_rows($query) == 1)
{
//If user hasn't been activated, turn 'em away!
if($row['status'] == 1)
{
echo $login['not_activated'];
exit();
}
//If they have been activated, welcome them with open webpages!
else
{
//Create session variables and set cookies
setcookie("loggedin", "yes", time()+604800, '', $cookie_domain);
setcookie("userid", $_COOKIE['userid'], time()+604800, '', $cookie_domain);
$_SESSION['type'] = $row['type'];
$_SESSION['first_name'] = $row['first_name'];
header('Location: ' . $full_domain . 'index.php');
exit();
}
}
}
elseif ($_POST['submit'] == 'Login')
{
//If the username or password isn't set...
if(!$username || !$password)
{
echo $loginMISSING;
include('required/login_form.php');
}
else
{
//Attempt to login...
$sql = "SELECT username, password FROM users WHERE username = '$username' AND password = '$MD5'";
$query = mysql_query($sql) or die(sql_error());
$row = mysql_fetch_array($query);
//If database finds a match...
if (mysql_num_rows($query) == 1)
{
//If user hasn't been activated, turn 'em away!
if($row['status'] == 1)
{
echo $login['not_activated'];
exit();
}
//If they have been activated, welcome them with open webpages!
else
{
//Create session variables and set cookies
$_SESSION['userid'] = $row['userid'];
$_SESSION['type'] = $row['type'];
$_SESSION['first_name'] = $row['first_name'];
//If they want the website to remember their login information, set session data to last for one week
if($_POST['remember'])
{
setcookie("loggedin", "yes", time()+604800, '', $cookie_domain);
setcookie("userid", $_SESSION['userid'], time()+604800, '', $cookie_domain);
}
//Send the user to the index.php page
header('Location: ' . $full_domain . 'index.php');
exit();
}
}
//If the database couldn't find a match...
else
{
echo $login['denied'];
include('required/login_form.php');
exit();
}
}
}
//Check and see if the user is ALREADY logged in
elseif(isset($_SESSION['userid']) && isset($_SESSION['type']) && isset($_SESSION['first_name']))
{
header('Location: index.php');
}
else
{
echo $login['first'];
include('required/login_form.php');
}
?>
The HTML:
<form action="<?php $PHP_SELF; ?>" method="post" name="login_form">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr align="left" valign="top">
<td>Username:</td>
<td><input name="username" type="text" value="<?php echo $username; ?>" size="20"></td>
</tr>
<tr align="left" valign="top">
<td>Password:</td>
<td><input name="password" type="password" size="20"></td>
</tr>
<tr align="left" valign="top">
<td>Remember Info?</td>
<td><input name="remember" type="checkbox" value="remember"></td>
</tr>
<tr align="left" valign="top">
<td colspan="2"><input name="submit" type="submit" value="Login"></td>
</tr>
</table>
</form>
The solution:
Please tell me 🙂