前提・実現したいこと
phpでログインシステムを実装中に以下のエラーが発生しました。
発生している問題・エラーメッセージ
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at login.php:1) in login.php on line 21 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at login.php:1) in/login.php on line 21 Notice: Undefined index: email in login.php on line 23 Notice: Undefined index: email in login.php on line 83 Notice: Undefined variable: error in login.php on line 84 Notice: Undefined variable: error in login.php on line 87 Notice: Undefined variable: error in login.php on line 87
該当のソースコード
php
1<?php 2session_start(); 3?> 4<!DOCTYPE html> 5<html lang="ja"> 6<head> 7 <meta charset="UTF-8"> 8 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 9 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 10 <title>ひとこと掲示板</title> 11 12 <link rel="stylesheet" href="../style.css" /> 13</head> 14 15<body> 16<div id="wrap"> 17 <div id="head"> 18 <h1>ログインする</h1> 19 </div> 20 <div id="content"> 21 <?php 22 require('dbconnect.php'); 23 24 //session_start(); 25 26 if ($_COOKIE['email'] != '') { 27 $_POST['email'] = $_COOKIE['email']; 28 $_POST['password'] = $_COOKIE['password']; 29 $_POST['save'] = 'on'; 30 } 31 32 if (!empty($_POST)) { 33 // ログインの処理 34 if ($_POST['email'] != '' && $_POST['password'] != '') { 35 $login = $db->prepare('SELECT * FROM members WHERE email=? AND password=?'); 36 $login->execute(array( 37 $_POST['email'], 38 sha1($_POST['password']) 39 )); 40 $member = $login->fetch(); 41 42 if ($member) { 43 // ログイン成功 44 $_SESSION['id'] = $member['id']; 45 $_SESSION['time'] = time(); 46 47 // ログイン情報を記録する 48 if ($_POST['save'] == 'on') { 49 setcookie('email', $_POST['email'], time()+60*60*24*14); 50 setcookie('password', $_POST['password'], time()+60*60*24*14); 51 } 52 53 header('Location: index.php'); exit(); 54 } else { 55 $error['login'] = 'failed'; 56 } 57 } else { 58 $error['login'] = 'blank'; 59 } 60 } 61 ?> 62 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 63 <html xmlns="http://www.w3.org/1999/xhtml"> 64 <head> 65 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 66 <link rel="stylesheet" type="text/css" href="../style.css" /> 67 <link href="https://fonts.googleapis.com/css?family=Pacifico" rel="stylesheet"> 68 <title>ログインする</title> 69 </head> 70 71 <body> 72 <div id="wrap"> 73 <div id="head"> 74 75 </div> 76 <div id="content"> 77 <div id="lead"> 78 <p>メールアドレスとパスワードを記入してログインしてください。</p> 79 <p>入会手続きがまだの方はこちらからどうぞ。</p> 80 <p>»<a href="join/">入会手続きをする</a></p> 81 </div> 82 <form action="" method="post"> 83 <dl> 84 <dt>メールアドレス</dt> 85 <dd> 86 <input type="text" name="email" size="35" maxlength="255" value="<?php echo htmlspecialchars($_POST['email']); ?>" /> 87 <?php if ($error['login'] == 'blank'): ?> 88 <p class="error">* メールアドレスとパスワードをご記入ください</p> 89 <?php endif; ?> 90 <?php if ($error['login'] == 'failed'): ?> 91 <p class="error">* ログインに失敗しました。正しくご記入ください。</p> 92 <?php endif; ?> 93 </dd> 94 <dt>パスワード</dt> 95 <dd> 96 <input type="password" name="password" size="35" maxlength="255" value="<?php echo htmlspecialchars($_POST['password']); ?>" /> 97 </dd> 98 <dt>ログイン情報の記録</dt> 99 <dd> 100 <input id="save" type="checkbox" name="save" value="on"> 101 <label for="save">次回からは自動的にログインする</label> 102 </dd> 103 </dl> 104 <div> 105 <input type="submit" value="ログインする" /> 106 </div> 107 </form> 108 </div> 109 110 </div> 111 </div> 112 <div id="foot"> 113 <p style="font-family: 'Pacifico', cursive; font-size:25px; text-align:center;">©2012-2018 Todaylesson Team BLACKBOARD <br>© H2O Space. MYCOM,</p> 114 </div> 115 </body> 116 </html> 117 </div> 118 119</div> 120</body> 121</html>
補足情報(FW/ツールのバージョンなど)
php 7.1
mysql 5.6
質問したいことは何でしょうか。「エラーが発生しました」だけでは何が聞きたいのかわかりません。自身がわかっている範囲とわかっていない範囲を明確にして、要件をきちんと書いてください。「コード全部見て理解して」というのは横暴です。提示される以上は自身が一番分かっているはずなので、どのような仕組みになっているかは概要説明レベルは必要です。
回答1件
あなたの回答
tips
プレビュー