{"id":6595,"date":"2024-10-24T10:51:48","date_gmt":"2024-10-24T01:51:48","guid":{"rendered":"https:\/\/taitan916.info\/blog\/?p=6595"},"modified":"2024-07-26T14:58:29","modified_gmt":"2024-07-26T05:58:29","slug":"post-6595","status":"publish","type":"post","link":"https:\/\/taitan916.info\/blog\/archives\/6595","title":{"rendered":"jQuery\u304b\u3089Ajax\u7d4c\u7531\u3067PHP\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3057Cookie\u3092\u64cd\u4f5c\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>\u5148\u65e5<a href=\"https:\/\/taitan916.info\/blog\/archives\/6552\" target=\"_blank\" rel=\"noopener\">PHP\u3067Cookie\u4fdd\u5b58\u6642\u306bHttpOnly \/ Secure\u5c5e\u6027\u3092\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5<\/a>\u3092\u30e1\u30e2\u3057\u305f\u304c\u3001HttpOnly\u5c5e\u6027\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u3068\u5f53\u7136JavaScript \/ jQuery\u304b\u3089\u5f53\u8a72Cookie\u306f\u53d6\u308a\u6271\u3048\u306a\u3044\u3002\u305f\u3060\u3001Ajax\u7d4c\u7531\u3067PHP\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3059\u308c\u3070\u53d6\u308a\u6271\u3048\u305d\u3046\u3002\u4ee5\u4e0b\u306b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u30e1\u30e2\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n<h3>Ajax\u5074<\/h3>\n<p>Ajax\u7d4c\u7531\u3067PHP\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3057Cookie\u306eon \/ off\u3092#result\u90e8\u5206\u306b\u53cd\u6620\u3059\u308b\u3068\u3044\u3046\u5185\u5bb9\u306b\u306a\u308b\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;div id=\"result\"&gt;\r\n    &lt;?php if( isset($_COOKIE['test_cookie']) ){ ?&gt;\r\n        on\r\n    &lt;?php }else{ ?&gt;\r\n        off\r\n    &lt;?php } ?&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;button id=\"cookie_btn\"&gt;cookie btn&lt;\/button&gt;\r\n\r\n&lt;script type=\"text\/javascript\" src=\"https:\/\/code.jquery.com\/jquery-3.7.1.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\n$(function(){\r\n    $(`#cookie_btn`).on(`click`, function(){\r\n        $.ajax({\r\n            url:'.\/set_cookie.php',\r\n            type: 'POST',\r\n        }).done(function(result){\r\n            if( result == 0 ){\r\n                $(`#result`).text(`off`);\r\n            }else if( result == 1 ){\r\n                $(`#result`).text(`on`);\r\n            }else{\r\n                alert('\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002');\r\n            }\r\n        }).fail(function() {\r\n            alert('\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002');\r\n        }).always(function() {\r\n            \r\n        });\r\n    });\r\n});\r\n&lt;\/script&gt;<\/pre>\n<h3>\u53d7\u3051\u53d6\u308a\u5074<\/h3>\n<p>\u30a8\u30e9\u30fc\u30c1\u30a7\u30c3\u30af\u7b49\u306f\u5165\u3063\u3066\u3044\u306a\u3044\u70b9\u306b\u6ce8\u610f\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\nif( isset($_COOKIE['test_cookie']) ){\r\n    $cookie_options = array(\r\n        'expires' =&gt; time() - 3600, \r\n        'path' =&gt; '\/',\r\n    );\r\n    setcookie('test_cookie', '', $cookie_options);\r\n    $return = 0;\r\n}else{\r\n    $cookie_options = array(\r\n        'expires' =&gt; time() + 3600, \r\n        'path' =&gt; '\/',\r\n        'domain' =&gt; '',\r\n        'secure' =&gt; true,\r\n        'httponly' =&gt; true,\r\n        'samesite' =&gt; 'Lax'\r\n    );\r\n    setcookie('test_cookie', true, $cookie_options);\r\n    $return = 1;\r\n}\r\n\r\necho $return;<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u6240\u611f<\/h2>\n<p>Ajax\u90e8\u5206\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u304c\u5897\u3048\u308b\u306e\u3067\u3084\u3084\u9762\u5012\u306a\u3082\u306e\u306e\u3001\u3053\u308c\u3067JavaScript \/ jQuery\u304b\u3089\u3082Cookie\u3092\u53d6\u308a\u6271\u3048\u305d\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u65e5PHP\u3067Cookie\u4fdd\u5b58\u6642\u306bHttpOnly \/ Secure\u5c5e\u6027\u3092\u8a2d\u5b9a\u3059\u308b ... <\/p>\n","protected":false},"author":1,"featured_media":1071,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,3,4],"tags":[],"class_list":["post-6595","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-jquery","category-javascript"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/6595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/comments?post=6595"}],"version-history":[{"count":1,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/6595\/revisions"}],"predecessor-version":[{"id":6597,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/6595\/revisions\/6597"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media\/1071"}],"wp:attachment":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media?parent=6595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/categories?post=6595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/tags?post=6595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}