kalm1234 Posted December 9, 2008 Share Posted December 9, 2008 I have been working on a site for a few days, just to test out all my PHP scripts as I am new at PHP and have been loving it so far. I have run into a problem with using cookies to make a login system, however. The user logs in, and I can verify that the cookie is sent to the computer because I see it in the cookies folder. For some reason, however, when I do a conditional statement if the cookie exists, it reports it never is. The following page is the page that sets the cookie when the user logs in. The user is redirected to this page from a form in which they type their username and password. <?php $name = $_POST['username']; $password = $_POST['password']; if (file_exists($name.".txt")) { $myFile = $name.".txt"; $fh = fopen($myFile, 'r+'); $theData = fread($fh, filesize($myFile)); fclose($fh); if ($password == $theData) { setcookie('loggedin', $name, time()+86400); if(isset($_COOKIE['loggedin'])) { echo "<font size =1>You are logged in as<b> ".$_COOKIE['loggedin']."</b></font><p>"; } else { echo "<font size=1>You are not logged in</font><p>"; } echo "<html><head></head><body bgcolor=yellow> <center><h2>Welcome, ".$name."!<p></h2><h3>You have sucessfully logged in!</h3><p>Click<a href='../index.php'> here </a>to go back to the home page.</center> or <a href='../checklogin.php'>here</a> to check your status."; } else { echo "<html><head></head><body bgcolor=blue> The username or password you entered is invalid. Please <a href='../login.php'>try again</a>"; } } else { echo "<html><head></head><body bgcolor=blue> The username or password you entered is invalid. Please <a href='../login.php'>try again</a>"; } ?> </body> </html> The following page is a testing page I use to see if the cookie was successfully added to the user's computer. <?php if(isset($_COOKIE['loggedin'])) { $user = $_COOKIE['loggedin']; echo "You are logged in as ".$user; } else { echo 'You are <b><u>not</u></b> logged in!'; } ?> Can someone help me with my problem? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/136211-php-cookies-help/ Share on other sites More sharing options...
revraz Posted December 9, 2008 Share Posted December 9, 2008 Use the domain parameter when you set the cookie. Link to comment https://forums.phpfreaks.com/topic/136211-php-cookies-help/#findComment-710502 Share on other sites More sharing options...
kalm1234 Posted December 9, 2008 Author Share Posted December 9, 2008 Thanks for the reply. I have tried changing the parameters to: setcookie('loggedin', $name, time()+86400, '/', '.mywebsite.com' ); The results were the same. Link to comment https://forums.phpfreaks.com/topic/136211-php-cookies-help/#findComment-710516 Share on other sites More sharing options...
PFMaBiSmAd Posted December 9, 2008 Share Posted December 9, 2008 In the first piece of code, if(isset($_COOKIE['loggedin'])) will be false at least the first time you enter the password because the $_COOKIE variable is not set until the browser requests the page after the setcookie() has been executed. If the cookie has been set and you go to that page a second time and the password logic is true, then the isset(...) will be true. Since you have probably been experimenting, it is possible that the cookie is messed up, especially if you have changed the setcookie() parameters. Delete the cookie in your browser and try again. Link to comment https://forums.phpfreaks.com/topic/136211-php-cookies-help/#findComment-710529 Share on other sites More sharing options...
kalm1234 Posted December 10, 2008 Author Share Posted December 10, 2008 I have deleted all cookies associated with my browser, played with the setcookie's parameters, and refreshed the page many times. The conditional statement on the second set of code still returns false and shows that I am not logged in. Not sure what is wrong. Link to comment https://forums.phpfreaks.com/topic/136211-php-cookies-help/#findComment-711375 Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2008 Share Posted December 10, 2008 Just because you can see the cookie file that the browser creates, does not mean that the browser will send the cookie to the web server. Is your browser configured to only send cookies for specific sites or zones? Cookies are domain name specific and the usage of the setcookie() parameters can cause it to be hostname/subdomain specific and path specific. Are you using the same domain for the page that is setting the cookie and the second piece of code to test the cookie? What are the two URL's that you are using and what is your current code? Add the following two lines immediately after your first <?php tag in both pages - ini_set ("display_errors", "1"); error_reporting(E_ALL); Basically, what you are doing works, so there must be something specific that is occurring on your server or browser that is preventing it from working. Link to comment https://forums.phpfreaks.com/topic/136211-php-cookies-help/#findComment-711380 Share on other sites More sharing options...
kalm1234 Posted December 10, 2008 Author Share Posted December 10, 2008 I did some looking around and it just so happens that the browser was not allowing cookies to be sent for some reason. I went on another browser on a different network and it worked great. I thought it was something wrong with my code. Thanks a lot guys! Link to comment https://forums.phpfreaks.com/topic/136211-php-cookies-help/#findComment-711414 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.