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

回答編集履歴

2

修正

2017/03/01 14:19

投稿

退会済みユーザー
answer CHANGED
@@ -33,6 +33,7 @@
33
33
  PRIMARY KEY (`id`)
34
34
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
35
35
 
36
+ -- password = 'password'
36
37
  INSERT INTO `Users` (`id`, `email`, `password`)
37
38
  VALUES
38
39
  (1,'user@example.com','$2y$10$OqyHPnhhs7nFZGrB/P7wDuN1sZ7olqyc8r3bUTXIKRqg00eJRu/W2');
@@ -51,7 +52,7 @@
51
52
 
52
53
  define('DSN', 'mysql:host=localhost;dbname=sample;charset=utf8');
53
54
  define('USERNAME', 'root');
54
- define('PASSWORD', '3m3tssrr');
55
+ define('PASSWORD', 'password');
55
56
 
56
57
  /**
57
58
  * htmlspecialchars

1

追記

2017/03/01 14:19

投稿

退会済みユーザー
answer CHANGED
@@ -17,4 +17,232 @@
17
17
  error_reporting(E_ALL);
18
18
 
19
19
  require('dbconnect.php');
20
+ ```
21
+
22
+ ---
23
+
24
+ #やっつけサンプル
25
+
26
+ ##SQL
27
+
28
+ ```sql
29
+ CREATE TABLE `Users` (
30
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
31
+ `email` varchar(64) DEFAULT NULL,
32
+ `password` varchar(256) DEFAULT NULL,
33
+ PRIMARY KEY (`id`)
34
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
35
+
36
+ INSERT INTO `Users` (`id`, `email`, `password`)
37
+ VALUES
38
+ (1,'user@example.com','$2y$10$OqyHPnhhs7nFZGrB/P7wDuN1sZ7olqyc8r3bUTXIKRqg00eJRu/W2');
39
+ ```
40
+ ##index.php
41
+
42
+ ```php
43
+ <?php
44
+ /**
45
+ * index.php
46
+ */
47
+ ini_set('display_errors', true);
48
+ error_reporting(E_ALL);
49
+
50
+ session_start();
51
+
52
+ define('DSN', 'mysql:host=localhost;dbname=sample;charset=utf8');
53
+ define('USERNAME', 'root');
54
+ define('PASSWORD', '3m3tssrr');
55
+
56
+ /**
57
+ * htmlspecialchars
58
+ */
59
+ function h($string)
60
+ {
61
+ return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
62
+ }
63
+
64
+ /**
65
+ * SELECT
66
+ */
67
+ function select($sql, $params = [])
68
+ {
69
+ $options = [
70
+ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
71
+ ];
72
+ $objPdo = new PDO(DSN, USERNAME, PASSWORD, $options);
73
+ $stmt = $objPdo->prepare($sql);
74
+ $stmt->execute($params);
75
+ return $stmt->fetchAll();
76
+ }
77
+
78
+ /**
79
+ * ログイン
80
+ */
81
+ function login()
82
+ {
83
+ if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') {
84
+ return;
85
+ }
86
+ $email = filter_input(INPUT_POST, 'email');
87
+ $password = filter_input(INPUT_POST, 'password');
88
+ if (empty($email) || empty($password)) {
89
+ throw new Exception('メールアドレスおよびパスワードは入力必須です。');
90
+ }
91
+
92
+ $sql = 'SELECT * FROM `Users` WHERE `email` = :email ';
93
+ $params = [];
94
+ $params['email'] = $email;
95
+ $rows = select($sql, $params);
96
+
97
+ $user = reset($rows);
98
+ if (count($rows) === 0 || !password_verify($password, $user['password'])) {
99
+ throw new Exception('メールアドレスおよびパスワードが間違っています。');
100
+ }
101
+
102
+ session_regenerate_id(true);
103
+ $_SESSION['login_user'] = $user;
104
+
105
+ return true;
106
+ }
107
+
108
+ try {
109
+ if (login()) {
110
+ header('Location: top.php');
111
+ }
112
+ } catch (Exception $ex) {
113
+ $err = $ex->getMessage();
114
+ }
115
+ ?>
116
+ <!DOCTYPE html>
117
+ <html lang="en">
118
+ <head>
119
+ <meta charset="utf-8">
120
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
121
+ <meta name="viewport" content="width=device-width, initial-scale=1">
122
+
123
+ <title>Signin Template for Bootstrap</title>
124
+
125
+ <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
126
+ <style type="text/css">
127
+ body {
128
+ padding-top: 40px;
129
+ padding-bottom: 40px;
130
+ background-color: #eee;
131
+ }
132
+
133
+ .form-signin {
134
+ max-width: 330px;
135
+ padding: 15px;
136
+ margin: 0 auto;
137
+ }
138
+ .form-signin .form-signin-heading,
139
+ .form-signin .checkbox {
140
+ margin-bottom: 10px;
141
+ }
142
+ .form-signin .checkbox {
143
+ font-weight: normal;
144
+ }
145
+ .form-signin .form-control {
146
+ position: relative;
147
+ height: auto;
148
+ -webkit-box-sizing: border-box;
149
+ -moz-box-sizing: border-box;
150
+ box-sizing: border-box;
151
+ padding: 10px;
152
+ font-size: 16px;
153
+ }
154
+ .form-signin .form-control:focus {
155
+ z-index: 2;
156
+ }
157
+ .form-signin input[type="email"] {
158
+ margin-bottom: -1px;
159
+ border-bottom-right-radius: 0;
160
+ border-bottom-left-radius: 0;
161
+ }
162
+ .form-signin input[type="password"] {
163
+ margin-bottom: 10px;
164
+ border-top-left-radius: 0;
165
+ border-top-right-radius: 0;
166
+ }
167
+ </style>
168
+ </head>
169
+
170
+ <body>
171
+
172
+ <div class="container">
173
+
174
+ <?php if (isset($err)) : ?>
175
+ <div class="alert alert-danger">
176
+ <p>
177
+ <?= h($err); ?>
178
+ </p>
179
+ </div>
180
+ <?php endif; ?>
181
+
182
+ <form class="form-signin" method="post">
183
+ <h2 class="form-signin-heading">Please sign in</h2>
184
+
185
+ <label for="inputEmail" class="sr-only">
186
+ Email address
187
+ </label>
188
+ <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" autofocus>
189
+
190
+ <label for="inputPassword" class="sr-only">
191
+ Password
192
+ </label>
193
+ <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password">
194
+
195
+ <button class="btn btn-lg btn-primary btn-block" type="submit">
196
+ Sign in
197
+ </button>
198
+ </form>
199
+
200
+ </div> <!-- /container -->
201
+ </body>
202
+ </html>
203
+ ```
204
+
205
+ ##top.php
206
+
207
+ ```php
208
+ <?php
209
+ /**
210
+ * top.php
211
+ */
212
+ ini_set('display_errors', true);
213
+ error_reporting(E_ALL);
214
+
215
+ session_start();
216
+
217
+ /**
218
+ * htmlspecialchars
219
+ */
220
+ function h($string)
221
+ {
222
+ return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
223
+ }
224
+
225
+ /**
226
+ * check_login
227
+ */
228
+ function check_login()
229
+ {
230
+ $res = (isset($_SESSION['login_user']) && $_SESSION['login_user'] != NULL);
231
+ if (!$res) {
232
+ header('Location: index.php');
233
+ }
234
+ }
235
+
236
+ check_login();
237
+ ?>
238
+ <!DOCTYPE HTML>
239
+ <html lang="ja">
240
+ <head>
241
+ <meta charset="UTF-8">
242
+ <title></title>
243
+ </head>
244
+ <body>
245
+ <?= h($_SESSION['login_user']['email']); ?>
246
+ </body>
247
+ </html>
20
248
  ```