Jump to content

Refreshing To Display Cookie


fanfavorite

Recommended Posts

I am writing a login script and having some issues with Cookies.  If I go directly to the login page (login.php), it works great.  However if I try to go directly to a members page without being logged in (loggedin.php), when it brings up the login page and I submit the information, it will not get the Cookie because the page has not been reloaded.  So I figured my options are to directly load the login.php (header ("location: login.php")), which I can't because the headers are already done for the page or refresh the page somehow.  Any options would be greatly appreciated.  Thanks! 

 

LOGGEDIN.PHP

<?
   include ('membersverify.php');
   $User = $_COOKIE[login];
?>
<html>
...

 

LOGIN.PHP

<?

if ($_GET['logout'])
{
setcookie("login");
}
if ($_POST[loginaccount])
{

include ('membersconfig.php');
$q = mysql_query("select * from Login WHERE Username = '$_POST[username]';");
while($f=mysql_fetch_array($q))
{
$c = "$f[username]";
if ($_POST[password] == $f[Password] AND $_POST[password] != "")
{
setcookie("login",$c,false,"/",false);
header("Location: loggedin.php");
}
else
{
$error = "Invalid username and/or password.  Please try again.";
include ('membersloginform.php');
}
die;
   }
$error = "Invalid username and/or password.  Please try again.";
include ('membersloginform.php');
}
else
{
include ('membersloginform.php');
}
?>

 

MEMBERSVERIFY.PHP

if ($_COOKIE[login] != "")
{
$q = mysql_query("select * from Login WHERE Username = '$_COOKIE[login]';");
while($f=mysql_fetch_array($q))
{
if ($f[username] != $_COOKIE[login])
{
include ('login.php');
die;
}
}
} else {
include ('login.php');
die;
}

Link to comment
https://forums.phpfreaks.com/topic/47846-refreshing-to-display-cookie/
Share on other sites

I read a comment in the php manual:

 

Be careful of using the same cookie name in subdirectories. Setting a simple cookie

 

<?setcookie("region", $_GET['set_region']);?>

 

both in the root / and for instance in this case /admin/ will create 2 cookies with different paths. In reading the cookies back only the first one is read regardless of path.

 

 

I think this may be the problem with my script because it works if I put everything in the same directory. The login.php and membersverify.php pages are in a directory called login and the logged in page is just outside of that directory. When someone goes to the login page (login/login.php) sets the cookie in the login directory and when someone tries to go to a page outside the directory, the membersverify includes the login.php and sets the cookie, which is outside the directory. This is a different path.

 

Now my question is, how do I fix this so that I can have files in different directories? Is there a way to make sure that it overwrites and reads the same login cookie everytime?  I thought thats what the "/" did in my setcookie script. 

 

Thanks,

 

-JC

 

 

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.