質問するログイン新規登録

質問編集履歴

1

登録処理resister.phpのコード追加

2020/01/14 15:18

投稿

KazKit
KazKit

スコア8

title CHANGED
File without changes
body CHANGED
@@ -116,4 +116,75 @@
116
116
 
117
117
  コード中盤あたりでfetchしたデータを$userに格納してvar_dumpすると、ちゃんと入力されて照合された正しいデータが入ってきていますが、その後の$stmtのvar_dumpではうまくデータが渡っておらず、困っています。
118
118
 
119
- どこが間違っているのでしょうか。
119
+ どこが間違っているのでしょうか。
120
+
121
+ 追記
122
+ ```resister.php
123
+ <?php
124
+
125
+ $errorMessageEmail = '';
126
+ $errorMessagePass = '';
127
+
128
+ if (isset($_POST['submit'])) {
129
+
130
+ if ($_POST['email'] === '') {
131
+ $errorMessageEmail = ' Fill in the E-mail address';
132
+ } elseif($_POST['password'] === '') {
133
+ $errorMessagePass = ' Fill in the Password';
134
+ }
135
+
136
+ if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
137
+ $incorrectEmail = 'Your email type is not correct';
138
+ }
139
+
140
+ if (!preg_match('/\A[a-zA-Z0-9]+\z/', $_POST['password'])) {
141
+ $incorrectPass = 'Your Password type is not correct';
142
+ }
143
+
144
+ // if (!isset($_SESSION['token'])) {
145
+ // $_SESSION['token'] = bin2hex(openssl_random_pseudo_bytes(16));
146
+ // }
147
+
148
+ // if (!isset($_POST['token']) || $_POST['token'] !== $_SESSION['token']) {
149
+ // echo 'Bad token';
150
+ // exit;
151
+ // }
152
+ // else {
153
+ // header('Location: ' . $_SERVER['HTTP_HOST']);
154
+ // }
155
+
156
+ if (!empty($_POST['email']) && !empty($_POST["password"])) {
157
+ $email = $_POST['email'];
158
+ $password = $_POST['password'];
159
+
160
+ try{
161
+ //DB接続
162
+ $db = new \PDO(PDO_DSN,DB_USERNAME,DB_PASSWORD);
163
+ //エラーをスロー
164
+ $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
165
+
166
+ if (filter_var($email, FILTER_VALIDATE_EMAIL) &&
167
+ preg_match('/\A[a-zA-Z0-9]+\z/', $password)) {
168
+
169
+ $stmt = $db->prepare("INSERT INTO users (email, password, created, modified)
170
+ VALUES (:email, :password, now(), now())");
171
+
172
+ $stmt->execute([
173
+ ':email' => $email,
174
+ ':password' => password_hash($password, PASSWORD_DEFAULT)
175
+ ]);
176
+ }
177
+
178
+
179
+ } catch(\PDOException $e){
180
+ echo $e->getMessage();
181
+ exit;
182
+ }
183
+
184
+ header('Location: ' . SITE_URL . '/login.php');
185
+ exit;
186
+ }
187
+
188
+ }
189
+
190
+ ```