PHP Basic Login Problems
Posted: Fri Mar 05, 2010 9:38 am
Hi, I'm having some difficulties in getting a basic php login system to work. I'm new to php but have read tutorials and books on the topic. I'm sure the code i have is close, just down to one error now that's really bugging me!
It's producing the "Sorry, could not log you in. Wrong login information." error when i enter a username and password that exist in the 'user' table of the database. It seems as if the query isn't returning anything....
Any suggestions appreciated
thanks
Code: Select all
<?php
include('db_login.php');
$con = mysql_connect($db_host, $db_username, $db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ccn", $con);
if (isset($_GET["op"])) {
if ($_GET["op"] == "login")
{
if (!$_POST["username"] || !$_POST["password"])
{
die("You need to provide a username and password.");
}
// Create query
$q = "SELECT * FROM `user` "
."WHERE `username`='".$_POST["username"]."' "
."AND `password`=PASSWORD('".$_POST["password"]."') "
."LIMIT 1";
// Run query
echo $q."<br>";
$r = mysql_query($q);
if ( $obj = @mysql_fetch_object($r) )
{
// Login good, create session variables
$_SESSION["valid_id"] = $obj->id;
$_SESSION["valid_user"] = $_POST["username"];
$_SESSION["valid_time"] = time();
// Redirect to member page
Header("Location: http://localhost/members.php");
}
else
{
// Login not successful
die("Sorry, could not log you in. Wrong login information.");
}
} // op isn't login
} // op isn't set
else
{
//If all went right the Web form appears and users can log in
echo "<form action=\"?op=login\" method=\"POST\">";
echo "Username: <input name=\"username\" size=\"15\"><br />";
echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
echo "<input type=\"submit\" value=\"Login\">";
echo "</form>";
}
?>Any suggestions appreciated
thanks