Link to home
Start Free TrialLog in
Avatar of smudgemouse
smudgemouse

asked on

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?
Avatar of obareey
obareey
Flag of Türkiye image

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 );
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey, smudgemouse - thanks for the points.  Good luck with your project! ~Ray