1

Does anyone know why these setcookies() do not work? No cookies are getting written to the local machine:

<?php
    setcookie("repeat_visitor", "yes", time() + (10 * 365 * 24 * 60 * 60), "/", "www.mydomain.com");
    setcookie("repeat_visitor", "yes", time() + (10 * 365 * 24 * 60 * 60), "/", "mydomain.com");
?>

AMENDMENT

Well, here is what I want to do -- I want to display a message to the visitor when they hit my site once, on the first time someone hits the site. After that hit, I do not want to display the message any longer to that visitor. I am thinking setting a cookie is the way to go with that.

If it is not, can you share any other suggestions?

Thanks.

H. Ferrence
  • 7,906
  • 31
  • 98
  • 161
  • 1
    does your local machine add host like `127.0.0.1 www.mydomain.com` ? – jasonslyvia Oct 24 '13 at 01:02
  • I am not sure @jasonslyvia. I am actually not sure what you mean. Can you provide guidance on how I can check what you are referring too? – H. Ferrence Oct 24 '13 at 01:05
  • How are you checking if these cookies exist? – Chase Oct 24 '13 at 01:15
  • First of all, through the website because I show a pop-up (css overlay) and set the cookie, Then on a subsequent page display, the cookies should be set and the overlay should not appear. I am also looking at the cookies through Chrome's settings to see if the presence of the cookie gets set. – H. Ferrence Oct 24 '13 at 01:24

1 Answers1

0

If your localhost of 127.0.0.1 does not resolve to www.mydomain.com or mydomain.com, then you are trying to set third party cookies which browsers today do not allow.

You also can not set cookies to an IP or localhost. So you need to do what @jasonslyvia suggests and set up the domain on your local machine.

See setcookie() does not set cookie in Google Chrome

Community
  • 1
  • 1
jk.
  • 14,365
  • 4
  • 43
  • 58
  • let me ask you this @jk ... can you set cookies via PHP so that the cookies get registered in Chrome and all other browsers? I am not sure I need to be concerned about localhost and 127.0.0.1,, etc. I am hoping to use cookie technology for anyone and everyone that hits my site – H. Ferrence Oct 24 '13 at 01:42
  • @H.Ferrence Yes, provided they are not third party meaning that the cookie domain matches the domain of the php page. – jk. Oct 24 '13 at 01:48
  • the cookies in my PHP code match my site's domain. Since they are not working I was writing 2 cookies -- the "www" version and version without "www" even through the PHP documentation states that www. covers the full domain and the version without the www – H. Ferrence Oct 24 '13 at 01:54
  • @H.Ferrence Try setting them a different way. See the docs on the various methods: http://php.net/manual/en/function.setcookie.php – jk. Oct 24 '13 at 02:00
  • how many ways can you set a cookie? isn't there just one? and do you see anything wrong with my syntax above? @jk – H. Ferrence Oct 24 '13 at 02:04