Skip to main content

Set/Get Cookie using PHP and JavaScript

Find variety of codes and details discussion about the cookie both using JavaScript and PHP:

  1. Set cookie through PHP and get through JavaScript
  2. Set cookie through JavaScript and get through PHP
  3. Set cookie in JavaScript and get through JavaScript
  4. Set cookie through PHP and get through PHP
  5. Set the Array Cookies
  6. Delete all Cookies through PHP

Commented code are left so that you can find something new or play with that.

1. Set cookie through PHP and get through JavaScript

  1. <?php  
  2. //Page: set_cookie.php  
  3.   
  4. //$_SERVER['HTTP_HOST'] = 'http://www.example.com ';  
  5.   
  6. // localhost create problem on IE so this line  
  7. // to get the top level domain  
  8. $myDomain = ereg_replace('^[^\.]*\.([^\.]*)\.(.*)$''\1.\2'$_SERVER['HTTP_HOST']);  
  9.   
  10. $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false;  
  11.   
  12. setcookie ("site"'http://only-your-views.blogspot.com', time()+3600*24*(2), '/'"$setDomain", 0 );  
  13.   
  14. // You can change (2) to any negative value (-2) for deleting it. It is number of days for cookie to keep live. Any -ve number will tell browser that it is useless now.  
  15. ?>  

Page: get_cookie.html

  1. <script>  
  2.   
  3. function readCookie(name) {  
  4.   
  5.     var cookiename = name + "=";  
  6.   
  7.     var ca = document.cookie.split(';');  
  8.   
  9.     for(var i=0;i < ca.length;i++)  
  10.     {  
  11.   
  12.         var c = ca[i];  
  13.   
  14.         while (c.charAt(0)==' ') c = c.substring(1,c.length);  
  15.   
  16.         if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);  
  17.   
  18.     }  
  19.   
  20.     return null;  
  21. }  
  22.   
  23. document.write("n" + readCookie('site'));  
  24.   
  25. </script>  

2. Set cookie through JavaScript and get through PHP

Page: set_cookie.html

  1. <script>  
  2.   
  3. document.cookie = 'name=David' ;  
  4.   
  5. </script>  
  6.   
  7. Page: get_cookie.php  
  8.   
  9. <?php  
  10.   
  11. var_dump($_COOKIE['name']);  
  12.   
  13. ?>  

3. Set cookie through JavaScript and get through JavaScript

  1. <script type="text/javascript">  
  2. days = 3; // -ve for deleting it.  
  3.   
  4. var date = new Date();  
  5.   
  6. date.setTime(date.getTime ()+(days*24*60*60*1000));  
  7.   
  8. var expires = "; expires="+date.toGMTString();  
  9.   
  10. document.cookie = 'language=ruby' + expires;  
  11.   
  12. function readCookie(name)  
  13. {  
  14.   
  15.     var cookiename = name + "=";  
  16.   
  17.     var ca = document.cookie.split(';');  
  18.   
  19.     for(var i=0;i < ca.length;i++)  
  20.     {  
  21.   
  22.         var c = ca[i];  
  23.   
  24.         while (c.charAt(0)==' ') c = c.substring(1,c.length);  
  25.   
  26.         if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);  
  27.   
  28.     }  
  29.   
  30.     return null;  
  31.   
  32. }  
  33.   
  34. // refresh the page for getting the value or use this line in another page  
  35. document.write("n" + readCookie('language'));  
  36.   
  37. </script>  

4. Set cookie through PHP and get through PHP

  1. <?php  
  2.   
  3. //$_SERVER['HTTP_HOST'] = 'http://www.example.com ';  
  4.   
  5. // localhost create problem on IE so this line  
  6. // to get the top level domain  
  7. $myDomain = ereg_replace('^[^\.]*\.([^\.]*)\.(.*)$''\1.\2'$_SERVER['HTTP_HOST']);  
  8.   
  9. $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false;  
  10.   
  11. setcookie ("site2"'http://only-your-views.blogspot.com', time()+3600*24*(2), '/'"$setDomain", 0 );  
  12.   
  13. echo @$_COOKIE ['site2'];  
  14. ?>  

5. Set the Array Cookies

  1. <?php  
  2.   
  3. setcookie("cookie1[0]""cookiethree");  
  4.   
  5. setcookie("cookie1[1]""cookietwo");  
  6.   
  7. setcookie("cookie1[2]""cookieone");  
  8.   
  9. // after the page reloads, echo them out  
  10.   
  11. if (isset($_COOKIE['cookie1']))  
  12. {  
  13.   
  14.     foreach ($_COOKIE['cookie1'as $name => $value)  
  15.     {  
  16.   
  17.         echo "cookie1[$name] : $value <br />n";  
  18.     }  
  19. }  
  20.   
  21. ?>  

6. Delete all Cookies through PHP

  1. <?php  
  2.   
  3. foreach ($_COOKIE as $k=>$v)  
  4. {  
  5.   
  6.     if (is_array($_COOKIE[$k]))  
  7.     {  
  8.   
  9.         foreach ($_COOKIE[$kas $key=>$val)  
  10.         {  
  11.             setcookie($k.'['.$key.']',"", time()+3600*24*(-100));  
  12.         }  
  13.     }  
  14.   
  15.     setcookie($k,"", time()+3600*24*(-100));  
  16. }  
  17. ?>  

Few facts about Cookie:

Expire time is dependent of Client time. So, remember to check your geo location and your visitors geo location.

If you do not specify expiry date for cookie then it will available, until browser is closed.

Path for cookie is the current directory by default.

In PHP, cookie must be sent before any output to client.

In PHP setcookie function accepts argument like this:

True/False Setcookie (name, value, expire, path, domain, secure)

Path = ‘/’ will set cookie for entire domain. Path = ‘foo’ will set it for foo directory and subdirectory of ‘/foo/’.

Httponly is last parameter added in PHP 5.2.0 in setcookie (), but not supported by all browsers.

For deleting cookie, you will set cookie again but with days with negative values.

Ref: http://www.satya-weblog.com/2007/05/php-and-javascript-cookie.html