Jump to content

Making Cookies Work All Across A Website


!!!!!

Recommended Posts

Okay... I am trying to make a cookie that goes all across my website so it can keep displaying the information, but I only know how to make the cookie availabe on the page it was made at. I really don't want my website viewers having to keep using the info in Forms... So, can anybody please tell me how to make a cookie work all across my website? Thanks

Link to comment
https://forums.phpfreaks.com/topic/51899-making-cookies-work-all-across-a-website/
Share on other sites

The cookie has to be set in the root directory to be able to be accessed by all pages.....I found this out a while ago as the cookie was being set in the current directory, which meant that when another directory was being accessed, the cookie didn't work. Try this....

 

<? 
setcookie ("cookie", "Hello", 0, "/");  
// The "/" sets the cookie to root directory
?>

//If you echo out $_COOKIE["cookie"], it will display "Hello".

The cookie has to be set in the root directory to be able to be accessed by all pages.....I found this out a while ago as the cookie was being set in the current directory, which meant that when another directory was being accessed, the cookie didn't work. Try this....

 

<? 
setcookie ("cookie", "Hello", 0, "/");  
// The "/" sets the cookie to root directory
?>

//If you echo out $_COOKIE["cookie"], it will display "Hello".

I pasted that code into a Notepad, then made another one and put this in it:
<?php
echo $_COOKIE["cookie"];
?>

And the webpage is blank. Any answers to why??

Are you working with Localhost? If so look here: http://www.aeonity.com/frost/php-setcookie-localhost-apache

 

If not, try adding the domain parameter after the "/". IE:

 

<?php
// set cookie for one hour
setcookie ("cookie", "Hello", time()+3600, "/", 'yourdomain.com');  

 

Note there is a difference between www.yourdomain.com and yourdomain.com, use which ever you want but there is a difference.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.