I'm working on a basic CMS for a club at my school. But I'm having trouble with the log in. The log in works but after entering the user name and password and submitting it, the form comes back up and you have to do it a second time before it lets you into the site.
Its important to mention that this log in page is included into other pages using require_once.
The SQL DB connects in a different file so that's not a problem as it is working or I couldn't log i at all. Here's the log in code.
<?php
if(!function_exists('showLoginPasswordProtect')) {
// show login form
function showLoginPasswordProtect($error_msg) {
include("$RP/skin/head.php");
?>
<!--Page Start-->
<div id="main">
<div id="Mtext">
<div style="width:500px; margin-left:auto; margin-right:auto; text-align:center">
<form method="post">
<br>
<font color="red" Size="6"><?php echo $error_msg; ?></font><br/>
<br/>
Username:<input type="text" name="UN" maxlength="34" value="<?php echo $_POST['UN'];?>"/>
<br/>
<br/>
Password:
<input type="password" name="PW" maxlength="50"/><br><br>
<input type="submit" name="Submit" value="Login" />
</form>
</div>
</body>
</html>
</div>
</div>
<!--Page End-->
<?php
include_once("$RP/sql-close.php");
include("$RP/skin/foot.php");
// stop at this point
die();
};
};
//Check Username in DB
if (isset($_POST['UN'])) {
if (isset($_POST['PW'])) {
$UN2 = $_POST['UN'];
$PW2 = md5($_POST['PW']);
$sql2 = "SELECT * FROM `users` WHERE `username` = '".$UN2."'";
$query2 = mysql_query($sql2) or die(mysql_error());
$result2 = mysql_fetch_assoc($query2);
if ($result2['password'] !== $PW2) {
showLoginPasswordProtect("Wrong Password!");
}
//Set Cookies
if ($result2['password'] == $PW2) {
setcookie("name", $UN2, time()+300);
setcookie("PW", $PW2, time()+300);
};
};
};
//Check for blanks
if (isset($_POST['UN'])) {
if ($_POST['UN'] == '') {
showLoginPasswordProtect("No Username!");
};
};
if (isset($_POST['PW'])) {
if ($_POST['PW'] == '') {
showLoginPasswordProtect("No Password!");
};
};
// Check if set and if correct.
if (isset($_COOKIE['name'])) {
if (isset($_COOKIE['PW'])) {
$UN1 = $_COOKIE['name'];
$PW1 = $_COOKIE['PW'];
$sql1 = "SELECT * FROM `users` WHERE `username` = '".$UN1."'";
$query1 = mysql_query($sql1) or die(mysql_error());
$result1 = mysql_fetch_assoc($query1);
if ($UN1 != $result1['username']) {
if ($PW1 != $result1['password']) {
showLoginPasswordProtect("");
};
showLoginPasswordProtect("");
};
} else {
showLoginPasswordProtect("");
};
} else {
showLoginPasswordProtect("");
};
//Show form if no cookies
if (!isset($_COOKIE['name'])) {
showLoginPasswordProtect("");
};
if (!isset($_COOKIE['PW'])) {
showLoginPasswordProtect("");
};
//Set use veriable
$_SESSION['ID'] = $result1['ID'];
?>
So I have absolutely no idea why it makes me login twice. Can anyone tell me?
Thanks for any help.