Hi I am quite new to PHP. All of today I have been working on a PHP user login script. I got it working fine, then I added some HTML to make it look better. The page shows up, but when i write my username and password in it just refreshes the screen. Can anyone help me. The code should be below. If you need the whole project, say.
<?php
include("conf.inc.php"); // Includes the db and form info.
session_start(); // Starts the session.
if ($_SESSION['logged'] == 1) { // User is already logged in.
header("Location: index.php"); // Goes to main page.
exit(); // Stops the rest of the script.
} else {
if (!isset($_POST['submit'])) { // The form has not been submitted.
echo "<html><body bgcolor='#f1f1f1' text='#000000' link='#33cc00'>
<table align='center' width='40%'>
<br><br><td><font face=\"arial\" size=\"2\">Log in</font></td>
</table>
<table align='center' width='40%' bgcolor='#FFFFFF' style='borger-color:#999999;border-style:solid;border-width:1px'>
<tr>
<td>
<br>
<center>
<form action=\"login.php\" method=\"POST\">";
echo "
<input name=\"username\" type=\"text\" size=\"25\" /><br>
<input name=\"password\" type=\"password\" size=\"25\" /><br>
<input type=\"submit\" value=\"Submit\" />
</form>
<br></font>
</center>
</td>
</tr>
</table>
</body>
</html>
";
} else {
$username = form($_POST['username']);
$password = md5($_POST['password']); // Encrypts the password.
$q = mysql_query("SELECT * FROM `users` WHERE username = '$username' AND password = '$password'") or die (mysql_error()); // mySQL query
$r = mysql_num_rows($q); // Checks to see if anything is in the db.
if ($r == 1) { // There is something in the db. The username/password match up.
$_SESSION['logged'] = 1; // Sets the session.
header("Location: index.php"); // Goes to main page.
exit(); // Stops the rest of the script.
} else { // Invalid username/password.
exit("Incorrect username/password!"); // Stops the script with an error message.
}
}
}
mysql_close($db_connect); // Closes the connection.
?>
Here is the working code.
<?php
include("conf.inc.php"); // Includes the db and form info.
session_start(); // Starts the session.
if ($_SESSION['logged'] == 1) { // User is already logged in.
header("Location: index.php"); // Goes to main page.
exit(); // Stops the rest of the script.
} else {
if (!isset($_POST['submit'])) { // The form has not been submitted.
echo "<form action=\"login.php\" method=\"POST\">";
echo "<table>";
echo "<tr>";
echo "<td colspan=\"2\">Login:</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"50%\">Username:</td><td width=\"50%\"><input name=\"username\" size=\"18\" type=\"text\" />";
echo "</tr>";
echo "<tr>";
echo "<td width=\"50%\">Password:</td><td width=\"50%\"><input name=\"password\" size=\"18\" type=\"text\" />";
echo "</tr>";
echo "<tr>";
echo "<td colspan=\"2\"><input type=\"submit\" name=\"submit\" value=\"submit\"</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
} else {
$username = form($_POST['username']);
$password = md5($_POST['password']); // Encrypts the password.
$q = mysql_query("SELECT * FROM `users` WHERE username = '$username' AND password = '$password'") or die (mysql_error()); // mySQL query
$r = mysql_num_rows($q); // Checks to see if anything is in the db.
if ($r == 1) { // There is something in the db. The username/password match up.
$_SESSION['logged'] = 1; // Sets the session.
header("Location: index.php"); // Goes to main page.
exit(); // Stops the rest of the script.
} else { // Invalid username/password.
exit("Incorrect username/password!"); // Stops the script with an error message.
}
}
}
mysql_close($db_connect); // Closes the connection.
?>
Sorry, it is quite messy :)