PHP; Set Cookies Not Working

Posted on 2008-11-18
Medium Priority
Last Modified: 2012-05-05
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?
Question by:smudgemouse
  • 2

Expert Comment

ID: 22983246
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/", "mydomain.com",0 );
LVL 111

Accepted Solution

Ray Paseur earned 2000 total points
ID: 22984880
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

LVL 111

Expert Comment

by:Ray Paseur
ID: 22994078
Hey, smudgemouse - thanks for the points.  Good luck with your project! ~Ray

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question