PHP; Set Cookies Not Working

Posted on 2008-11-18
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
    LVL 4

    Expert Comment

    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 );
    LVL 107

    Accepted Solution

    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 107

    Expert Comment

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

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    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.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now