This is my main login page, it does not work, when i login in it always echos invalid username / password.
when i check my database the user name is there and so is the password, however the password is encrypted, could this be the problem? if so how can i fix it please help
<?php
session_start();
// clear session for testing only
if (!empty($_GET['i'])) unset($_SESSION['username']);
// register redirect
if (!empty($_GET['return'])) $_SESSION['return'] = $_GET['return'];
if (isset($_SESSION['username'])) {
echo 'already logged in as: '.$_SESSION['username']."<br />";
echo '<a href="login.php?i=clear">clear session variable</a>';
exit();
} else {
if (!empty($_POST)) {
// get values from the form and check with database
$t_name = $_POST['name'];
$t_password = $_POST['password'];
//if (($_POST['name'] == $t_name) and ($_POST['password'] == $t_password)) {
$connect = mysql_connect("localhost", "root", "")
or die ("Hey loser, check your server connection.");
mysql_select_db("test");
// query the database to see if there is a record which matches
$query = "select count(*) from user where
username = '$t_name' and
password = '$t_password'";
$results = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($results);
extract($row);
$count = $row[0];
if ( $count > 0 ){
// create session variable as username and password match
$_SESSION['username'] = $_POST['name'];
if (!empty($_SESSION['return'])) header('Location: '.$_SESSION['return']);
echo '<a href successful login>';
} else {
echo 'invalid login, try again';
}
}
}
if(!isset($_POST['name'])&&!isset($_POST['password']))
{
//Visitor needs to enter a name and password
?>
<h1>Please Log In</h1>
<form method="post" action="login.php">
<table border="1">
<tr>
<th> Username </th>
<td> <input type="text" name="name"> </td>
</tr>
<tr>
<th> Password </th>
<td> <input name="password" type="password" maxlength="255"> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Log In">
</td>
</tr>
</table>
<p>If you are not a registered please <a href="register.php">click here</a> </p>
</form>
<?php
}