Jump to content


Photo

Can't Set a Cookie (Not a header problem <_<)


  • Please log in to reply
4 replies to this topic

#1 Anthop

Anthop
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 21 September 2006 - 11:53 PM

Though I've done a lot of desktop development, I'm new to PHP and to web-development in general, so please treat me kindly :D.  In any case, I have a login script that won't set a cookie.  Attempting to set the cookie returns a true on success (see $success), so I don't think it's a header issue.  However, checking isset($_COOKIE['User ID']) and just refreshing the page to see if I'm still logged on shows that there is no cookie to be found.  (I also checked my brower's list of cookies to no avail -_-;.)

In any case, here is my code.  I've deleted the non-cookie parts for quicker browsing (My code isn't really this sparse XD).  The cookie isn't set whether I run index.php or login.php directly.


This is index.php.
<?php ob_start() ?>
<html>
<head>
</head>
<body>
<?php // Here is a form for other input.?>
<br /><br />
<?php
	include 'login.php';
	ob_flush();
?>
</body>
</html>



This is login.php.
<?php
	// If user is logging out, remove cookie.
	if ($_POST['logout']) {
		setcookie("User ID", "", mktime(12,0,0,1,1,1990));
	}
	// If information submitted, and no cookie is found, check login information and set cookie.
	if ($_POST['submitted'] && !isset($_COOKIE['User ID'])) {
		// Connect to the User database using a public account.
	
		// Search to see if user name matches, and fetch password.
		
		// If password matches that given, set a cookie which expires in 12 hours.
		if($password == $row['Password']) {
			$success = setcookie('User ID', $row['User ID'], time()+43200, '/', '.light-within.org', 0);
			$new_session = 1;
		}
	}
?>
<html>
<head>
</head>
<body>
<?php
	// If not already logged in,
	if (!$_POST['submitted'] && !isset($_COOKIE['User ID'])) {
	
	// Display the Login Form (the target is PHP_SELF)
	
	// Otherwise, display welcome message.
	}else{
		// If this is a new login, display welcome message.
		if($new_session) {

		// If there is a cookie, retrieve user information.
		} else if (isset($_COOKIE['User ID'])) {

		// Otherwise, return invalid login message.
		} else {
			echo("<br><p>Screen Name and Password combination not valid.</p>");
		}
		
		// This checks to see if there is a cookie installed.  Success returns true,  but isset returns false.
		if(!$success || !isset($_COOKIE['User ID'])) {
			echo('NO COOKIE SET!! <br>');
		}
		// Close database connection.
	}
?>
</body>
</html>


#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 22 September 2006 - 12:30 AM

Change
$success = setcookie('User ID', $row['User ID'], time()+43200, '/', '.light-within.org', 0);
to
setcookie('User ID', $row['User ID'], time()+43200, '/', '.light-within.org', 0);

and see if it works
Why doesn't anyone ever say hi, hey, or whad up world?

#3 Anthop

Anthop
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 22 September 2006 - 12:54 AM

Nope.  It didn't make a difference -_-;.

#4 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 22 September 2006 - 06:07 AM

A shot in the dark here.. try giving your cookie a name with no spaces in it.

#5 Anthop

Anthop
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 22 September 2006 - 08:20 PM

O_O.... It worked!  Wow.  So I've learned that you have to set all the values of a cookie and not use any whitespace to make a cookie work in Firefox <_<;;.

Anyway, thanks for your help, btherl and corbin :D.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users