Jump to content

PHP Login system driving me bonkers...


FalseProphet

Recommended Posts

I wrote a simple login system to try out new things with. It works super great until I pull it out of my temporary testing directory and onto the real site.

 

My website structure:

[+] mywebsite.com
- index.php
[+] system
	[+] scripts
		- apnetwork_login.php
[+] mydatabase
[+] database
	- admin.log

 

index.php

<?PHP

if ($_COOKIE['apn_clientname'] != "") {
	$apnetLogin = $_COOKIE['apn_clientname'];
	$apnetPassword = $_COOKIE['apn_clientpass'];
}

function LoginFunction($apnetLogin,$apnetPassword) {
	// TODO: ereg express apnetpassword & apnetlogin
	$filePath = "../mydatabase/database/";
	$fileFullPath = $filePath . $apnetLogin . ".log";
	if (file_exists($fileFullPath) == TRUE) {
		$file = file($fileFullPath);

		if ($file[0] == $apnetPassword."\n") {
			// Login successful
			$clientLoggedIn = TRUE;
		}
		else {
			// Login unsuccessful - incorrect password
			echo "Unable to log you in, $apnetLogin";
			$clientLoggedIn = FALSE;
		}
	}

	if ($clientLoggedIn == TRUE) { // Logged in successfully
		echo "<font color=#ff0000>Welcome to the site,</font> <font color=#dd1111>$apnetLogin!</font>";
	}
	else { // Not logged in
		echo '<form method="post" action="system/scripts/apnetwork_login.php">';
		echo '<input type="submit" class="csubmit" value="Login"/>';
		echo '<input type="password" name="apn_password" class="pinput" value="Password"/>';
		echo '<input type="text" name="apn_name" class="linput" value="Username"/>';
		echo '</form>';
	}
}
?>

<html>
<head>
</head>

<body>
	<div>
		<?PHP LoginFunction($apnetLogin,$apnetPassword); ?>
	</div>
</body>
</html>

 

APNetwork_Login.php

<?PHP
$apnetLogin = $_POST['apn_name'];
$apnetPassword = $_POST['apn_password'];

if (setcookie("apn_clientname",$apnetLogin)) {
	if (setcookie("apn_clientpass",$apnetPassword)) {
		header('location:http://mywebsite.com/index.php');
	}
} else {
	echo "Error logging in.";
}

?>

 

Any idea's? I am stumped. Like I said, it works perfectly if I have it in the original folder that I tested the code on. But anywhere else it just don't work.  :shrug:

Link to comment
Share on other sites

I don't get any errors at all and apnetwork_login.php is in the right directory.

 

When I try to log in, the browser just refreshes and I am still presented with the login form, which should not occur.

 

In my temporary directory it worked fine, I would see "Welcome to the site, name" whenever I logged into the test account.

Link to comment
Share on other sites

var_dump doesn't print anything, but it's strange. If I comment the header out of apnetwork_login.php I can see that everything post correctly, and I can view cookie data there.

 

[edit] Here is some debug data... Okay, so cookies are NOT getting set. But why? My apnetwork_login.php script shouldn't contain any errors with the cookie headers?

 

The debug data below was taken directly from apnetwork_login.php

Cookie Login:

Cookie Password:

POST Login: admin

POST Password: admin

NULL var_dump login data:

Link to comment
Share on other sites

Your setcooke() statement in APNetwork_Login.php is not using the 4th parameter (the path the cookie matches), so the cookies only match the system/scripts/ path and the browser won't send the cookies to the server for pages in any other folder.

 

You would want to set the 4th parameter to '/' so that it will match all paths.

 

All of this information can be found in the setcooke() section of the php.net documentation.

Link to comment
Share on other sites

Your setcooke() statement in APNetwork_Login.php is not using the 4th parameter (the path the cookie matches), so the cookies only match the system/scripts/ path and the browser won't send the cookies to the server for pages in any other folder.

 

You would want to set the 4th parameter to '/' so that it will match all paths.

 

All of this information can be found in the setcooke() section of the php.net documentation.

 

That did it! Thank you so much, this freakin' thing has been bugging me for two days straight now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.