• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

PHP; Set Cookies Not Working

Having trouble setting cookies in different browsers.

The following snippet sets cookies in Firefox for the PC and for the Mac.
It does not set them in Safari or in IE7.
Safari does, however, respond to session_start() with a blank PHPSESSID.

Code is:
<?php session_start();
require_once('../Connections/conn_333mysql.php'); ?>
<?php
if (! isset($_COOKIE["selectfc"]))
{$rand = rand(10000,99999);
$expyr = 60 * 60 * 24 * 365 + time();
setcookie("selectfc", $rand, $expyr, "/", " ",0 );
}
?>

Could somebody help with our errors please?
0
smudgemouse
Asked:
smudgemouse
  • 2
1 Solution
 
obareeyCommented:
first of all, you do not need session_start() for creating or reading cookies.

and what your problem is...

bool setcookie  ( string $name  [, string $value  [, int $expire  [, string $path  [, string $domain  [, bool $secure  [, bool $httponly  ]]]]]] )

http://tr2.php.net/setcookie

you give the path "/" on your server, but probably you don't have permissions to write there. try "/tmp/". also if you don't write correct domain, most browsers won't accept your cookie as a security matter. so do the two above like that:

setcookie("selectfc", $rand, $expyr, "/tmp/", "mydomain.com",0 );
0
 
Ray PaseurCommented:
Here is my "remember me" code that sets a cookie to remember someone that logged in.  I don't think the path is the issue.

Be aware, that even though you set the cookie in your script, the cookie is NOT available in $_COOKIE until the next script.  Also, (at least in FF) the cookie is available to all windows and tabs until all windows and tabs have been closed - this confused me several times!

Try substituting your values into my code and see if that helps.  If not, post back here and we will do some debugging.

Best regards, ~Ray
// LOGIN SUCCESSFUL
 
// SET A COOKIE TO REMEMBER THE LOGIN?
	if ($cFlag == '') {
		$cookie_expires	= 0; // EXPIRE AT END OF SESSION
	} else {
		$cookie_expires	= time() + 6*60*60 + $my_cookie_life;  // +6 HOURS FOR GMT (SERVER IN CENTRAL TIME)
	}
	
	$cookie_value	= $_SESSION["_cookie"]; // VALUE IS SET ELSEWHERE
	$cookie_name 	= "CP2C";
	$cookie_path	= '/';
	$cookie_domain	= ereg_replace('^www', '', strtolower($_SERVER["HTTP_HOST"])); // STARTS WITH DOT - NO WWW, ETC.
	$cookie_secure	= FALSE;
	$cookie_http	= TRUE;
	if (!setcookie($cookie_name, $cookie_value, $cookie_expires, $cookie_path, $cookie_domain, $cookie_secure, $cookie_http)) { /* echo "Cookie Failed"; */ }

Open in new window

0
 
Ray PaseurCommented:
Hey, smudgemouse - thanks for the points.  Good luck with your project! ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now