Jump to content

PHP Cookies Help


kalm1234

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.