Posted By

peterbelsky on 11/10/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

proto


cookies


 / Published in: PHP
 

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

  1. 1. Set cookie through PHP and get through JavaScript
  2. view plaincopy to clipboardprint?
  3.  
  4. 1. <?php
  5. 2. //Page: set_cookie.php
  6. 3.
  7. 4. //$_SERVER['HTTP_HOST'] = 'http://www.example.com ';
  8. 5.
  9. 6. // localhost create problem on IE so this line
  10. 7.
  11. 8. $myDomain = ereg_replace('^[^\.]*\.([^\.]*)\.(.*)$', '\1.\2', $_SERVER['HTTP_HOST']);
  12. 9.
  13. 10. $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false;
  14. 11.
  15. 12. setcookie ("site", 'http://only-your-views.blogspot.com', time()+3600*24*(2), '/', "$setDomain", 0 );
  16. 13.
  17. 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.
  18. 15. ?>
  19.  
  20. <?php
  21. //Page: set_cookie.php
  22.  
  23. //$_SERVER['HTTP_HOST'] = 'http://www.example.com ';
  24.  
  25. // localhost create problem on IE so this line
  26.  
  27. $myDomain = ereg_replace('^[^\.]*\.([^\.]*)\.(.*)$', '\1.\2', $_SERVER['HTTP_HOST']);
  28.  
  29. $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false;
  30.  
  31. setcookie ("site", 'http://only-your-views.blogspot.com', time()+3600*24*(2), '/', "$setDomain", 0 );
  32.  
  33. // 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.
  34. ?>
  35.  
  36. Page: get_cookie.html
  37. view plaincopy to clipboardprint?
  38.  
  39. 1. <script>
  40. 2.
  41. 3. function readCookie(name) {
  42. 4.
  43. 5. var cookiename = name + "=";
  44. 6.
  45. 7. var ca = document.cookie.split(';');
  46. 8.
  47. 9. for(var i=0;i < ca.length;i++)
  48. 10. {
  49. 11.
  50. 12. var c = ca[i];
  51. 13.
  52. 14. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  53. 15.
  54. 16. if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);
  55. 17.
  56. 18. }
  57. 19.
  58. 20. return null;
  59. 21. }
  60. 22.
  61. 23. document.write("n" + readCookie('site'));
  62. 24.
  63. 25. </script>
  64.  
  65. <script>
  66.  
  67. function readCookie(name) {
  68.  
  69. var cookiename = name + "=";
  70.  
  71. var ca = document.cookie.split(';');
  72.  
  73. for(var i=0;i < ca.length;i++)
  74. {
  75.  
  76. var c = ca[i];
  77.  
  78. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  79.  
  80. if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);
  81.  
  82. }
  83.  
  84. return null;
  85. }
  86.  
  87. document.write("n" + readCookie('site'));
  88.  
  89. </script>
  90.  
  91. 2. Set cookie through JavaScript and get through PHP
  92.  
  93. Page: set_cookie.html
  94. view plaincopy to clipboardprint?
  95.  
  96. 1. <script>
  97. 2.
  98. 3. document.cookie = 'name=David' ;
  99. 4.
  100. 5. </script>
  101. 6.
  102. 7. Page: get_cookie.php
  103. 8.
  104. 9. <?php
  105. 10.
  106. 11. var_dump($_COOKIE['name']);
  107. 12.
  108. 13. ?>
  109.  
  110. <script>
  111.  
  112. document.cookie = 'name=David' ;
  113.  
  114. </script>
  115.  
  116. Page: get_cookie.php
  117.  
  118. <?php
  119.  
  120. var_dump($_COOKIE['name']);
  121.  
  122. ?>
  123.  
  124. 3. Set cookie through JavaScript and get through JavaScript
  125. view plaincopy to clipboardprint?
  126.  
  127. 1. <script type="text/javascript">
  128. 2. days = 3; // -ve for deleting it.
  129. 3.
  130. 4. var date = new Date();
  131. 5.
  132. 6. date.setTime(date.getTime ()+(days*24*60*60*1000));
  133. 7.
  134. 8. var expires = "; expires="+date.toGMTString();
  135. 9.
  136. 10. document.cookie = 'language=ruby' + expires;
  137. 11.
  138. 12. function readCookie(name)
  139. 13. {
  140. 14.
  141. 15. var cookiename = name + "=";
  142. 16.
  143. 17. var ca = document.cookie.split(';');
  144. 18.
  145. 19. for(var i=0;i < ca.length;i++)
  146. 20. {
  147. 21.
  148. 22. var c = ca[i];
  149. 23.
  150. 24. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  151. 25.
  152. 26. if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);
  153. 27.
  154. 28. }
  155. 29.
  156. 30. return null;
  157. 31.
  158. 32. }
  159. 33.
  160. 34. // refresh the page for getting the value or use this line in another page
  161. 35. document.write("n" + readCookie('language'));
  162. 36.
  163. 37. </script>
  164.  
  165. <script type="text/javascript">
  166. days = 3; // -ve for deleting it.
  167.  
  168. var date = new Date();
  169.  
  170. date.setTime(date.getTime ()+(days*24*60*60*1000));
  171.  
  172. var expires = "; expires="+date.toGMTString();
  173.  
  174. document.cookie = 'language=ruby' + expires;
  175.  
  176. function readCookie(name)
  177. {
  178.  
  179. var cookiename = name + "=";
  180.  
  181. var ca = document.cookie.split(';');
  182.  
  183. for(var i=0;i < ca.length;i++)
  184. {
  185.  
  186. var c = ca[i];
  187.  
  188. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  189.  
  190. if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);
  191.  
  192. }
  193.  
  194. return null;
  195.  
  196. }
  197.  
  198. // refresh the page for getting the value or use this line in another page
  199. document.write("n" + readCookie('language'));
  200.  
  201. </script>
  202.  
  203. 4. Set cookie through PHP and get through PHP
  204. view plaincopy to clipboardprint?
  205.  
  206. 1. <?php
  207. 2.
  208. 3. //$_SERVER['HTTP_HOST'] = 'http://www.example.com ';
  209. 4.
  210. 5. // localhost create problem on IE so this line
  211. 6.
  212. 7. $myDomain = ereg_replace('^[^\.]*\.([^\.]*)\.(.*)$', '\1.\2', $_SERVER['HTTP_HOST']);
  213. 8.
  214. 9. $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false;
  215. 10.
  216. 11. setcookie ("site2", 'http://only-your-views.blogspot.com', time()+3600*24*(2), '/', "$setDomain", 0 );
  217. 12.
  218. 13. echo @$_COOKIE ['site2'];
  219. 14. ?>
  220.  
  221. <?php
  222.  
  223. //$_SERVER['HTTP_HOST'] = 'http://www.example.com ';
  224.  
  225. // localhost create problem on IE so this line
  226.  
  227. $myDomain = ereg_replace('^[^\.]*\.([^\.]*)\.(.*)$', '\1.\2', $_SERVER['HTTP_HOST']);
  228.  
  229. $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false;
  230.  
  231. setcookie ("site2", 'http://only-your-views.blogspot.com', time()+3600*24*(2), '/', "$setDomain", 0 );
  232.  
  233. echo @$_COOKIE ['site2'];
  234. ?>
  235.  
  236. 5. Set the Array Cookies
  237. view plaincopy to clipboardprint?
  238.  
  239. 1. <?php
  240. 2.
  241. 3. setcookie("cookie1[0]", "cookiethree");
  242. 4.
  243. 5. setcookie("cookie1[1]", "cookietwo");
  244. 6.
  245. 7. setcookie("cookie1[2]", "cookieone");
  246. 8.
  247. 9. // after the page reloads, echo them out
  248. 10.
  249. 11. if (isset($_COOKIE['cookie1']))
  250. 12. {
  251. 13.
  252. 14. foreach ($_COOKIE['cookie1'] as $name => $value)
  253. 15. {
  254. 16.
  255. 17. echo "cookie1[$name] : $value <br />n";
  256. 18. }
  257. 19. }
  258. 20.
  259. 21. ?>
  260.  
  261. <?php
  262.  
  263. setcookie("cookie1[0]", "cookiethree");
  264.  
  265. setcookie("cookie1[1]", "cookietwo");
  266.  
  267. setcookie("cookie1[2]", "cookieone");
  268.  
  269. // after the page reloads, echo them out
  270.  
  271. if (isset($_COOKIE['cookie1']))
  272. {
  273.  
  274. foreach ($_COOKIE['cookie1'] as $name => $value)
  275. {
  276.  
  277. echo "cookie1[$name] : $value <br />n";
  278. }
  279. }
  280.  
  281. ?>
  282.  
  283. 6. Delete all Cookies through PHP
  284. view plaincopy to clipboardprint?
  285.  
  286. 1. <?php
  287. 2.
  288. 3. foreach ($_COOKIE as $k=>$v)
  289. 4. {
  290. 5.
  291. 6. if (is_array($_COOKIE[$k]))
  292. 7. {
  293. 8.
  294. 9. foreach ($_COOKIE[$k] as $key=>$val)
  295. 10. {
  296. 11. setcookie($k.'['.$key.']',"", time()+3600*24*(-100));
  297. 12. }
  298. 13. }
  299. 14.
  300. 15. setcookie($k,"", time()+3600*24*(-100));
  301. 16. }
  302. 17. ?>
  303.  
  304. <?php
  305.  
  306. foreach ($_COOKIE as $k=>$v)
  307. {
  308.  
  309. if (is_array($_COOKIE[$k]))
  310. {
  311.  
  312. foreach ($_COOKIE[$k] as $key=>$val)
  313. {
  314. setcookie($k.'['.$key.']',"", time()+3600*24*(-100));
  315. }
  316. }
  317.  
  318. setcookie($k,"", time()+3600*24*(-100));
  319. }
  320. ?>
  321.  
  322. Few facts about Cookie:
  323.  
  324. Expire time is dependent of Client time. So, remember to check your geo location and your visitors geo location.
  325.  
  326. If you do not specify expiry date for cookie then it will available, until browser is closed.
  327.  
  328. Path for cookie is the current directory by default.
  329.  
  330. In PHP, cookie must be sent before any output to client.
  331.  
  332. In PHP setcookie function accepts argument like this:
  333.  
  334. True/False Setcookie (name, value, expire, path, domain, secure)
  335.  
  336. Path = ‘/’ will set cookie for entire domain. Path = ‘foo’ will set it for foo directory and subdirectory of ‘/foo/’.
  337.  
  338. Httponly is last parameter added in PHP 5.2.0 in setcookie (), but not supported by all browsers.
  339.  
  340. For deleting cookie, you will set cookie again but with days with negative values.

Report this snippet  

You need to login to post a comment.