質問編集履歴

4

説明を追記いたしました。

2018/10/13 07:56

投稿

webillion
webillion

スコア33

test CHANGED
File without changes
test CHANGED
@@ -110,6 +110,4 @@
110
110
 
111
111
 
112
112
 
113
- 追記:二重投稿防止ため、トクン使いましたが、28行目エラーが出てしまい、うまくいきませんでした。
113
+ 追記:すべて処理の前にheader関数で自身にリロかけましたが、二重処理が解決できませんでした。
114
-
115
- よろしくお願い致します。

3

header関数をすべての処理の前に入れましたが、二重リロード問題は防げませんでした。何か間違っているのでしょうか?お手数をおかけして申し訳ございませんが、もし可能であれば何が問題かご教示お願いいたし

2018/10/13 07:56

投稿

webillion
webillion

スコア33

test CHANGED
File without changes
test CHANGED
@@ -13,6 +13,8 @@
13
13
 
14
14
 
15
15
  ```PHP
16
+
17
+ <?php header('http://localhost/php_revise/chapter7/test1_db_insert.php')?>
16
18
 
17
19
  <body>
18
20
 
@@ -36,61 +38,13 @@
36
38
 
37
39
  <?php echo 'こんにちは、', $_SESSION['customer']['name'], 'さん。'; ?>
38
40
 
41
+ <hr>
42
+
39
43
  <?php require '../footer.php'; ?>
40
44
 
41
45
 
42
46
 
43
47
  <?php
44
-
45
-
46
-
47
- // セッションに入れておいたトークンを取得
48
-
49
- $session_token = isset($_SESSION['message']) ? $_SESSION['message'] : '';
50
-
51
-
52
-
53
- // POSTの値からトークンを取得
54
-
55
- $message = isset($_POST['message']) ? $_POST['message'] : '';
56
-
57
-
58
-
59
- // トークンがない場合は不正扱い
60
-
61
- if ($message === '') {
62
-
63
- die("不正な処理ですよ。");
64
-
65
- }
66
-
67
-
68
-
69
- //セッションに入れたトークンとPOSTされたトークンの比較(ここでエラーとなる)
70
-
71
- if ($message !== $session_message) {
72
-
73
- die("不正な処理ですよ。");
74
-
75
- }
76
-
77
-
78
-
79
- // セッションに保存しておいたトークンの削除
80
-
81
- unset($_SESSION['message']);
82
-
83
-
84
-
85
- // セッションの保存
86
-
87
- session_write_close();
88
-
89
- // セッションの再開
90
-
91
- session_start();
92
-
93
-
94
48
 
95
49
 
96
50
 
@@ -124,6 +78,10 @@
124
78
 
125
79
  file_put_contents($file, json_encode($board));
126
80
 
81
+
82
+
83
+
84
+
127
85
  foreach ($board as $message) {
128
86
 
129
87
  echo $today,'<p>', $message, '</p><hr>';
@@ -131,10 +89,6 @@
131
89
  //echo $_POST['message'];
132
90
 
133
91
  }
134
-
135
- header('Location:board-input.php');
136
-
137
- exit;
138
92
 
139
93
  }
140
94
 

2

2重POST対策としてトークンを考慮してみました。コードの中身が変わっているのでもしお時間がありましたらご回答いただけたら幸いですが、

2018/10/13 07:55

投稿

webillion
webillion

スコア33

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,83 @@
14
14
 
15
15
  ```PHP
16
16
 
17
+ <body>
18
+
19
+ <p>投稿するメッセージを入力してください。</p>
20
+
21
+ <form action="board-input.php" method="post">
22
+
23
+ <input type="text" name="message" required>
24
+
25
+ <input type="submit" value="投稿">
26
+
27
+ </form>
28
+
29
+ </body>
30
+
31
+
32
+
33
+ <?php require '../header.php'; ?>
34
+
35
+ <?php session_start(); ?>
36
+
37
+ <?php echo 'こんにちは、', $_SESSION['customer']['name'], 'さん。'; ?>
38
+
39
+ <?php require '../footer.php'; ?>
40
+
41
+
42
+
17
43
  <?php
44
+
45
+
46
+
47
+ // セッションに入れておいたトークンを取得
48
+
49
+ $session_token = isset($_SESSION['message']) ? $_SESSION['message'] : '';
50
+
51
+
52
+
53
+ // POSTの値からトークンを取得
54
+
55
+ $message = isset($_POST['message']) ? $_POST['message'] : '';
56
+
57
+
58
+
59
+ // トークンがない場合は不正扱い
60
+
61
+ if ($message === '') {
62
+
63
+ die("不正な処理ですよ。");
64
+
65
+ }
66
+
67
+
68
+
69
+ //セッションに入れたトークンとPOSTされたトークンの比較(ここでエラーとなる)
70
+
71
+ if ($message !== $session_message) {
72
+
73
+ die("不正な処理ですよ。");
74
+
75
+ }
76
+
77
+
78
+
79
+ // セッションに保存しておいたトークンの削除
80
+
81
+ unset($_SESSION['message']);
82
+
83
+
84
+
85
+ // セッションの保存
86
+
87
+ session_write_close();
88
+
89
+ // セッションの再開
90
+
91
+ session_start();
92
+
93
+
18
94
 
19
95
 
20
96
 
@@ -25,6 +101,10 @@
25
101
  $board=json_decode(file_get_contents($file));
26
102
 
27
103
  }
104
+
105
+
106
+
107
+
28
108
 
29
109
 
30
110
 
@@ -64,32 +144,6 @@
64
144
 
65
145
 
66
146
 
67
- <body>
68
-
69
- <p>投稿するメッセージを入力してください。</p>
70
-
71
- <form action="board-input.php" method="post">
72
-
73
- <input type="text" name="message" required>
74
-
75
- <input type="submit" value="投稿">
76
-
77
- </form>
78
-
79
- </body>
80
-
81
-
82
-
83
- <?php require '../header.php'; ?>
84
-
85
- <?php session_start(); ?>
86
-
87
- <?php echo 'こんにちは、', $_SESSION['customer']['name'], 'さん。'; ?>
88
-
89
- <?php require '../footer.php'; ?>
90
-
91
-
92
-
93
147
  ```
94
148
 
95
149
  ### 試したこと
@@ -100,4 +154,8 @@
100
154
 
101
155
  header関数をコードの何処に記述すればいいのか分かりません。
102
156
 
157
+
158
+
159
+ 追記:二重投稿防止のため、トークンを使いましたが、28行目でエラーが出てしまい、うまくいきませんでした。
160
+
103
161
  よろしくお願い致します。

1

コードを書き直しました。kunaiさん大変恐縮ですが、ご確認お願いできますか?

2018/10/12 08:25

投稿

webillion
webillion

スコア33

test CHANGED
File without changes
test CHANGED
@@ -14,33 +14,7 @@
14
14
 
15
15
  ```PHP
16
16
 
17
- <?php require '../header.php'; ?>
18
-
19
- <?php session_start(); ?>
20
-
21
- <?php echo 'こんにちは、', $_SESSION['customer']['name'], 'さん。'; ?>
22
-
23
-
24
-
25
- <body>
26
-
27
- <p>投稿するメッセージを入力してください。</p>
28
-
29
- <form action="board-input.php" method="post">
30
-
31
- <input type="text" name="message" required>
32
-
33
- <input type="submit" value="投稿">
34
-
35
- </form>
36
-
37
- </body>
38
-
39
-
40
-
41
17
  <?php
42
-
43
-
44
18
 
45
19
 
46
20
 
@@ -51,10 +25,6 @@
51
25
  $board=json_decode(file_get_contents($file));
52
26
 
53
27
  }
54
-
55
-
56
-
57
-
58
28
 
59
29
 
60
30
 
@@ -82,6 +52,10 @@
82
52
 
83
53
  }
84
54
 
55
+ header('Location:board-input.php');
56
+
57
+ exit;
58
+
85
59
  }
86
60
 
87
61
 
@@ -90,15 +64,33 @@
90
64
 
91
65
 
92
66
 
67
+ <body>
93
68
 
69
+ <p>投稿するメッセージを入力してください。</p>
70
+
71
+ <form action="board-input.php" method="post">
72
+
73
+ <input type="text" name="message" required>
74
+
75
+ <input type="submit" value="投稿">
76
+
77
+ </form>
78
+
79
+ </body>
80
+
81
+
82
+
83
+ <?php require '../header.php'; ?>
84
+
85
+ <?php session_start(); ?>
86
+
87
+ <?php echo 'こんにちは、', $_SESSION['customer']['name'], 'さん。'; ?>
94
88
 
95
89
  <?php require '../footer.php'; ?>
96
90
 
97
91
 
98
92
 
99
93
  ```
100
-
101
-
102
94
 
103
95
  ### 試したこと
104
96