So im getting a problem with my login script. My registration works fine and the details are stored properly in the database, but im getting a problem when i try to login using them. Basically i get the following error when i hit login:
The following error(s) occurred:
- The username and password entered do not match those on file.
-
Query: SELECT user_id, username FROM Users WHERE username='test' AND password=SHA('testing')
Please try again.
Below is my code, any help would be appreciated.
<?php # Script - login.php
if (isset($_POST['submitted'])) {
require_once ('../includes/mysql_connect.php');
$errors = array();
if (empty($_POST['username'])) {
$errors[] = 'You forgot to enter your username.';
} else {
$u = $_POST['username'];
}
if (empty($_POST['password'])) {
$errors[] = 'You forgot to enter your password.';
} else {
$p = $_POST['password'];
}
if (empty($errors)) {
$query = "SELECT user_id, username FROM Users WHERE username='$u' AND password=SHA('$p')";
$result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
if ($row) {
setcookie ('user_id', $row[0]);
setcookie ('username', $row[1]);
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1);
}
$url .= '/loggedin.php';
header("Location: $url");
exit();
} else {
$errors[] = 'The username and password entered do not match those on file.';
$errors[] = mysql_error() . '<br /><br />Query: ' . $query;
}
}
mysql_close();
} else {
$errors = NULL;
}
$page_title = 'Login';
include ('../includes/header.html');
if (!empty($errors)) {
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) {
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p>';
}
?>
<h2>Login</h2>
<form action="login.php" method="post">
<p>Username: <input type="text" name="username" size="20" maxlength="40" /> </p>
<p>Password: <input type="password" name="password" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Login" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('../includes/footer.html');
?>

New Topic/Question
Reply




MultiQuote




|