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.

Recommended Answers

All 6 Replies

It's running localhost right now in XAMPP. I'm still building it.

thats why. i used to have the same problem when i launched it in http://localhost/. Try launching it with the site name or ip address and this shouldnt happen

commented: I didn't understand immediately, but it fixed my problem. +0

I just tried it with 127.0.0.1 and the same thing happens.
Do I have to change me SQL log in script to 127.0.0.1 instead of localhost also?

No your dynamic or static ip address that is givin from your internet provider. ex.. 123.456.789.012

My home network is closed and I don't want to port forward anything. I also tried it from my static IP in my home network. that's set to 192.168.0.150.
I see what you mean though. I just tried it from my IPT and it worked right. Is there anyway I can run it on localhost and not have to double log in?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.