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

質問編集履歴

3

修正

2017/12/06 00:21

投稿

dog57
dog57

スコア131

title CHANGED
File without changes
body CHANGED
@@ -91,48 +91,6 @@
91
91
 
92
92
  ```
93
93
 
94
- 遷移先 login_admin.php
95
- ```php
96
- <?php
97
- // 開発時にこれを書かないのはNG
98
- ini_set('display_errors', true);
99
- error_reporting(E_ALL);
100
-
101
- session_start();
102
- header("Content-type: text/html; charset=utf-8");
103
-
104
- $username = '';
105
-
106
-
107
- // ログイン状態のチェック
108
- if (!isset($_SESSION["username"])) {
109
- header("Location: login_form.php");
110
- exit();
111
- }
112
-
113
- function h($string) {
114
- return htmlspecialchars($string, ENT_QUOTES, "UTF-8");
115
- }
116
-
117
- ?>
118
-
119
- <!DOCTYPE html>
120
- <html lang="ja">
121
- <head>
122
- <meta charset="utf-8">
123
- <title>ログイン</title>
124
- </head>
125
- <body>
126
-
127
- <?php echo h($username)."さん、こんにちは!"; ?>
128
-
129
- <a href="logout.php">ログアウトする</a>
130
-
131
- </body>
132
- </html>
133
-
134
- ```
135
-
136
94
  login_check.php (チェック) html省略
137
95
  ```php
138
96
  <?php
@@ -253,4 +211,46 @@
253
211
  }
254
212
 
255
213
  ?>
214
+ ```
215
+
216
+ 遷移先 login_admin.php
217
+ ```php
218
+ <?php
219
+ // 開発時にこれを書かないのはNG
220
+ ini_set('display_errors', true);
221
+ error_reporting(E_ALL);
222
+
223
+ session_start();
224
+ header("Content-type: text/html; charset=utf-8");
225
+
226
+ $username = '';
227
+
228
+
229
+ // ログイン状態のチェック
230
+ if (!isset($_SESSION["username"])) {
231
+ header("Location: login_form.php");
232
+ exit();
233
+ }
234
+
235
+ function h($string) {
236
+ return htmlspecialchars($string, ENT_QUOTES, "UTF-8");
237
+ }
238
+
239
+ ?>
240
+
241
+ <!DOCTYPE html>
242
+ <html lang="ja">
243
+ <head>
244
+ <meta charset="utf-8">
245
+ <title>ログイン</title>
246
+ </head>
247
+ <body>
248
+
249
+ <?php echo h($username)."さん、こんにちは!"; ?>
250
+
251
+ <a href="logout.php">ログアウトする</a>
252
+
253
+ </body>
254
+ </html>
255
+
256
256
  ```

2

login_check.php (チェック) の追記

2017/12/06 00:21

投稿

dog57
dog57

スコア131

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  どなかたご教授いただけると嬉しいです。
9
9
 
10
- login_form.php (画像の画面)
10
+ login_form.php (画像の画面) htmlは一部抜粋
11
11
  ```php
12
12
  <?php
13
13
  // 開発時にこれを書かないのはNG
@@ -29,23 +29,7 @@
29
29
 
30
30
  ?>
31
31
 
32
-
33
- <!DOCTYPE html>
34
- <html lang="ja">
35
- <head>
36
- <meta charset="utf-8">
37
- <title>ログインフォーム</title>
38
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
39
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
40
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
41
-
42
-
43
- </head>
44
- <body>
45
-
46
-
47
-
48
- <!-- パンくずリスト-->
32
+ <!-- パンくずリスト-->
49
33
  <ol class="breadcrumb">
50
34
  <li><a href="http://192.168.33.15"><span class="glyphicon glyphicon-home" aria-hidden="true"></span>トップ</a></li>
51
35
  <li class="active">ログインフォーム</li>
@@ -105,108 +89,6 @@
105
89
  </div>
106
90
 
107
91
 
108
- <div class="form-group">
109
- <div class="col-md-12 control">
110
- <div style="border-top: 1px solid#888; padding-top:15px; font-size:85%" >
111
- Don't have an account!
112
- <a href="#" onClick="$('#loginbox').hide(); $('#signupbox').show()">
113
- Sign Up Here
114
- </a>
115
- </div>
116
- </div>
117
- </div>
118
- </form>
119
-
120
-
121
-
122
- </div>
123
- </div>
124
- </div>
125
- <div id="signupbox" style="display:none; margin-top:50px" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
126
- <div class="panel panel-info">
127
- <div class="panel-heading">
128
- <div class="panel-title">Sign Up</div>
129
- <div style="float:right; font-size: 85%; position: relative; top:-10px"><a id="signinlink" href="#" onclick="$('#signupbox').hide(); $('#loginbox').show()">Sign In</a></div>
130
- </div>
131
- <div class="panel-body" >
132
- <form id="signupform" class="form-horizontal" role="form">
133
-
134
- <div id="signupalert" style="display:none" class="alert alert-danger">
135
- <p>Error:</p>
136
- <span></span>
137
- </div>
138
-
139
-
140
-
141
- <div class="form-group">
142
- <label for="email" class="col-md-3 control-label">Email</label>
143
- <div class="col-md-9">
144
- <input type="text" class="form-control" name="email" placeholder="Email Address">
145
- </div>
146
- </div>
147
-
148
- <div class="form-group">
149
- <label for="firstname" class="col-md-3 control-label">First Name</label>
150
- <div class="col-md-9">
151
- <input type="text" class="form-control" name="firstname" placeholder="First Name">
152
- </div>
153
- </div>
154
- <div class="form-group">
155
- <label for="lastname" class="col-md-3 control-label">Last Name</label>
156
- <div class="col-md-9">
157
- <input type="text" class="form-control" name="lastname" placeholder="Last Name">
158
- </div>
159
- </div>
160
- <div class="form-group">
161
- <label for="password" class="col-md-3 control-label">Password</label>
162
- <div class="col-md-9">
163
- <input type="password" class="form-control" name="passwd" placeholder="Password">
164
- </div>
165
- </div>
166
-
167
- <div class="form-group">
168
- <label for="icode" class="col-md-3 control-label">Invitation Code</label>
169
- <div class="col-md-9">
170
- <input type="text" class="form-control" name="icode" placeholder="">
171
- </div>
172
- </div>
173
-
174
- <div class="form-group">
175
- <!-- Button -->
176
- <div class="col-md-offset-3 col-md-9">
177
- <button id="btn-signup" type="button" class="btn btn-info"><i class="icon-hand-right"></i> &nbsp Sign Up</button>
178
- <span style="margin-left:8px;">or</span>
179
- </div>
180
- </div>
181
-
182
- <div style="border-top: 1px solid #999; padding-top:20px" class="form-group">
183
-
184
- <div class="col-md-offset-3 col-md-9">
185
- <button id="btn-fbsignup" type="button" class="btn btn-primary"><i class="icon-facebook"></i> Sign Up with Facebook</button>
186
- </div>
187
-
188
- </div>
189
-
190
-
191
-
192
- </form>
193
- </div>
194
- </div>
195
-
196
-
197
-
198
-
199
- </div>
200
- </div>
201
-
202
-
203
-
204
-
205
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
206
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
207
- </body>
208
- </html>
209
-
210
92
  ```
211
93
 
212
94
  遷移先 login_admin.php
@@ -249,4 +131,126 @@
249
131
  </body>
250
132
  </html>
251
133
 
134
+ ```
135
+
136
+ login_check.php (チェック) html省略
137
+ ```php
138
+ <?php
139
+ // 開発時にこれを書かないのはNG
140
+ ini_set('display_errors', true);
141
+ error_reporting(E_ALL);
142
+
143
+ session_start();
144
+ header("Content-type: text/html; charset=utf-8");
145
+
146
+ // クロスサイトリクエストフォージュリ(CSRF)対策のトークン判定
147
+ if ($_POST["token"] != $_SESSION["token"]) {
148
+ echo "不正アクセスの可能性あり。";
149
+ exit();
150
+ }
151
+
152
+ // クリックジャッキング対策
153
+ header("X-FRAME-OPTIONS: SAMEORIGIN");
154
+
155
+ // 変数の初期化
156
+ $email = '';
157
+
158
+ // セッション変数の定義
159
+
160
+ // エラーメッセージの配列の初期化
161
+ $errors = array();
162
+ // セッション変数の初期化
163
+ $_SESSION['errors'] = array();
164
+
165
+ // データベース接続
166
+ require_once(__DIR__. '/db.php');
167
+
168
+ // 前後にある半角全角スペースを削除する関数
169
+ function spaceTrim($str) {
170
+
171
+ // 行頭
172
+ $str = preg_replace('/^[ ]+/u', '', $str);
173
+ // 末尾
174
+ $str = preg_replace('/[ ]+$/u', '', $str);
175
+
176
+ return $str;
177
+ }
178
+
179
+ // エラーメッセージの初期化
180
+ $errors = array();
181
+
182
+ // POST時
183
+ if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') == "POST") {
184
+
185
+ // POSTされたデータを各変数に入れる
186
+ $username = filter_input(INPUT_POST, 'username');
187
+ $password = filter_input(INPUT_POST, 'password');
188
+
189
+ // 前後にある半角全角スペースを削除
190
+ $username = spaceTrim($username);
191
+ $password = spaceTrim($password);
192
+
193
+ // アカウント入力判定
194
+ if (empty($_SESSION["username"])) {
195
+ $errors[] = "usernameもしくは、emailが入力されていません。";
196
+ }
197
+
198
+ // パスワード入力判定
199
+ if (empty($_SESSION["password"])) {
200
+ $errors[] = "パスワードが入力されていません。";
201
+ } elseif (preg_match('/^[0-9a-zA-Z]{1,7}$/', $password)) {
202
+ $errors[] = "パスワードは8文字以上で入力してください。";
203
+ } else {
204
+ $password_hide = str_repeat('*', strlen($password));
205
+ }
206
+ }
207
+
208
+ // エラーがなければ実行する
209
+ if (count($errors) == 0) {
210
+ try {
211
+ // 例外処理を投げる(スロー)ようにする
212
+ $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
213
+
214
+ // アカウントで検索
215
+ $statement = $dbh->prepare("SELECT * FROM users WHERE email=(:email)");
216
+ $statement->bindValue(":email", $email, PDO::PARAM_STR);
217
+ $statement->execute();
218
+
219
+ // アカウントが一致
220
+ if ($row = $statement->fetch()) {
221
+ $db_password = $row["password"];
222
+
223
+ // パスワードが一致
224
+ if ($password === $db_password) {
225
+
226
+ // セッションハイジャック対策
227
+ session_regenerate_id(true);
228
+
229
+ $_SESSION["username"] = $username;
230
+ header("Location: login_admin.php");
231
+ exit();
232
+ } else {
233
+ $errors[] = "アカウント及びパスワードが一致しません。";
234
+ }
235
+ } else {
236
+ $errors[] = "アカウント及びパスワードが一致しません。";
237
+ }
238
+
239
+ if (count($errors) > 0) {
240
+ $errors = $_SESSION["errors"];
241
+ foreach ($errors as $value) {
242
+ echo $error;
243
+ }
244
+ header("Location: login_form.php");
245
+ }
246
+
247
+ // データベース切断
248
+ $dbh = null;
249
+ } catch (PDOException $e) {
250
+ print("Error:" .$e->getMessage());
251
+ die();
252
+ }
253
+ }
254
+
255
+ ?>
252
256
  ```

1

追記

2017/12/06 00:16

投稿

dog57
dog57

スコア131

title CHANGED
File without changes
body CHANGED
File without changes