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'); ?>
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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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  ]]]]]] )

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/", "",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
	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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Hey, smudgemouse - thanks for the points.  Good luck with your project! ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.