2016-09-10 4 views
0

以下のコードは私のユーザーログインフォームです。しかし、ユーザー名とパスワードが一致してページがリダイレクトされた後、[ユーザー名に[未定義のインデックス] [パス] 'というエラーが表示されます。ユーザーがPHPのログインフォームからログインした後にデータが正しく表示されない

<?php 
session_start(); 

require_once "dbreg.php"; 
$errormsg = array(); 
$errorcount = 0; 
if (!empty($_POST)) { 
    if (empty($_POST['username'])) { 
    $errormsg[] = "Enter valid username"; 
    $errorcount++; 
    } 
    if(empty($_POST['password'])) { 
    $errormsg[] = "Please enter password"; 
    $errorcount++; 
} 
if(!empty($_POST['username'])) { 
$userquery = mysql_query("SELECT * FROM regform WHERE username='".$_POST['username']."'"); 
$useroutput = mysql_fetch_assoc($userquery); 

if (empty($useroutput)) { 
    $errormsg[] = "Invalid username or password"; 
    $errorcount++; 
} 
else { 
    $queryoutput = mysql_query("SELECT * FROM regform WHERE username = '".$_POST['username']."' AND userpass = '".$_POST['password']."'"); 
    $newoutput = mysql_fetch_assoc($queryoutput); 
    if (empty($newoutput)) { 
    $errormsg[] = "Please enter valid login and password"; 
    $errorcount++; 
    } 
    else { 
    $_SESSION['uid'] = $newoutput['id']; 
    header("Location: http://localhost/classwork2/userprofile.php"); 
    } 
} 
} 
} 

?> 
    <!DOCTYPE html> 
    <html> 

    <head> 
     <meta charset="utf-8"> 
     <title>Login form</title> 
    </head> 

    <body> 
     <h1>Login</h1> 
     <?php 
     if (!empty($errormsg)) { 
     for ($i=0; $i < count($errormsg) ; $i++) { 
      # code... 
      ?> 
      <div style="color:red"><?php echo $errormsg[$i]; ?></div> 
      <?php 
     } 
     } 
     ?> 
     <table border="1"> 
      <form name="lognow" id="lognow" action="reglogin.php" method="post" enctype="multipart/form-data"> 
       <tr> 
        <td> 
         <label>Username</label> 
        </td> 
        <td> 
         <input type="text" name="username" id="username"> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <label>Password</label> 
        </td> 
        <td> 
         <input type="text" name="password" id="password"> 
        </td> 
       </tr> 
       <tr> 
        <td> 
        <input type="submit" value="Login"> 
        </td> 
       </tr> 
      </form> 
     </table> 
     <h3>Or</h3> 
     <h2> 
      <a href="reg1.php">Click Here</a> to Register. 
     </h2> 
    </body> 

    </html> 

以下れる、誰もが私が間違って何をやっている私を導くことができるかどうuserprofile.phpページにコード

<?php 
session_start(); 
require_once "dbreg.php"; 

$sql = "SELECT firstname, lastname FROM regform WHERE username = '" . $_SESSION['username'] . "'"; 
$result = mysql_query($sql); 
$row = mysql_fetch_assoc($result); 

echo "Hello, " . $row['firstname'] . $row['lastname'] ; 

?> 

は親切だろう。また、上記のコードをif(!empty($_POST['username']))の中に入れておくと、ページは空白になります。問題がある

+1

これは、成功したログインの後に '$ _SESSION ['username'] =" YOUR_USERNAME "'を設定したことがないため、$ _SESSION ['uid'] = $ newoutput ['id']; 'をリダイレクトしただけですユーザーは* userprofile.php *ページに移動します。 –

+0

多くのお返事ありがとうございますが、ユーザー名とパスワードがたくさんありますが、この方法を使用することはできません。どのようにコードを修正する必要がありますか? –

+0

私は以下の解決策を提示しました。うまくいけば、あなたの問題を解決するでしょう。 –

答えて

0

は、あなたがやった/、ログイン成功後$_SESSION['username'] = "YOUR_USERNAME"を設定することはありません、あなただけの$_SESSION['uid'] = $newoutput['id'];を行なったし、userprofile.phpページにユーザーをリダイレクト。

したがってソリューションは、ユーザーのusernameではなく、ユーザーのidに基づいてクエリを作成します。 userprofile.phpページでは、次のようにクエリを変更します

$sql = "SELECT firstname, lastname FROM regform WHERE id = '" . $_SESSION['uid'] . "'"; 

// your code 

追記を:mysql_*機能は、PHP 5.5で廃止されており、PHP 7.0で完全に削除されます。代わりにmysqliまたはpdoを使用してください。そしてthis is why you shouldn't use mysql_* functions

+0

はい、今動作します。 Thxと私は間違いなくmysqliを使用する必要があります –

+1

@AbhrapratimNagようこそ。 mysqliで始めるなら、[prepared statements](https://en.wikipedia.org/wiki/Prepared_statement)も読んでみることをお勧めします。 –

+0

もちろん@Rajdeep私はそうです。ちょうど質問、 'mysqli'はW3 Schoolsまたは他の何かが良い場所になるでしょうか? –

関連する問題