It is only basic at the minute - I am planning to add features such as last login time, last IP, browser.
But for now I would like you guys to look at my code and see if I have made any mistakes, or if there is anything you could improve, Security or otherwise.
<h3>Login</h3>
<form method="POST">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br /><br />
<?php
//set vars
$dbhost = "host";
$dbuser = "username";
$dbpass = "password";
$dbtable = "table";
$username = addslashes(strip_tags(strtolower($_POST['username'])));
$password = addslashes(strip_tags($_POST['password']));;
$encpassword = sha1($password);
//if login button is pushed
if ($_POST['login'])
{
//if $username and $encpassword contain values continue
if ($username&&$password)
{
//connect to the database
$connect = @mysql_connect("$dbhost","$dbuser","$dbpass") or die("Error connecting to server, please try again later.");
@mysql_select_db("$dbtable") or die("Error connecting to server, please try again later.");
//find all users with the username matching $username_clean
$query = mysql_query("SELECT * FROM users WHERE username ='$username'");
//count the usernames matching $username
$numrows = mysql_num_rows($query);
//if any matches are found
if ($numrows!=0)
{
//put username and password from database into varables
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username_clean'];
$dbpassword = $row['password'];
}
//if both $username and $password match the database values then login
if (($username==$dbusername)&&($encpassword==$dbpassword))
{
//set session variable
$_SESSION['username']=$username;
//redirect the user, and make sure script ends
header("Location: index.php");
exit();
}
//password dosn't match
else
{
echo "Incorrect login details, please try again.<br /><br />";
}
}
//if username isnt found
else
{
echo "Incorrect login details, please try again.<br /><br />";
}
//end ($username&&$password) if statement
}
//if $username and $encpassword does not contain values continue
else
{
echo "Please fill in both fields.<br /><br />";
}
}
?>
<input type="submit" name="login" value="Login!" />
This post has been edited by KingCuddles: 03 January 2010 - 10:08 AM

New Topic/Question
Reply



MultiQuote



|