質問編集履歴

5

aaa

2020/10/20 16:07

投稿

kamomesaaaaan
kamomesaaaaan

スコア0

test CHANGED
File without changes
test CHANGED
@@ -2,492 +2,310 @@
2
2
 
3
3
 
4
4
 
5
- PHPを使ったログイン機能と投稿フォームを作成しています。
6
-
7
-
8
-
9
- $_SESSION['id']を用いたログイン機能を作りたいのですが、
10
-
11
- ページ遷移の過程で$_SESSIONの中身がNULLになってしまい、
12
-
13
- 思うような挙動になりません。
14
-
15
-
16
-
17
- どのような原因が考えられるでしょうか?
18
-
19
-
20
-
21
-
22
-
23
- 不明
5
+
6
+
7
+
8
+
9
+ ```ini
10
+
11
+
12
+
13
+ [Session]
14
+
15
+ ; Handler used to store/retrieve data.
16
+
17
+ session.save_handler = files
18
+
19
+
20
+
21
+ ; Argument passed to save_handler. In the case of files, this is the path
22
+
23
+ ; where data files are stored. Note: Windows users have to change this
24
+
25
+ ; variable in order to use PHP's session functions.
26
+
27
+ ;
28
+
29
+ ; As of PHP 4.0.1, you can define the path as:
30
+
31
+ ;
32
+
33
+ ; session.save_path = "N;/path"
34
+
35
+ ;
36
+
37
+ ; where N is an integer. Instead of storing all the session files in
38
+
39
+ ; /path, what this will do is use subdirectories N-levels deep, and
40
+
41
+ ; store the session data in those directories. This is useful if you
42
+
43
+ ; or your OS have problems with lots of files in one directory, and is
44
+
45
+ ; a more efficient layout for servers that handle lots of sessions.
46
+
47
+ ;
48
+
49
+ ; NOTE 1: PHP will not create this directory structure automatically.
50
+
51
+ ; You can use the script in the ext/session dir for that purpose.
52
+
53
+ ; NOTE 2: See the section on garbage collection below if you choose to
54
+
55
+ ; use subdirectories for session storage
56
+
57
+ ;
58
+
59
+ ; The file storage module creates files using mode 600 by default.
60
+
61
+ ; You can change that by using
62
+
63
+ ;
64
+
65
+ ; session.save_path = "N;MODE;/path"
66
+
67
+ ;
68
+
69
+ ; where MODE is the octal representation of the mode. Note that this
70
+
71
+ ; does not overwrite the process's umask.
72
+
73
+ ;session.save_path = "/tmp"
74
+
75
+
76
+
77
+ ; Whether to use cookies.
78
+
79
+ session.use_cookies = 1
80
+
81
+
82
+
83
+ ;session.cookie_secure =
84
+
85
+
86
+
87
+ ; This option enables administrators to make their users invulnerable to
88
+
89
+ ; attacks which involve passing session ids in URLs; defaults to 0.
90
+
91
+ ; session.use_only_cookies = 1
92
+
93
+
94
+
95
+ ; Name of the session (used as cookie name).
96
+
97
+ session.name = PHPSESSID
98
+
99
+
100
+
101
+ ; Initialize session on request startup.
102
+
103
+ session.auto_start = 1
104
+
105
+
106
+
107
+ ; Lifetime in seconds of cookie or, if 0, until browser is restarted.
108
+
109
+ session.cookie_lifetime = 0
110
+
111
+
112
+
113
+ ; The path for which the cookie is valid.
114
+
115
+ session.cookie_path = /
116
+
117
+
118
+
119
+ ; The domain for which the cookie is valid.
120
+
121
+ session.cookie_domain =
122
+
123
+
124
+
125
+ ; Whether or not to add the httpOnly flag to the cookie, which makes it inaccessible to browser scripting languages such as JavaScript.
126
+
127
+ session.cookie_httponly =
128
+
129
+
130
+
131
+ ; Handler used to serialize data. php is the standard serializer of PHP.
132
+
133
+ session.serialize_handler = php
134
+
135
+
136
+
137
+ ; Define the probability that the 'garbage collection' process is started
138
+
139
+ ; on every session initialization.
140
+
141
+ ; The probability is calculated by using gc_probability/gc_divisor,
142
+
143
+ ; e.g. 1/100 means there is a 1% chance that the GC process starts
144
+
145
+ ; on each request.
146
+
147
+
148
+
149
+ session.gc_probability = 1
150
+
151
+ session.gc_divisor = 1000
152
+
153
+
154
+
155
+ ; After this number of seconds, stored data will be seen as 'garbage' and
156
+
157
+ ; cleaned up by the garbage collection process.
158
+
159
+ session.gc_maxlifetime = 1440
160
+
161
+
162
+
163
+ ; NOTE: If you are using the subdirectory option for storing session files
164
+
165
+ ; (see session.save_path above), then garbage collection does *not*
166
+
167
+ ; happen automatically. You will need to do your own garbage
168
+
169
+ ; collection through a shell script, cron entry, or some other method.
170
+
171
+ ; For example, the following script would is the equivalent of
172
+
173
+ ; setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
174
+
175
+ ; cd /path/to/sessions; find -cmin +24 | xargs rm
176
+
177
+
178
+
179
+ ; PHP 4.2 and less have an undocumented feature/bug that allows you to
180
+
181
+ ; to initialize a session variable in the global scope, albeit register_globals
182
+
183
+ ; is disabled. PHP 4.3 and later will warn you, if this feature is used.
184
+
185
+ ; You can disable the feature and the warning separately. At this time,
186
+
187
+ ; the warning is only displayed, if bug_compat_42 is enabled.
188
+
189
+
190
+
191
+ session.bug_compat_42 = 0
192
+
193
+ session.bug_compat_warn = 1
194
+
195
+
196
+
197
+ ; Check HTTP Referer to invalidate externally stored URLs containing ids.
198
+
199
+ ; HTTP_REFERER has to contain this substring for the session to be
200
+
201
+ ; considered as valid.
202
+
203
+ session.referer_check =
204
+
205
+
206
+
207
+ ; How many bytes to read from the file.
208
+
209
+ session.entropy_length = 0
210
+
211
+
212
+
213
+ ; Specified here to create the session id.
214
+
215
+ session.entropy_file =
216
+
217
+
218
+
219
+ ;session.entropy_length = 16
220
+
221
+
222
+
223
+ ;session.entropy_file = /dev/urandom
224
+
225
+
226
+
227
+ ; Set to {nocache,private,public,} to determine HTTP caching aspects
228
+
229
+ ; or leave this empty to avoid sending anti-caching headers.
230
+
231
+ session.cache_limiter = nocache
232
+
233
+
234
+
235
+ ; Document expires after n minutes.
236
+
237
+ session.cache_expire = 180
238
+
239
+
240
+
241
+ ; trans sid support is disabled by default.
242
+
243
+ ; Use of trans sid may risk your users security.
244
+
245
+ ; Use this option with caution.
246
+
247
+ ; - User may send URL contains active session ID
248
+
249
+ ; to other person via. email/irc/etc.
250
+
251
+ ; - URL that contains active session ID may be stored
252
+
253
+ ; in publically accessible computer.
254
+
255
+ ; - User may access your site with the same session ID
256
+
257
+ ; always using URL stored in browser's history or bookmarks.
258
+
259
+ session.use_trans_sid = 0
260
+
261
+
262
+
263
+ ; Select a hash function
264
+
265
+ ; 0: MD5 (128 bits)
266
+
267
+ ; 1: SHA-1 (160 bits)
268
+
269
+ session.hash_function = 0
270
+
271
+
272
+
273
+ ; Define how many bits are stored in each character when converting
274
+
275
+ ; the binary hash data to something readable.
276
+
277
+ ;
278
+
279
+ ; 4 bits: 0-9, a-f
280
+
281
+ ; 5 bits: 0-9, a-v
282
+
283
+ ; 6 bits: 0-9, a-z, A-Z, "-", ","
284
+
285
+ session.hash_bits_per_character = 5
286
+
287
+
288
+
289
+ ; The URL rewriter will look for URLs in a defined set of HTML tags.
290
+
291
+ ; form/fieldset are special; if you include them here, the rewriter will
292
+
293
+ ; add a hidden <input> field with the info which is otherwise appended
294
+
295
+ ; to URLs. If you want XHTML conformity, remove the form entry.
296
+
297
+ ; Note that all valid entries require a "=", even if no value follows.
298
+
299
+ url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
300
+
301
+ session.save_path="C:\MAMP\bin\php\sessions\"
302
+
303
+
24
304
 
25
305
 
26
306
 
27
307
  ```
28
308
 
29
- なし $_SESSION['id']がNULLになることによる強制リダイレクトの発生
30
-
31
- ```
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
- ### 該当のソースコード
40
-
41
- ●login.php
42
-
43
-
44
-
45
-
46
-
47
- ```
48
-
49
- <?php
50
-
51
- session_start(); // セッション開始
52
-
53
-
54
-
55
- if (isset($_SESSION['id'])){
56
-
57
- // セッションにユーザIDがある=ログインしている
58
-
59
- // トップページに遷移する
60
-
61
- header('Location: write.php');
62
-
63
- } else if (isset($_POST['name']) && isset($_POST['password'])){
64
-
65
- // ログインしていないがユーザ名とパスワードが送信されたとき
66
-
67
-
68
-
69
- // データベースに接続
70
-
71
- $dsn = 'mysql:host=localhost;dbname=db;charset=utf8';
72
-
73
- $user = 'root';
74
-
75
- $password = 'root';
76
-
77
-
78
-
79
- try {
80
-
81
- $db = new PDO($dsn, $user, $password);
82
-
83
-
84
-
85
- // プリペアドステートメントを作成
86
-
87
- $stmt = $db->prepare(
88
-
89
- "SELECT * FROM table WHERE name=:name AND pass=:pass"
90
-
91
- );
92
-
93
-
94
-
95
- // パラメータを割り当て
96
-
97
- $stmt->bindParam(':name', $_POST['name'], PDO::PARAM_STR);
98
-
99
- $stmt->bindParam(':pass', $_POST['password'], PDO::PARAM_STR);
100
-
101
-
102
-
103
- //クエリの実行
104
-
105
- $stmt->execute();
106
-
107
-
108
-
109
- if ($row = $stmt->fetch()){
110
-
111
- // ユーザが存在していたので、セッションにユーザIDをセット
112
-
113
- $_SESSION['id'] = $row['name'];
114
-
115
- // セッションID再作成
116
-
117
- session_regenerate_id(true);
118
-
119
- header('Location: write.php');
120
-
121
- exit();
122
-
123
- } else {
124
-
125
- // 1レコードも取得できなかったとき
126
-
127
- // ユーザ名・パスワードが間違っている可能性あり
128
-
129
- // もう一度ログインフォームを表示
130
-
131
- header('Location: login.php');
132
-
133
- exit();
134
-
135
- }
136
-
137
- } catch(PDOException $e){
138
-
139
- die('エラー:' . $e->getMessage());
140
-
141
- }
142
-
143
-
144
-
145
- } else {
146
-
147
- // ログインしていない場合はログインフォームを表示する
148
-
149
- ?>
150
-
151
-
152
-
153
- <html>
154
-
155
- <head>
156
-
157
- <meta charset="UTF-8">
158
-
159
- <title>ログイン認証画面</title>
160
-
161
- </head>
162
-
163
- <body>
164
-
165
- <div>
166
-
167
- <div>
168
-
169
- <h1>ログイン認証をしてください。</h1>
170
-
171
- <form>
172
-
173
- <p>ユーザー名<input type="text" name="name"></p>
174
-
175
- <p>パスワード<input type="password" name="password"></p>
176
-
177
- <input type="submit" value="ログイン" />
178
-
179
- </form>
180
-
181
- </div>
182
-
183
- </div>
184
-
185
- </body>
186
-
187
- </html>
188
-
189
- <?php } ?>
190
-
191
- ```
192
-
193
-
194
-
195
-
196
-
197
- ●write.php
198
-
199
-
200
-
201
- ```
202
-
203
- <?php
204
-
205
- session_start();
206
-
207
- if (!isset($_SESSION['id'])){
208
-
209
- header('Location: login.php');
210
-
211
- exit();
212
-
213
- }
214
-
215
- ?>
216
-
217
-
218
-
219
- <?php
220
-
221
- $mode = 'input';
222
-
223
- $errmessage = array();
224
-
225
- if( isset($_POST['back']) && $_POST['back'] ){
226
-
227
- // 何もしない
228
-
229
- } else if( isset($_POST['confirm']) && $_POST['confirm'] ){
230
-
231
- // 確認画面
232
-
233
- if( !$_POST['text'] ) {
234
-
235
- $errmessage[] = "本文を入力してください";
236
-
237
- } else if( mb_strlen($_POST['text']) > 42 ){
238
-
239
- $errmessage[] = "本文は42文字以内で入力してください。";
240
-
241
- }
242
-
243
- $_SESSION['text'] = htmlspecialchars($_POST['text'], ENT_QUOTES);
244
-
245
-
246
-
247
- if( !$_POST['status'] ){
248
-
249
- $errmessage[] = "公開ステータスを選択してください";
250
-
251
- }
252
-
253
-
254
-
255
- $_SESSION['status'] = htmlspecialchars(intval($_POST['status']), ENT_QUOTES);
256
-
257
- if( $errmessage ){
258
-
259
- $mode = 'input';
260
-
261
- } else {
262
-
263
- $token = bin2hex(random_bytes(32));
264
-
265
- $_SESSION['token'] = $token;
266
-
267
- $mode = 'confirm';
268
-
269
- }
270
-
271
- } else if( isset($_POST['send']) && $_POST['send'] ){
272
-
273
- // 送信ボタンを押したとき
274
-
275
- if( !$_POST['token'] || !$_SESSION['token']){
276
-
277
- $errmessage[] = '不正な処理が行われました';
278
-
279
- $_SESSION = array();
280
-
281
- $mode = 'input';
282
-
283
- } else if($_POST['token'] != $_SESSION['token'] ){
284
-
285
- $errmessage[] = '不正な処理が行われました!';
286
-
287
- $_SESSION = array();
288
-
289
- $mode = 'input';
290
-
291
- } else {
292
-
293
- $message = "投稿を完了しました。";
294
-
295
- $_SESSION = array();
296
-
297
- $mode = 'send';
298
-
299
- }
300
-
301
- } else {
302
-
303
- $_SESSION = array();
304
-
305
- }
306
-
307
- ?>
308
-
309
- <!DOCTYPE html>
310
-
311
- <html lang="ja">
312
-
313
- <head>
314
-
315
- <meta charset="utf-8">
316
-
317
- <title>投稿フォーム</title>
318
-
319
- </head>
320
-
321
- <body>
322
-
323
- <div>
324
-
325
- <div>
326
-
327
- <?php if( $mode == 'input' ){ ?>
328
-
329
- <!-- 入力画面 -->
330
-
331
-
332
-
333
- <?php
334
-
335
- if( $errmessage ){
336
-
337
- echo '<div class="alert-danger" role="alert">'; echo implode('<br>', $errmessage );
338
-
339
- echo '</div>';
340
-
341
- }
342
-
343
- ?>
344
-
345
- <form action="./write.php" method="post">
346
-
347
- <h2>NEWSに表示する文章を更新してください。</h2>
348
-
349
- <textarea name="text"></textarea>
350
-
351
- <div>
352
-
353
- <h2>ステータスを選択してください。</h2>
354
-
355
- <div>
356
-
357
-
358
-
359
- <input id="displayButton" type="radio" value="1" name="status" checked></input>
360
-
361
- <input id="hideButton" type="radio" value="2" name="status"></input>
362
-
363
- </div>
364
-
365
- </div>
366
-
367
- <?php
368
-
369
- $_SESSION['token'] = sha1(uniqid(mt_rand(), true));
370
-
371
- ?>
372
-
373
- <input type="hidden" name="token" value="<?php echo $_SESSION['token'];?>">
374
-
375
-
376
-
377
- <input type="submit" name="confirm" value="確認">
378
-
379
-
380
-
381
-
382
-
383
- </form>
384
-
385
- </div>
386
-
387
- </div>
388
-
389
- <?php } else if( $mode == 'confirm' ){ ?>
390
-
391
- <!-- 確認画面 -->
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
- <?php var_dump($_SESSION['id']);?>
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
- <form action="./write.php" method="post">
420
-
421
- <input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>">
422
-
423
- <h4>本文内容</h4> <p><?php echo nl2br($_SESSION['text']) ?><br></p>
424
-
425
- <?php if($_SESSION['status'] == 1) {
426
-
427
- echo "<h4>表示ステータス</h4><p>本文を公開する。</p>";
428
-
429
- } elseif ($_SESSION['status'] == 2) {
430
-
431
- echo "<h4>表示ステータス</h4><p>本文を非公開にする。</p>";
432
-
433
- }
434
-
435
- ?>
436
-
437
-
438
-
439
- <div>
440
-
441
-
442
-
443
- <input type="submit" name="back" value="戻る" />TOPに戻る
444
-
445
- <input type="submit" name="send" value="送信" />送信する。
446
-
447
- </div>
448
-
449
- </form>
450
-
451
- <?php } else { ?>
452
-
453
- <!-- 完了画面 -->
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
- <?php var_dump($_SESSION['id']);?>
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-  <a href="write.php">更新しました。</a>
476
-
477
-
478
-
479
- <?php } ?>
480
-
481
- </body>
482
-
483
- </html>
484
-
485
-
486
-
487
-
488
-
489
- ```
490
-
491
309
 
492
310
 
493
311
  ### 試したこと

4

q

2020/10/20 16:07

投稿

kamomesaaaaan
kamomesaaaaan

スコア0

test CHANGED
File without changes
test CHANGED
@@ -42,6 +42,10 @@
42
42
 
43
43
 
44
44
 
45
+
46
+
47
+ ```
48
+
45
49
  <?php
46
50
 
47
51
  session_start(); // セッション開始
@@ -184,19 +188,13 @@
184
188
 
185
189
  <?php } ?>
186
190
 
187
-
188
-
189
-
190
-
191
191
  ```
192
192
 
193
193
 
194
194
 
195
195
 
196
196
 
197
- ●write.php
197
+ ●write.php
198
-
199
- ```ここに言語を入力
200
198
 
201
199
 
202
200
 

3

a

2020/10/20 13:42

投稿

kamomesaaaaan
kamomesaaaaan

スコア0

test CHANGED
File without changes
test CHANGED
@@ -198,6 +198,10 @@
198
198
 
199
199
  ```ここに言語を入力
200
200
 
201
+
202
+
203
+ ```
204
+
201
205
  <?php
202
206
 
203
207
  session_start();
@@ -482,14 +486,12 @@
482
486
 
483
487
 
484
488
 
489
+
490
+
485
491
  ```
486
492
 
487
493
 
488
494
 
489
- ```
490
-
491
-
492
-
493
495
  ### 試したこと
494
496
 
495
497
 

2

a

2020/10/20 13:41

投稿

kamomesaaaaan
kamomesaaaaan

スコア0

test CHANGED
File without changes
test CHANGED
@@ -32,458 +32,460 @@
32
32
 
33
33
 
34
34
 
35
+
36
+
37
+
38
+
35
39
  ### 該当のソースコード
36
40
 
37
-
38
-
39
- ```PHP
40
-
41
41
  ●login.php
42
42
 
43
+
44
+
45
+ <?php
46
+
47
+ session_start(); // セッション開始
48
+
49
+
50
+
51
+ if (isset($_SESSION['id'])){
52
+
53
+ // セッションにユーザIDがある=ログインしている
54
+
55
+ // トップページに遷移する
56
+
57
+ header('Location: write.php');
58
+
59
+ } else if (isset($_POST['name']) && isset($_POST['password'])){
60
+
61
+ // ログインしていないがユーザ名とパスワードが送信されたとき
62
+
63
+
64
+
65
+ // データベースに接続
66
+
67
+ $dsn = 'mysql:host=localhost;dbname=db;charset=utf8';
68
+
69
+ $user = 'root';
70
+
71
+ $password = 'root';
72
+
73
+
74
+
75
+ try {
76
+
77
+ $db = new PDO($dsn, $user, $password);
78
+
79
+
80
+
81
+ // プリペアドステートメントを作成
82
+
83
+ $stmt = $db->prepare(
84
+
85
+ "SELECT * FROM table WHERE name=:name AND pass=:pass"
86
+
87
+ );
88
+
89
+
90
+
91
+ // パラメータを割り当て
92
+
93
+ $stmt->bindParam(':name', $_POST['name'], PDO::PARAM_STR);
94
+
95
+ $stmt->bindParam(':pass', $_POST['password'], PDO::PARAM_STR);
96
+
97
+
98
+
99
+ //クエリの実行
100
+
101
+ $stmt->execute();
102
+
103
+
104
+
105
+ if ($row = $stmt->fetch()){
106
+
107
+ // ユーザが存在していたので、セッションにユーザIDをセット
108
+
109
+ $_SESSION['id'] = $row['name'];
110
+
111
+ // セッションID再作成
112
+
113
+ session_regenerate_id(true);
114
+
115
+ header('Location: write.php');
116
+
117
+ exit();
118
+
119
+ } else {
120
+
121
+ // 1レコードも取得できなかったとき
122
+
123
+ // ユーザ名・パスワードが間違っている可能性あり
124
+
125
+ // もう一度ログインフォームを表示
126
+
127
+ header('Location: login.php');
128
+
129
+ exit();
130
+
131
+ }
132
+
133
+ } catch(PDOException $e){
134
+
135
+ die('エラー:' . $e->getMessage());
136
+
137
+ }
138
+
139
+
140
+
141
+ } else {
142
+
143
+ // ログインしていない場合はログインフォームを表示する
144
+
145
+ ?>
146
+
147
+
148
+
149
+ <html>
150
+
151
+ <head>
152
+
153
+ <meta charset="UTF-8">
154
+
155
+ <title>ログイン認証画面</title>
156
+
157
+ </head>
158
+
159
+ <body>
160
+
161
+ <div>
162
+
163
+ <div>
164
+
165
+ <h1>ログイン認証をしてください。</h1>
166
+
167
+ <form>
168
+
169
+ <p>ユーザー名<input type="text" name="name"></p>
170
+
171
+ <p>パスワード<input type="password" name="password"></p>
172
+
173
+ <input type="submit" value="ログイン" />
174
+
175
+ </form>
176
+
177
+ </div>
178
+
179
+ </div>
180
+
181
+ </body>
182
+
183
+ </html>
184
+
185
+ <?php } ?>
186
+
187
+
188
+
189
+
190
+
191
+ ```
192
+
193
+
194
+
195
+
196
+
197
+ ●write.php
198
+
43
199
  ```ここに言語を入力
44
200
 
201
+ <?php
202
+
203
+ session_start();
204
+
205
+ if (!isset($_SESSION['id'])){
206
+
207
+ header('Location: login.php');
208
+
209
+ exit();
210
+
211
+ }
212
+
213
+ ?>
214
+
215
+
216
+
45
217
  <?php
46
218
 
47
- session_start(); // セッション開始
48
-
49
-
50
-
51
- if (isset($_SESSION['id'])){
52
-
53
- // セッションにユーザIDがある=ログイン
54
-
55
- // トップページに遷移する
56
-
57
- header('Location: write.php');
58
-
59
- } else if (isset($_POST['name']) && isset($_POST['password'])){
60
-
61
- // ログインしていないがユーザ名とパスワードが送信されたとき
62
-
63
-
64
-
65
- // データベースに接続
66
-
67
- $dsn = 'mysql:host=localhost;dbname=db;charset=utf8';
68
-
69
- $user = 'root';
70
-
71
- $password = 'root';
72
-
73
-
74
-
75
- try {
76
-
77
- $db = new PDO($dsn, $user, $password);
78
-
79
-
80
-
81
- // プリペアドステートメントを作成
82
-
83
- $stmt = $db->prepare(
84
-
85
- "SELECT * FROM table WHERE name=:name AND pass=:pass"
86
-
87
- );
88
-
89
-
90
-
91
- // パラメータを割り当て
92
-
93
- $stmt->bindParam(':name', $_POST['name'], PDO::PARAM_STR);
94
-
95
- $stmt->bindParam(':pass', $_POST['password'], PDO::PARAM_STR);
96
-
97
-
98
-
99
- //クエリの実行
100
-
101
- $stmt->execute();
102
-
103
-
104
-
105
- if ($row = $stmt->fetch()){
106
-
107
- // ユーザが存在していたので、セッションにユーザIDをセット
108
-
109
- $_SESSION['id'] = $row['name'];
110
-
111
- // セッションID再作成
112
-
113
- session_regenerate_id(true);
114
-
115
- header('Location: write.php');
116
-
117
- exit();
118
-
119
- } else {
120
-
121
- // 1レコードも取得できなかったとき
122
-
123
- // ユーザ名・パスワードが間違っている可能性あり
124
-
125
- // もう一度ログインフォームを表示
126
-
127
- header('Location: login.php');
128
-
129
- exit();
130
-
131
- }
132
-
133
- } catch(PDOException $e){
134
-
135
- die('エラー:' . $e->getMessage());
136
-
137
- }
138
-
139
-
219
+ $mode = 'input';
220
+
221
+ $errmessage = array();
222
+
223
+ if( isset($_POST['back']) && $_POST['back'] ){
224
+
225
+ // 何も
226
+
227
+ } else if( isset($_POST['confirm']) && $_POST['confirm'] ){
228
+
229
+ // 確認画面
230
+
231
+ if( !$_POST['text'] ) {
232
+
233
+ $errmessage[] = "本文を入力してくださ";
234
+
235
+ } else if( mb_strlen($_POST['text']) > 42 ){
236
+
237
+ $errmessage[] = "本文は42文字以内で入力してください。";
238
+
239
+ }
240
+
241
+ $_SESSION['text'] = htmlspecialchars($_POST['text'], ENT_QUOTES);
242
+
243
+
244
+
245
+ if( !$_POST['status'] ){
246
+
247
+ $errmessage[] = "公開ステータスを選択してください";
248
+
249
+ }
250
+
251
+
252
+
253
+ $_SESSION['status'] = htmlspecialchars(intval($_POST['status']), ENT_QUOTES);
254
+
255
+ if( $errmessage ){
256
+
257
+ $mode = 'input';
258
+
259
+ } else {
260
+
261
+ $token = bin2hex(random_bytes(32));
262
+
263
+ $_SESSION['token'] = $token;
264
+
265
+ $mode = 'confirm';
266
+
267
+ }
268
+
269
+ } else if( isset($_POST['send']) && $_POST['send'] ){
270
+
271
+ // 送信ボタンを押したとき
272
+
273
+ if( !$_POST['token'] || !$_SESSION['token']){
274
+
275
+ $errmessage[] = '不正な処理が行われました';
276
+
277
+ $_SESSION = array();
278
+
279
+ $mode = 'input';
280
+
281
+ } else if($_POST['token'] != $_SESSION['token'] ){
282
+
283
+ $errmessage[] = '不正な処理が行われました!';
284
+
285
+ $_SESSION = array();
286
+
287
+ $mode = 'input';
140
288
 
141
289
  } else {
142
290
 
143
- // ログインていない場合はログインフォームを表示する
291
+ $message = "投稿を完了ました。";
292
+
293
+ $_SESSION = array();
294
+
295
+ $mode = 'send';
296
+
297
+ }
298
+
299
+ } else {
300
+
301
+ $_SESSION = array();
302
+
303
+ }
144
304
 
145
305
  ?>
146
306
 
147
-
148
-
149
- <html>
307
+ <!DOCTYPE html>
308
+
309
+ <html lang="ja">
150
310
 
151
311
  <head>
152
312
 
153
- <meta charset="UTF-8">
313
+ <meta charset="utf-8">
154
-
314
+
155
- <title>ログイン認証画面</title>
315
+ <title>投稿フォーム</title>
156
316
 
157
317
  </head>
158
318
 
159
319
  <body>
160
320
 
321
+ <div>
322
+
161
323
  <div>
162
324
 
325
+ <?php if( $mode == 'input' ){ ?>
326
+
327
+ <!-- 入力画面 -->
328
+
329
+
330
+
331
+ <?php
332
+
333
+ if( $errmessage ){
334
+
335
+ echo '<div class="alert-danger" role="alert">'; echo implode('<br>', $errmessage );
336
+
337
+ echo '</div>';
338
+
339
+ }
340
+
341
+ ?>
342
+
343
+ <form action="./write.php" method="post">
344
+
345
+ <h2>NEWSに表示する文章を更新してください。</h2>
346
+
347
+ <textarea name="text"></textarea>
348
+
163
349
  <div>
164
350
 
165
- <h1>ログイン認証をしてください。</h1>
351
+ <h2>ステータス選択してください。</h2>
166
-
352
+
167
- <form>
353
+ <div>
354
+
355
+
356
+
168
-
357
+ <input id="displayButton" type="radio" value="1" name="status" checked></input>
358
+
169
- <p>ユーザー名<input type="text" name="name"></p>
359
+ <input id="hideButton" type="radio" value="2" name="status"></input>
170
-
171
- <p>パスワード<input type="password" name="password"></p>
360
+
172
-
173
- <input type="submit" value="ログイン" />
174
-
175
- </form>
361
+ </div>
176
362
 
177
363
  </div>
178
364
 
365
+ <?php
366
+
367
+ $_SESSION['token'] = sha1(uniqid(mt_rand(), true));
368
+
369
+ ?>
370
+
371
+ <input type="hidden" name="token" value="<?php echo $_SESSION['token'];?>">
372
+
373
+
374
+
375
+ <input type="submit" name="confirm" value="確認">
376
+
377
+
378
+
379
+
380
+
381
+ </form>
382
+
179
383
  </div>
180
384
 
385
+ </div>
386
+
387
+ <?php } else if( $mode == 'confirm' ){ ?>
388
+
389
+ <!-- 確認画面 -->
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+ <?php var_dump($_SESSION['id']);?>
402
+
403
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+ <form action="./write.php" method="post">
418
+
419
+ <input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>">
420
+
421
+ <h4>本文内容</h4> <p><?php echo nl2br($_SESSION['text']) ?><br></p>
422
+
423
+ <?php if($_SESSION['status'] == 1) {
424
+
425
+ echo "<h4>表示ステータス</h4><p>本文を公開する。</p>";
426
+
427
+ } elseif ($_SESSION['status'] == 2) {
428
+
429
+ echo "<h4>表示ステータス</h4><p>本文を非公開にする。</p>";
430
+
431
+ }
432
+
433
+ ?>
434
+
435
+
436
+
437
+ <div>
438
+
439
+
440
+
441
+ <input type="submit" name="back" value="戻る" />TOPに戻る
442
+
443
+ <input type="submit" name="send" value="送信" />送信する。
444
+
445
+ </div>
446
+
447
+ </form>
448
+
449
+ <?php } else { ?>
450
+
451
+ <!-- 完了画面 -->
452
+
453
+
454
+
455
+
456
+
457
+
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+ <?php var_dump($_SESSION['id']);?>
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+  <a href="write.php">更新しました。</a>
474
+
475
+
476
+
477
+ <?php } ?>
478
+
181
479
  </body>
182
480
 
183
481
  </html>
184
482
 
185
- <?php } ?>
186
-
187
-
188
-
189
483
 
190
484
 
191
485
  ```
192
486
 
193
487
 
194
488
 
195
-
196
-
197
- ●write.php
198
-
199
- ```ここに言語を入力
200
-
201
- <?php
202
-
203
- session_start();
204
-
205
- if (!isset($_SESSION['id'])){
206
-
207
- header('Location: login.php');
208
-
209
- exit();
210
-
211
- }
212
-
213
- ?>
214
-
215
-
216
-
217
- <?php
218
-
219
- $mode = 'input';
220
-
221
- $errmessage = array();
222
-
223
- if( isset($_POST['back']) && $_POST['back'] ){
224
-
225
- // 何もしない
226
-
227
- } else if( isset($_POST['confirm']) && $_POST['confirm'] ){
228
-
229
- // 確認画面
230
-
231
- if( !$_POST['text'] ) {
232
-
233
- $errmessage[] = "本文を入力してください";
234
-
235
- } else if( mb_strlen($_POST['text']) > 42 ){
236
-
237
- $errmessage[] = "本文は42文字以内で入力してください。";
238
-
239
- }
240
-
241
- $_SESSION['text'] = htmlspecialchars($_POST['text'], ENT_QUOTES);
242
-
243
-
244
-
245
- if( !$_POST['status'] ){
246
-
247
- $errmessage[] = "公開ステータスを選択してください";
248
-
249
- }
250
-
251
-
252
-
253
- $_SESSION['status'] = htmlspecialchars(intval($_POST['status']), ENT_QUOTES);
254
-
255
- if( $errmessage ){
256
-
257
- $mode = 'input';
258
-
259
- } else {
260
-
261
- $token = bin2hex(random_bytes(32));
262
-
263
- $_SESSION['token'] = $token;
264
-
265
- $mode = 'confirm';
266
-
267
- }
268
-
269
- } else if( isset($_POST['send']) && $_POST['send'] ){
270
-
271
- // 送信ボタンを押したとき
272
-
273
- if( !$_POST['token'] || !$_SESSION['token']){
274
-
275
- $errmessage[] = '不正な処理が行われました';
276
-
277
- $_SESSION = array();
278
-
279
- $mode = 'input';
280
-
281
- } else if($_POST['token'] != $_SESSION['token'] ){
282
-
283
- $errmessage[] = '不正な処理が行われました!';
284
-
285
- $_SESSION = array();
286
-
287
- $mode = 'input';
288
-
289
- } else {
290
-
291
- $message = "投稿を完了しました。";
292
-
293
- $_SESSION = array();
294
-
295
- $mode = 'send';
296
-
297
- }
298
-
299
- } else {
300
-
301
- $_SESSION = array();
302
-
303
- }
304
-
305
- ?>
306
-
307
- <!DOCTYPE html>
308
-
309
- <html lang="ja">
310
-
311
- <head>
312
-
313
- <meta charset="utf-8">
314
-
315
- <title>投稿フォーム</title>
316
-
317
- </head>
318
-
319
- <body>
320
-
321
- <div>
322
-
323
- <div>
324
-
325
- <?php if( $mode == 'input' ){ ?>
326
-
327
- <!-- 入力画面 -->
328
-
329
-
330
-
331
- <?php
332
-
333
- if( $errmessage ){
334
-
335
- echo '<div class="alert-danger" role="alert">'; echo implode('<br>', $errmessage );
336
-
337
- echo '</div>';
338
-
339
- }
340
-
341
- ?>
342
-
343
- <form action="./write.php" method="post">
344
-
345
- <h2>NEWSに表示する文章を更新してください。</h2>
346
-
347
- <textarea name="text"></textarea>
348
-
349
- <div>
350
-
351
- <h2>ステータスを選択してください。</h2>
352
-
353
- <div>
354
-
355
-
356
-
357
- <input id="displayButton" type="radio" value="1" name="status" checked></input>
358
-
359
- <input id="hideButton" type="radio" value="2" name="status"></input>
360
-
361
- </div>
362
-
363
- </div>
364
-
365
- <?php
366
-
367
- $_SESSION['token'] = sha1(uniqid(mt_rand(), true));
368
-
369
- ?>
370
-
371
- <input type="hidden" name="token" value="<?php echo $_SESSION['token'];?>">
372
-
373
-
374
-
375
- <input type="submit" name="confirm" value="確認">
376
-
377
-
378
-
379
-
380
-
381
- </form>
382
-
383
- </div>
384
-
385
- </div>
386
-
387
- <?php } else if( $mode == 'confirm' ){ ?>
388
-
389
- <!-- 確認画面 -->
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
- <?php var_dump($_SESSION['id']);?>
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
- <form action="./write.php" method="post">
418
-
419
- <input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>">
420
-
421
- <h4>本文内容</h4> <p><?php echo nl2br($_SESSION['text']) ?><br></p>
422
-
423
- <?php if($_SESSION['status'] == 1) {
424
-
425
- echo "<h4>表示ステータス</h4><p>本文を公開する。</p>";
426
-
427
- } elseif ($_SESSION['status'] == 2) {
428
-
429
- echo "<h4>表示ステータス</h4><p>本文を非公開にする。</p>";
430
-
431
- }
432
-
433
- ?>
434
-
435
-
436
-
437
- <div>
438
-
439
-
440
-
441
- <input type="submit" name="back" value="戻る" />TOPに戻る
442
-
443
- <input type="submit" name="send" value="送信" />送信する。
444
-
445
- </div>
446
-
447
- </form>
448
-
449
- <?php } else { ?>
450
-
451
- <!-- 完了画面 -->
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
- <?php var_dump($_SESSION['id']);?>
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-  <a href="write.php">更新しました。</a>
474
-
475
-
476
-
477
- <?php } ?>
478
-
479
- </body>
480
-
481
- </html>
482
-
483
-
484
-
485
-
486
-
487
489
  ```
488
490
 
489
491
 

1

a

2020/10/20 13:39

投稿

kamomesaaaaan
kamomesaaaaan

スコア0

test CHANGED
File without changes
test CHANGED
@@ -40,6 +40,8 @@
40
40
 
41
41
  ●login.php
42
42
 
43
+ ```ここに言語を入力
44
+
43
45
  <?php
44
46
 
45
47
  session_start(); // セッション開始
@@ -186,13 +188,15 @@
186
188
 
187
189
 
188
190
 
191
+ ```
192
+
189
193
 
190
194
 
191
195
 
192
196
 
193
197
  ●write.php
194
198
 
195
-
199
+ ```ここに言語を入力
196
200
 
197
201
  <?php
198
202
 
@@ -480,6 +484,8 @@
480
484
 
481
485
 
482
486
 
487
+ ```
488
+
483
489
 
484
490
 
485
491
  ### 試したこと