Home Backend Development PHP Tutorial Comparison of cookie operation methods in php and js

Comparison of cookie operation methods in php and js

Jul 25, 2016 am 08:56 AM

  1. setcookie('php_cn_ck','php_中文_cookie');
  2. setcookie('php_en_ck','php_english_cookie');
  3. ?>
  4. <script> </li> <li> Cookies.set('js_cn_ck','js_中文_cookie',5000); </li> <li> Cookies.set('js_en_ck','js_english_cookie'); </li> <li>&lt ;/script> </li> <li><meta http-equiv="Content-Type" content="text/html; charset=utf8"> </li> <li>PHP cookies have been set: php_cn_ck=php_中文_cookie, php_en_ck=php_english_cookie. </li> <li>JS cookies have been set: js_cn_ck=js_中文_cookie, js_en_ck=js_english_cookie. </li> <li><a href=getcookie.php>Read cookie</a><br> </li> <li><meta http-equiv="Content-Type" content="text/html; charset=utf8"></li> </ol></div> <em onclick="copycode($('code_Ke6'));"> Copy the code </em> </div> <p>1, read the Chinese and English cookies sent by php<br><br> 1. php reads php and sets php cookie </p> <div class="blockcode"> <div id="code_LTZ"><ol> <li> <li><?php </li> <li> include('function.php'); </li> <li> $php_cn_ck=$_COOKIE['php_cn_ck']; </li> <li> $un_php_cn_ck=unescape($php_cn_ck); </li> <li> echo "Chinese cookie before decoding :php_cn_ck=$php_cn_ck<br><br>"; </li> <li> echo "Decoded Chinese cookie:un_php_cn_ck=$un_php_cn_ck<br><br>"; </li> <li> $php_en_ck=$_COOKIE['php_en_ck']; </li> <li> echo "English cookies do not need to be decoded: php_en_ck=$php_en_ck<br><br>"; </li> <li>?></li> </ol></div> <em onclick="copycode($('code_LTZ'));">Copy code</em> </div> <p>2, js reads php to set cookies<br><br> </p> <div class="blockcode"> <div id="code_lnN"><ol> <li> <li><script src="cookie.js"></script>
  5. <script> </li> <li> php_cn_ck=Cookies.get('php_cn_ck'); </li> <li> un_php_cn_ck = decodeURIComponent (escape(php_cn_ck) ) ; </li> <li> document.write("Chinese cookie before decoding: php_cn_ck="+php_cn_ck+"<Br><br>"); </li> <li> document.write("Chinese cookie after decoding: un_php_cn_ck="+un_php_cn_ck+"< Br><br>"); </li> <li> php_en_ck=Cookies.get('php_en_ck'); </li> <li> document.write("English cookies do not need to be decoded: php_en_ck="+php_en_ck+"<Br><br>"); </li> <li> </script>
Copy the code

Second, read the Chinese and English cookies sent by JS 1. PHP reads JS and sets js cookie

  1. $js_cn_ck=$_COOKIE['js_cn_ck'];
  2. $un_js_cn_ck=unescape($js_cn_ck);
  3. echo "Chinese cookie before decoding:js_cn_ck=$js_cn_ck";
  4. echo " Decoded Chinese cookie: un_js_cn_ck=$un_js_cn_ck";
  5. $js_en_ck=$_COOKIE['js_en_ck'];
  6. echo "English cookie does not need to be decoded: js_en_ck=$js_en_ck";
  7. ?>
Copy code

2, js reads the cookie set by js

  1. <script> </li> <li> js_cn_ck=Cookies.get('js_cn_ck'); </li> <li> document.write("Chinese cookie before decoding: js_cn_ck="+js_cn_ck+"<Br><br>") ; </li> <li> //un_js_cn_ck = decodeURIComponent (escape(js_cn_ck)); Calling these two sentences will cause js parsing interruption </li> <li> //document.write("Decoded Chinese cookie: un_js_cn_ck="+un_js_cn_ck+"<Br><br> "); </li> <li> js_en_ck=Cookies.get('js_en_ck'); </li> <li> document.write("English cookies do not need to be decoded: js_en_ck="+js_en_ck+"<Br><br>"); </li> <li></script>
Copy code

Summary: 1. PHP uses its own function to read PHP cookies without any obstacles and without decoding. 2. js uses the cookie.js method to read js cookies without any obstacles and without decoding. 3. To read the Chinese cookie of PHP with js, you need to do "decodeURIComponent (escape(php_cn_ck))" function processing. 4. PHP needs to use the "unescape()" function to read the Chinese cookie of JS. Code:

  1. var Cookies = {};
  2. /**
  3. * Set Cookies
  4. */
  5. Cookies.set = function(name, value){
  6. var argv = arguments;
  7. var argc = arguments.length;
  8. var expires = (argc > 2) ? argv[2] : null;
  9. if(expires != null){
  10. var exp  = new Date();
  11. exp.setTime(exp.getTime() + 8*3600 + expires) ;
  12. }
  13. alert(exp.toGMTString());
  14. var path = (argc > 3) ? argv[3] : '/';
  15. var domain = (argc > 4) ? argv[4] : null ;
  16. var secure = (argc > 5) ? argv[5] : false;
  17. document.cookie = name + "=" + escape (value) +
  18. ((expires == null) ? "" : ("; expires=" + exp.toGMTString())) +
  19. ((path == null) ? "" : ("; path=" + path)) +
  20. ((domain == null) ? "" : ("; domain=" + domain)) +
  21. ((secure == true) ? "; secure" : "");
  22. };
  23. /**
  24. * Read Cookies
  25. */
  26. Cookies.get = function(name){
  27. var arg = name + "=";
  28. var alen = arg.length;
  29. var clen = document.cookie.length;
  30. var i = 0;
  31. var j = 0;
  32. while(i < clen){
  33. j = i + alen;
  34. if (document.cookie.substring(i, j) == arg)
  35. return Cookies.getCookieVal(j);
  36. i = document.cookie.indexOf(" ", i) + 1;
  37. if( i == 0)
  38. break;
  39. }
  40. return null;
  41. };
  42. /**
  43. * Clear Cookies
  44. */
  45. Cookies.clear = function(name) {
  46. if(Cookies.get(name)){
  47. var expdate = new Date();
  48. expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
  49. Cookies.set(name, "", expdate);
  50. }
  51. };
  52. Cookies.getCookieVal = function (offset){
  53. var endstr = document.cookie.indexOf(";", offset);
  54. if(endstr == -1){
  55. endstr = document.cookie.length;
  56. }
  57. return unescape(document.cookie. substring(offset, endstr));
  58. };
Copy code


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles