回答編集履歴

1

sample

2017/12/26 09:04

投稿

yambejp
yambejp

スコア114829

test CHANGED
@@ -33,3 +33,81 @@
33
33
  'user_title'
34
34
 
35
35
  は設定されていないなら、結局elseにしか処理がいかないのでは?
36
+
37
+
38
+
39
+ # sample
40
+
41
+ 参考までに
42
+
43
+
44
+
45
+ ```PHP
46
+
47
+ <?php
48
+
49
+ $val="あああ";
50
+
51
+ $ligetime = 1800;
52
+
53
+ if(isset($_COOKIE['user_title'])){
54
+
55
+ $vals=json_decode($_COOKIE['user_title']);
56
+
57
+ $vals[]=$val;
58
+
59
+ setcookie('user_title',json_encode($vals),time() + $ligetime);
60
+
61
+ }else{
62
+
63
+ setcookie('user_title',json_encode([$val]),time() + $ligetime);
64
+
65
+ }
66
+
67
+ ?>
68
+
69
+ <script>
70
+
71
+ var user_title=JSON.parse(unescape(getCookie("user_title")));
72
+
73
+ console.log(user_title);
74
+
75
+ if(user_title.length>5) delCookie('user_title');
76
+
77
+ function getCookie(key){
78
+
79
+ var params = document.cookie.split(/; */);
80
+
81
+ var ret=null;
82
+
83
+ for(var i=0;i<params.length;i++){
84
+
85
+ if(r=params[i].match(new RegExp("^"+key+"="+"(.*)"))){
86
+
87
+ ret=r[1];
88
+
89
+ }
90
+
91
+ }
92
+
93
+ return ret;
94
+
95
+ }
96
+
97
+ function delCookie(key){
98
+
99
+ expiredate = new Date();
100
+
101
+ expiredate.setYear(expiredate.getYear()-1);
102
+
103
+ tmp = key+"=;";
104
+
105
+ tmp += "expires="+expiredate.toGMTString();
106
+
107
+ document.cookie = tmp;
108
+
109
+ };
110
+
111
+ </script>
112
+
113
+ ```