0

This code in http://www.w3schools.com/php/showphp.asp?filename=demo_cookie5 returns

"Cookies are enabled"

In my localhost it returns

"Cookies are disabled"

maxshuty
  • 9,708
  • 13
  • 64
  • 77
yahya
  • 1
  • 1
  • 2
  • 2
    http://www.w3fools.com/ – Popnoodles Aug 17 '15 at 19:19
  • when you say "in localhost" are you saying running it from the command line? command line php isn't going to save cookies. – jdu Aug 17 '15 at 19:20
  • That example is bad, as `$_COOKIE` won't have value available in it until next page load. Also, the example is bad because you are outputting content to browser before `setcookie()` is called. cookies are set in response headers and after content is sent (as happens with DOCTYPE declaration), you will not be able to send headers anymore. Finally, testing this code on embedded code tester on W3schools site is going to be useless. Chances are if you have visited the site at any point prior to visiting that code sample, you will already have cookies on that domain. – Mike Brant Aug 17 '15 at 19:57
  • See http://stackoverflow.com/questions/6663859/check-if-cookies-are-enabled – tshalif Dec 26 '16 at 19:36

3 Answers3

0

This is just bad example.

Use isset($_COOKIE) instead of count($_COOKIE) > 0.

Also, use print_r($_COOKIE) to see its structure.

al'ein
  • 1,711
  • 1
  • 14
  • 21
0

This isn't a problem, it's a security configuration in your browser.

You only have to allow cookies in your browser.

Emmanuel HD
  • 191
  • 7
0

If you have enabled cookies in your browser then something simple like this should do the job:

if(isset($_COOKIE['name']))
{
$var = $_COOKIE['name'];
echo "Cookie was set".  "<br>";
echo "Welcome back! <br> Your name: ".  $var.  "<br>";
}
DirtyBit
  • 16,613
  • 4
  • 34
  • 55