note 25320 deleted from function.setcookie by sniper

From: Date: Wed, 11 Dec 2002 07:15:45 +0000
Subject: note 25320 deleted from function.setcookie by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-40721@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.

« previous php.notes (#40721) next »