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

質問編集履歴

2

脱字

2020/11/27 12:06

投稿

kqosu19
kqosu19

スコア0

title CHANGED
File without changes
body CHANGED
@@ -12,10 +12,12 @@
12
12
  ### 該当のソースコード
13
13
  login_form.php
14
14
 
15
-
16
15
  <?php
17
16
  session_start();
18
17
 
18
+ ini_set( 'display_errors', 1);
19
+ ini_set( 'error_reporting', E_ALL );
20
+
19
21
  require_once '../classes/UserLogic.php';
20
22
 
21
23
  $result = UserLogic::checkLogin();
@@ -30,10 +32,316 @@
30
32
  session_destroy();
31
33
  ?>
32
34
 
35
+ <!DOCTYPE html>
36
+ <html lang="en">
37
+ <head>
38
+ <meta charset="UTF-8">
39
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
40
+ <title>ログイン画面</title>
41
+ </head>
42
+ <body>
43
+ <div>
44
+ <img src="../img00/travel.jpg">
45
+ <h2>ログインフォーム</h2>
46
+ <?php if (isset($err['msg'])) : ?>
47
+ <p><?php echo $err['msg']; ?></p>
48
+ <?php endif; ?>
49
+ <form action="../upload/upload_form.php" method="POST">
50
+ <div class="email">
51
+ <p>
52
+ <label for="email">メールアドレス:</label>
53
+ <input type="email" name ="email">
54
+ <?php if (isset($err['email'])) : ?>
55
+ <p><?php echo $err['email']; ?></p>
56
+ <?php endif; ?>
57
+ </p>
58
+ </div>
59
+ <p>
60
+ <label for="username">パスワード:</label>
61
+ <input type="text" name ="password">
62
+ <?php if (isset($err['password'])) : ?>
63
+ <p><?php echo $err['password']; ?></p>
64
+ <?php endif; ?>
65
+ </p>
66
+ <p>
67
+ <input type="submit" value="ログイン">
68
+ </p>
69
+ </form>
70
+ <a href="signup_form.php">新規登録はこちら</a>
71
+ <p></p>
72
+ </div>
73
+ <style>
74
+ img {margin-top: 10px;}
75
+ div {text-align: center;}
76
+ .email {margin-right: 37px;}
77
+ body {background-color: #CCFFFF;}
78
+ h2, label, input {margin-top: 20px;}
79
+ </style>
80
+ </body>
81
+ </html>
33
82
 
83
+ upload_form.php
84
+
85
+ <?php
86
+ ini_set( 'display_errors', 1);
87
+ ini_set( 'error_reporting', E_ALL );
88
+
89
+
90
+ session_start();
91
+ // フォーム
92
+
93
+ require_once '../classes/UserLogic.php';
94
+ require_once '../dbc/dbc.php';
95
+ require_once "dbc.php";
96
+ $files = getAllFile();
97
+
98
+ //エラーメッセージ
99
+ $err = [];
100
+
101
+ //バリデーション
102
+ if(!$email = filter_input(INPUT_POST, 'email')) {
103
+ $err['email'] = 'メールアドレスを記入してください。';
104
+ }
105
+ if(!$password = filter_input(INPUT_POST, 'password')
106
+ ) {
107
+ $err['password'] = 'パスワードを記入してください。';
108
+ }
109
+
110
+ if (count($err) > 0) {
111
+ // エラーがあった場合は戻す
112
+ $_SESSION = $err;
113
+ header('Location: ../public/login_form.php');
114
+ return;
115
+ }
116
+ //ログイン成功時の処理
117
+ $result = UserLogic::login($email, $password);
118
+ //ログイン失敗時の処理
119
+ if (!$result) {
120
+ header('Location: ../public/login_form.php');
121
+ return;
122
+ }
123
+
124
+ ?>
125
+
126
+
127
+ <!-- ①フォームの説明 -->
128
+ <!-- ②$_FILEの確認 -->
129
+ <!-- ③バリデーション -->
130
+ <!DOCTYPE html>
131
+ <html lang="ja">
132
+ <head>
133
+ <meta charset="UTF-8" />
134
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
135
+ <title>アップロードフォーム</title>
136
+ </head>
137
+ <style>
138
+ body {
139
+ padding: 30px;
140
+ margin: 0 auto;
141
+ width: 50%;
142
+ background-color: #CCFFFF;
143
+ }
144
+ textarea {
145
+ width: 98%;
146
+ height: 60px;
147
+ }
148
+ .file-up {
149
+ margin-bottom: 10px;
150
+ }
151
+ .submit {
152
+ text-align: right;
153
+ }
154
+ .btn {
155
+ display: inline-block;
156
+ border-radius: 3px;
157
+ font-size: 18px;
158
+ background: #67c5ff;
159
+ border: 2px solid #67c5ff;
160
+ padding: 5px 10px;
161
+ color: #fff;
162
+ cursor: pointer;
163
+ }
164
+ </style>
165
+
166
+ <body>
167
+ <form enctype="multipart/form-data" action="./file_upload.php" method="POST">
168
+ <div class="file-up">
169
+ <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
170
+ <input name="img" type="file" accept="image/*" />
171
+ <a href="../public/mypage.php">マイページへ</a>
172
+ </div>
173
+
174
+ <div>
175
+ <textarea
176
+ name="caption"
177
+ placeholder="キャプション(140文字以下)"
178
+ id="caption"
179
+ ></textarea>
180
+ </div>
181
+
182
+ <div class="submit">
183
+ <input type="submit" value="送信" class="btn" />
184
+ </div>
185
+ </form>
186
+ <div>
187
+ <?php foreach($files as $file): ?>
188
+ <img src=<?php echo "{$file['file_path']}"; ?> alt="">
189
+ <p><?php echo h("{$file['description']}"); ?></p>
190
+ <?php endforeach; ?>
191
+ </div>
192
+ </body>
193
+ </html>
194
+
195
+ file_upload.php
196
+
197
+
198
+ <?php
199
+ ini_set( 'display_errors', 1);
200
+ ini_set( 'error_reporting', E_ALL );
201
+
202
+ session_start();
203
+
204
+ // require_once '../classes/UserLogic.php';
205
+
206
+
207
+ //①ファイルの保存
208
+ //②DB接続
209
+ //③DBへの保存
210
+ require_once "../upload/dbc.php";
211
+
212
+ // ファイル関連の取得
213
+ $file = $_FILES['img'];
214
+ $filename = basename($file['name']);
215
+ $tmp_path = $file['tmp_name'];
216
+ $file_err = $file['error'];
217
+ $filesize = $file['size'];
218
+ $upload_dir = 'images/';
219
+ $save_filename = date('YmdHis') . $filename;
220
+ $err_msgs = array();
221
+ $save_path = $upload_dir. $save_filename;
222
+
223
+ // キャプションを取得
224
+ $caption = filter_input(INPUT_POST, 'caption',
225
+ FILTER_SANITIZE_SPECIAL_CHARS);
226
+
227
+ //キャプションのバリデーション
228
+ //未入力
229
+ if(empty($caption)) {
230
+ array_push($err_msgs, 'キャプションを入力してください。');
231
+ echo '<br>';
232
+ }
233
+ //140文字か
234
+ if(strlen($caption) > 140) {
235
+ echo 'キャプションは140文字以内にしてください。';
236
+ echo '<br>';
237
+ }
238
+
239
+ //ファイルのバリデーション
240
+ // ファイルサイズが1MB未満か
241
+ if($filesize > 1048576 || $file_err == 2) {
242
+ echo 'ファイルサイズは1MB未満にしてください。';
243
+ echo '<br>';
244
+ }
245
+
246
+ //拡張は画像形式か
247
+ $allow_ext = array('jpg', 'jpeg', 'png');
248
+ $file_ext = pathinfo($filename, PATHINFO_EXTENSION);
249
+
250
+ if(!in_array(strtolower($file_ext), $allow_ext)) {
251
+ array_push($err_msgs, '画像ファイルを添付してください。');
252
+ echo '<br>';
253
+ }
254
+
255
+ if (count($err_msgs) === 0) {
256
+ //ファイルはあるかどうか?
257
+ if (is_uploaded_file($tmp_path)) {
258
+ if (move_uploaded_file($tmp_path, $save_path)) {
259
+ echo $filename . 'を'. $upload_dir. 'アップしました。';
260
+ // DBに保存(ファイル名、ファイルパス、キャプション)
261
+ $result = fileSave($filename, $save_path, $caption);
262
+
263
+ if ($result) {
264
+ echo'データベースに保存しました!';
265
+ } else {
266
+ echo 'データベースへの保存に失敗しました!';
267
+ }
268
+ } else {
269
+ echo 'ファイルが保存できませんでした。';
270
+ }
271
+ } else {
272
+ echo 'ファイルが選択されていません。';
273
+ echo '<br>';
274
+ }
275
+ } else {
276
+ foreach($err_msgs as $msg) {
277
+ echo $msg;
278
+ echo '<br>';
279
+ }
280
+
281
+ /// 更新日時順で並び替える関数
282
+ $sort_by_lastmod = function($a, $b) {
283
+ return filemtime($b) - filemtime($a);
284
+ };
285
+
286
+ /// 並び替えして出力
287
+ $files = glob( 'path/to/files/*.*' );
288
+ usort( $files, $sort_by_lastmod );
289
+ foreach( $files as $file ) {
290
+ $timestamp = date('Y-m-d H:i:s', filemtime( $file ) );
291
+ echo basename( $file ) . ' : ' . $timestamp . '<br>';
292
+ }
293
+
294
+ }
295
+ ?>
296
+ <a href= "upload_form.php">次ページ</a>
297
+
298
+
299
+ <style>
300
+ body {
301
+ background-color: #CCFFFF;
302
+ }
303
+ </style>
304
+
305
+
34
306
  UserLogic.php
35
307
 
308
+
309
+ <?php
310
+ ini_set( 'display_errors', 1);
311
+ ini_set( 'error_reporting', E_ALL );
312
+
313
+ require_once '../dbconnect.php';
314
+
315
+ class UserLogic
316
+ {
36
- /**
317
+ /**
318
+ * ユーザーを登録する
319
+ * @param array $userData
320
+ * @return array|bool $result|false
321
+ */
322
+ public static function createUser($userData)
323
+ {
324
+ $result = false;
325
+
326
+ $sql = 'INSERT INTO users (name, email, password) VALUES (?, ?, ?)';
327
+
328
+ //ユーザーデータを配列に入れる
329
+ $arr = [];
330
+ $arr[] = $userData['username'];
331
+ $arr[] = $userData['email'];
332
+ $arr[] = password_hash($userData['password'],
333
+ PASSWORD_DEFAULT);
334
+
335
+ try {
336
+ $stmt = connect()->prepare($sql);
337
+ $result = $stmt->execute($arr);
338
+ return $result;
339
+ } catch(\Exception $e) {
340
+ return $result;
341
+ }
342
+ }
343
+
344
+ /**
37
345
  * ログイン処理
38
346
  * @param string $email
39
347
  * @param string $password
@@ -108,7 +416,16 @@
108
416
  return $result;
109
417
  }
110
418
 
419
+ /**
420
+ * ログアウト処理
421
+ */
422
+ public static function logout()
423
+ {
424
+ $_SESSION = array();
425
+ session_destroy();
426
+ }
111
427
 
428
+ }
112
429
  ```ここに言語名を入力
113
430
  php html css MAMP
114
431
  ```

1

脱字

2020/11/27 12:06

投稿

kqosu19
kqosu19

スコア0

title CHANGED
File without changes
body CHANGED
@@ -110,7 +110,7 @@
110
110
 
111
111
 
112
112
  ```ここに言語名を入力
113
- php html css
113
+ php html css MAMP
114
114
  ```
115
115
 
116
116
  ### 試したこと