/ Published in: PHP
URL: http://www.satya-weblog.com/2007/05/php-and-javascript-cookie.html
Expand |
Embed | Plain Text
1. Set cookie through PHP and get through JavaScript view plaincopy to clipboardprint? 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. 9. 10. $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false; 11. 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. ?> <?php //Page: set_cookie.php //$_SERVER['HTTP_HOST'] = 'http://www.example.com '; // localhost create problem on IE so this line $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false; // 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. ?> Page: get_cookie.html view plaincopy to clipboardprint? 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> <script> function readCookie(name) { var cookiename = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length); } return null; } document.write("n" + readCookie('site')); </script> 2. Set cookie through JavaScript and get through PHP Page: set_cookie.html view plaincopy to clipboardprint? 1. <script> 2. 3. document.cookie = 'name=David' ; 4. 5. </script> 6. 7. Page: get_cookie.php 8. 9. <?php 10. 12. 13. ?> <script> document.cookie = 'name=David' ; </script> Page: get_cookie.php <?php ?> 3. Set cookie through JavaScript and get through JavaScript view plaincopy to clipboardprint? 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> <script type="text/javascript"> days = 3; // -ve for deleting it. var date = new Date(); date.setTime(date.getTime ()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); document.cookie = 'language=ruby' + expires; function readCookie(name) { var cookiename = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length); } return null; } // refresh the page for getting the value or use this line in another page document.write("n" + readCookie('language')); </script> 4. Set cookie through PHP and get through PHP view plaincopy to clipboardprint? 1. <?php 2. 3. //$_SERVER['HTTP_HOST'] = 'http://www.example.com '; 4. 5. // localhost create problem on IE so this line 6. 8. 9. $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false; 10. 12. 14. ?> <?php //$_SERVER['HTTP_HOST'] = 'http://www.example.com '; // localhost create problem on IE so this line $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false; ?> 5. Set the Array Cookies view plaincopy to clipboardprint? 1. <?php 2. 4. 6. 8. 9. // after the page reloads, echo them out 10. 12. { 13. 14. foreach ($_COOKIE['cookie1'] as $name => $value) 15. { 16. 18. } 19. } 20. 21. ?> <?php // after the page reloads, echo them out { foreach ($_COOKIE['cookie1'] as $name => $value) { echo "cookie1[$name] : $value <br />n"; } } ?> 6. Delete all Cookies through PHP view plaincopy to clipboardprint? 1. <?php 2. 3. foreach ($_COOKIE as $k=>$v) 4. { 5. 7. { 8. 9. foreach ($_COOKIE[$k] as $key=>$val) 10. { 12. } 13. } 14. 16. } 17. ?> <?php foreach ($_COOKIE as $k=>$v) { { foreach ($_COOKIE[$k] as $key=>$val) { } } } ?> 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.
You need to login to post a comment.
