回答編集履歴
3
修正
test
CHANGED
@@ -142,7 +142,7 @@
|
|
142
142
|
|
143
143
|
<?php
|
144
144
|
|
145
|
-
$terms = Const
|
145
|
+
$terms = AppConst::terms;
|
146
146
|
|
147
147
|
```
|
148
148
|
|
2
追記
test
CHANGED
@@ -75,3 +75,101 @@
|
|
75
75
|
(JSON とかが無かった時代はこの方法がよく使われていたような気がする)
|
76
76
|
|
77
77
|
(もっともその時代に data-* アトリビュートは無かったので input の type=hidden とかを使ってましたが)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
---
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
もし terms という値が定数的なもの(30 と値がベタ書きされていてアプリケーション内で変更されることがない)のであれば、php のソースファイルから js のコードを静的に生成する方法も考えられます。
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
例えば次のように定数を定義するためのクラスを作り、
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
```lang-php
|
94
|
+
|
95
|
+
<?php
|
96
|
+
|
97
|
+
class AppConst
|
98
|
+
|
99
|
+
{
|
100
|
+
|
101
|
+
const terms = 30;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
public static function buildJavaScript()
|
106
|
+
|
107
|
+
{
|
108
|
+
|
109
|
+
$ref = new ReflectionClass(__CLASS__);
|
110
|
+
|
111
|
+
$json = json_encode($ref->getConstants());
|
112
|
+
|
113
|
+
echo "var AppConst = $json;\n";
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
下記のコードの実行結果を `const.js` などと保存しておきます。
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
```lang-php
|
128
|
+
|
129
|
+
<?php
|
130
|
+
|
131
|
+
AppConst::buildJavaScript();
|
132
|
+
|
133
|
+
```
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
PHP 側ので terms の値が欲しければ次のようにします。
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
```lang-php
|
142
|
+
|
143
|
+
<?php
|
144
|
+
|
145
|
+
$terms = Constant::terms;
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
JS 側で欲しければ次のような感じです。
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
```lang-php
|
156
|
+
|
157
|
+
<html>
|
158
|
+
|
159
|
+
<head>
|
160
|
+
|
161
|
+
<script src="const.js"></script>
|
162
|
+
|
163
|
+
</head>
|
164
|
+
|
165
|
+
<script>
|
166
|
+
|
167
|
+
var terms = AppConst.terms;
|
168
|
+
|
169
|
+
console.log(terms);
|
170
|
+
|
171
|
+
</script>
|
172
|
+
|
173
|
+
```
|
174
|
+
|
175
|
+
|
1
追記
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
<?php
|
8
8
|
|
9
|
-
$terms = "</script>";
|
9
|
+
$terms = "<script>alert(1)</script>";
|
10
10
|
|
11
11
|
?>
|
12
12
|
|
@@ -31,3 +31,47 @@
|
|
31
31
|
</script>
|
32
32
|
|
33
33
|
```
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
---
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
あるいは適用な要素の属性値に入れてしまうという方法もあります。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
```lang-php
|
46
|
+
|
47
|
+
<?php
|
48
|
+
|
49
|
+
$terms = "<script>alert(1)</script>";
|
50
|
+
|
51
|
+
?>
|
52
|
+
|
53
|
+
<html>
|
54
|
+
|
55
|
+
<head>
|
56
|
+
|
57
|
+
<script id="data" data-terms="<?= htmlspecialchars($terms)?>"></script>
|
58
|
+
|
59
|
+
</head>
|
60
|
+
|
61
|
+
<script src="sample.js"></script>
|
62
|
+
|
63
|
+
<script>
|
64
|
+
|
65
|
+
var terms = document.getElementById('data').getAttribute('data-terms');
|
66
|
+
|
67
|
+
console.log(terms);
|
68
|
+
|
69
|
+
</script>
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
(JSON とかが無かった時代はこの方法がよく使われていたような気がする)
|
76
|
+
|
77
|
+
(もっともその時代に data-* アトリビュートは無かったので input の type=hidden とかを使ってましたが)
|