note 25320 added to function.setcookie

From: Date: Thu, 19 Sep 2002 15:14:23 +0000
Subject: note 25320 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-36959@lists.php.net to get a copy of this message
Hi, I just figured out a great way to check if someone has cookies enabled or not. My problem was that I wanted it to work across domains and I didn't want to reload the whole page to check if he accepted cookies. I have one domain that I use to set the cookie from (don't forget to read up on P3P issues), it's actually 3 scripts that are involved. http://domain.com/cookie1.php http://domain.com/cookie2.php http://domain.com/cookie3.php on http://domain2.com/index.html I add <IMG SRC="http://domain.com/cookie1.php"> cookie1.php looks like this.... if (!isset($_COOKIE["test"])) { setcookie("test", md5(microtime(""))); header("Location: /cookie2.php"); } else { header("Content-type: image/png"); $im = imagecreate (1, 1); $background_color = imagecolorallocate ($im, 255, 255, 255); imagepng ($im); } What does it do? if the cookie "test" isn't set, it will set the cookie "test" and then redirect you to cookie2.php. If it is set, it will print a 1x1 PNG (which is need because we called it from an IMG tag). cookie2.php looks like this... if (!isset($_COOKIE["test"])) { header("Location: /cookie3.php"); } else { header("Content-type: image/png"); $im = imagecreate (1, 1); $background_color = imagecolorallocate ($im, 255, 255, 255); imagepng ($im); } If the cookie isn't set, the user doesn't accept cookies and redirect him to cookie3.php. When he gets to cookie3.php you can do what ever you want but don't forget to print a PNG. If the cookie was set, the surfer accepts cookies. -- http://www.php.net/manual/en/function.setcookie.php http://master.php.net/manage/user-notes.php?action=edit+25320 http://master.php.net/manage/user-notes.php?action=delete+25320 http://master.php.net/manage/user-notes.php?action=reject+25320

« previous php.notes (#36959) next »