[RESOLVED] Setting cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

LuiePL
Forum Commoner
Posts: 40
Joined: Fri Aug 04, 2006 11:38 pm

Post by LuiePL »

OK I switched it to that now getting:

Cookie Set. Expires: 12/31/1969 8:00:00 PM
Time now: 08/06/2006 8:44:15 PM

And it's still saying that the cookie was not set in the body. Do I need to refresh the page in the code once it "sets" the cookie?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Doink. $expiry = time() + 3600
LuiePL
Forum Commoner
Posts: 40
Joined: Fri Aug 04, 2006 11:38 pm

Post by LuiePL »

LOL, even I should have picked up on that. :roll: :lol: Working now! Well, not the cookies, but the time is.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What do you expect the post var 'set' to contain?
LuiePL
Forum Commoner
Posts: 40
Joined: Fri Aug 04, 2006 11:38 pm

Post by LuiePL »

Everah wrote:What do you expect the post var 'set' to contain?
It's a boolean check box for if the user wants to stay logged in.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Bear in mind that checkboxes pass as either 'On' or empty. If it is empty, checking the value it in a compariosn or assigning it will throw an undefined index notice.
LuiePL
Forum Commoner
Posts: 40
Joined: Fri Aug 04, 2006 11:38 pm

Post by LuiePL »

I'm not too sure if it is setting it correctly though. My administration page is setup to read the cookie with the following code, and it's keeps telling me that I'm not authorized. So that leads me to believe it's not setting properly.

Code: Select all

if (isset($_COOKIE['user']))
	{
		$cookie_info = explode("-", $_COOKIE['user']);
		$name = $cookie_info[0];
		$pass = $cookie_info[1];
		$dbh = mysql_connect ("localhost", "name", "password") or die ('Database Connection Error: ' . mysql_error());
		mysql_select_db ("database");
		$query = "SELECT * FROM Login WHERE name='$name' AND password='$pass'"; 
			$sql = mysql_query($query); //or die(mysql_error()); 
			$count = mysql_num_rows($sql);
		$queryadmin = "SELECT * FROM Login WHERE name='$name' AND admin='-1'";
			$sqladmin = mysql_query($queryadmin);
			$countadmin = mysql_num_rows($sqladmin);

		if ($countadmin == 1)
			{
				echo "<center><span class='RED'><h1>Administration Panel</h1></span>";
				echo "<BR><a href='../MembersOnly.php'>Members Only</a>";
				echo "<BR><a href='../Logout.php'>Logout</a></center>";
			}
	}
else
	{
		echo "<CENTER><SPAN CLASS='RED'>You are not an administrator</SPAN>";
		echo "<BR><A HREF='../index.php'>Home</a></CENTER>";
	}
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Can you repost the entire index.php please?
LuiePL
Forum Commoner
Posts: 40
Joined: Fri Aug 04, 2006 11:38 pm

Post by LuiePL »

Either of you know why this would work in IE6 but not Firefox 1.5? I just did a quick check and got it working in IE without a problem. If that's the case, I'd rather work on a firefox problem than a entire page problem.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, that explains everything!

Yeah, Firefox is rejecting cookies from the website.

Tools > Options > Privacy > Cookies

Noodle with those options. Make sure your development domain isn't blacklisted.
LuiePL
Forum Commoner
Posts: 40
Joined: Fri Aug 04, 2006 11:38 pm

Post by LuiePL »

I tried tinkinering with that, including explicity allowing the domain with and without the "www." Still won't take it though...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you have the web developer extension, view the response headers from this page. It may be telling.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Let's check the obvious: you do have "Allow sites to set cookies" checked right? You can try removing all sites and see if that helps. Try clearing all cookies too.
LuiePL
Forum Commoner
Posts: 40
Joined: Fri Aug 04, 2006 11:38 pm

Post by LuiePL »

OK, cleared the cookies and got it work. I also had to change one part in my body code. It was trying to act like it was posted to if the cookie was set, but I switched the following code:

Code: Select all

$name = $_POST['name'];
$pass = md5($_POST['pass']);
To:

Code: Select all

$cookie_info = explode("-", $_COOKIE['user']);
$name = $cookie_info[0];
$pass = $cookie_info[1];
So it authenticates you properly if you go to the main page and the cookie is still set. But besides that, it looks likes it's functioning corrctly at this point, I got to tinker with the logout page so it clears it properly, but step 1 is complete. Thanks.
LuiePL
Forum Commoner
Posts: 40
Joined: Fri Aug 04, 2006 11:38 pm

Post by LuiePL »

I don't know what I'm doing wrong, but I thought clearing cookies was easier. This is my code at the top:

Code: Select all

if (isset($_COOKIE['user']))
	{
		setcookie ("user", "", time() - 3600);
	}
Post Reply