回答編集履歴
2
追記
test
CHANGED
@@ -8,6 +8,8 @@
|
|
8
8
|
|
9
9
|
0. HTML構造が正しくありません。
|
10
10
|
|
11
|
+
0. DBにHTMLを格納すると、後々面倒なことになります。
|
12
|
+
|
11
13
|
|
12
14
|
|
13
15
|
#修正コードサンプル
|
1
追記
test
CHANGED
@@ -7,3 +7,211 @@
|
|
7
7
|
0. XSS脆弱性があります。
|
8
8
|
|
9
9
|
0. HTML構造が正しくありません。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
#修正コードサンプル
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
```php
|
18
|
+
|
19
|
+
<?php
|
20
|
+
|
21
|
+
// エラーを強制的に表示する。
|
22
|
+
|
23
|
+
// 開発中は必ず記述すべき。
|
24
|
+
|
25
|
+
// エラーメッセージなしに、デバッグなんてできません。
|
26
|
+
|
27
|
+
ini_set('display_errors', true);
|
28
|
+
|
29
|
+
error_reporting(E_ALL);
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
function h($string)
|
34
|
+
|
35
|
+
{
|
36
|
+
|
37
|
+
return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
|
38
|
+
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
try {
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
// 投稿 を押したとき
|
48
|
+
|
49
|
+
if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) {
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
$title = filter_input(INPUT_POST, 'title');
|
54
|
+
|
55
|
+
$content = filter_input(INPUT_POST, 'content');
|
56
|
+
|
57
|
+
$upfile = $_FILES["upfile"];
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
// エラーを格納する配列
|
62
|
+
|
63
|
+
$err = [];
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
// タイトル入力チェック
|
68
|
+
|
69
|
+
if ('' === $title) {
|
70
|
+
|
71
|
+
$err[] = 'タイトルがありません。';
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
// 内容入力チェック
|
78
|
+
|
79
|
+
if ('' === $content) {
|
80
|
+
|
81
|
+
$err[] = '本文がありません。';
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
// ファイルアップロード失敗
|
88
|
+
|
89
|
+
if ($upfile['error'] > 0) {
|
90
|
+
|
91
|
+
$err[] = '画像をアップロードしてください。';
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
// エラーがないとき
|
98
|
+
|
99
|
+
if (count($err) === 0) {
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
$fileName = sha1($upfile["tmp_name"]);
|
104
|
+
|
105
|
+
move_uploaded_file($upfile["tmp_name"], "Image/$fileName.png");
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
$dsn = 'mysql:host=localhost;dbname=sample;charset=utf-8';
|
110
|
+
|
111
|
+
$username = '';
|
112
|
+
|
113
|
+
$password = '';
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
// DBエラーを例外でスローする設定(必須)
|
118
|
+
|
119
|
+
$options = [
|
120
|
+
|
121
|
+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
122
|
+
|
123
|
+
];
|
124
|
+
|
125
|
+
$pdo = new PDO($dsn, $username, $password, $options);
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
$sql = 'INSERT INTO `post` (title, eyecatch, content) VALUES (?, ?, ?)';
|
130
|
+
|
131
|
+
$stmt = $pdo->prepare($sql);
|
132
|
+
|
133
|
+
$params = [
|
134
|
+
|
135
|
+
$title
|
136
|
+
|
137
|
+
, $fileName
|
138
|
+
|
139
|
+
, $content
|
140
|
+
|
141
|
+
];
|
142
|
+
|
143
|
+
$stmt->execute($params);
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
} catch (Exception $ex) {
|
150
|
+
|
151
|
+
$err[] = $ex->getMessage();
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
?>
|
156
|
+
|
157
|
+
<!DOCTYPE HTML>
|
158
|
+
|
159
|
+
<html lang="ja">
|
160
|
+
|
161
|
+
<head>
|
162
|
+
|
163
|
+
<meta charset="UTF-8">
|
164
|
+
|
165
|
+
<title></title>
|
166
|
+
|
167
|
+
</head>
|
168
|
+
|
169
|
+
<body>
|
170
|
+
|
171
|
+
<div>
|
172
|
+
|
173
|
+
<h2>記事投稿</h2>
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
<form method="post" action="ActivityReportTransmission.php" enctype="multipart/form-data">
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
<label for="title">題名</label>
|
182
|
+
|
183
|
+
<input type="text" name="title" id="title" size="40" value="<?= h($title); ?>">
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
<label for="upfile">アイキャッチ</label>
|
188
|
+
|
189
|
+
<input type="file" name="upfile" id="upfile">
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
<label for="content">本文</label>
|
194
|
+
|
195
|
+
<textarea name="content" id="content" rows="8" cols="40"><?= h($content); ?></textarea>
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
<input name="submit" type="submit" value="投稿">
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
<?php foreach ($err as $e): ?>
|
204
|
+
|
205
|
+
<p><?= h($e); ?></p>
|
206
|
+
|
207
|
+
<?php endforeach; ?>
|
208
|
+
|
209
|
+
</form>
|
210
|
+
|
211
|
+
</div>
|
212
|
+
|
213
|
+
</body>
|
214
|
+
|
215
|
+
</html>
|
216
|
+
|
217
|
+
```
|