FalseProphet Posted March 21, 2011 Share Posted March 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231244-php-login-system-driving-me-bonkers/ Share on other sites More sharing options...
lastkarrde Posted March 21, 2011 Share Posted March 21, 2011 We need more information. Any errors? What works that should? What works that shouldn't? Quote Link to comment https://forums.phpfreaks.com/topic/231244-php-login-system-driving-me-bonkers/#findComment-1190187 Share on other sites More sharing options...
rascle Posted March 21, 2011 Share Posted March 21, 2011 Stupid question but have you checked that: echo '<form method="post" action="system/scripts/apnetwork_login.php">'; it is the right directory?? Quote Link to comment https://forums.phpfreaks.com/topic/231244-php-login-system-driving-me-bonkers/#findComment-1190191 Share on other sites More sharing options...
FalseProphet Posted March 21, 2011 Author Share Posted March 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231244-php-login-system-driving-me-bonkers/#findComment-1190199 Share on other sites More sharing options...
phil88 Posted March 21, 2011 Share Posted March 21, 2011 Are the cookies being set correctly? Put a var_dump($_COOKIES); in your index.php file and run through the process to see Quote Link to comment https://forums.phpfreaks.com/topic/231244-php-login-system-driving-me-bonkers/#findComment-1190221 Share on other sites More sharing options...
FalseProphet Posted March 21, 2011 Author Share Posted March 21, 2011 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: Quote Link to comment https://forums.phpfreaks.com/topic/231244-php-login-system-driving-me-bonkers/#findComment-1190224 Share on other sites More sharing options...
PFMaBiSmAd Posted March 21, 2011 Share Posted March 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231244-php-login-system-driving-me-bonkers/#findComment-1190227 Share on other sites More sharing options...
FalseProphet Posted March 21, 2011 Author Share Posted March 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231244-php-login-system-driving-me-bonkers/#findComment-1190235 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.