2016-09-30 10 views
-1

ブログページに基本的なログインフォームがあり、正しいユーザー名とパスワードを入力すると、ユーザー名+パスワードは正しいが、私はindex1.phpページにリダイレクトされません。以下PHPフォームが正しく機能しない

<?php 
    require_once('header.php'); 
    require_once('config.php'); 

$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 

    //check to see if form has been submitted 
    if (isset($_POST['submit'])) { 
    if ($_POST['username'] == 'bruce' && ($_POST['password'] == 'wayne')) { 
     echo 'Correct username and password.'; 
    } 
    else { 
     echo 'Incorrect username or password. Please try again.'; 
    } 

    $result = mysqli_query($db, $sql); 
    $numrows = mysqli_num_rows($result); 

    if($numrows == 1) { 
     $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
     $_SESSION['USERNAME'] = $row['username']; 
     $_SESSION['USERID'] = $row['id']; 

     header("Location:"."index1.php"); 
    } 
    else { 
     header("Location"."login.php?error=1"); 
    } 
} 

    else { 
     if(isset($_GET['error'])) { 
      echo "Incorrect login, please try again!"; 
     } 
    } 
?> 

<form action="<?php echo $_SERVER['index1.php'] ?>" method="post"> 
<table id='login'> 
<tr> 
    <td>Username</td> 
    <td><input type="text" name="username"></td> 
</tr> 
<tr> 
    <td>Password</td> 
    <td><input type="password" name="password"></td> 
</tr> 
<tr> 
    <td></td> 
    <td><input type="submit" name="submit" value="Login!"></td> 
</tr> 
</table> 
</form> 

index1.php:

は、ここに私のコードです!

<?php 
    require("header.php"); 

    $db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 

    $sql = "SELECT entries.*, categories.cat FROM entries, categories 
     WHERE entries.cat_id = categories.id 
     ORDER BY dateposted DESC 
     LIMIT 1;"; 

    $result = mysqli_query($db,$sql); 
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 

    echo "<h2><a href= 'viewentry.php?id=" . $row['id'] . "'>" . $row['subject']. "</a></h2><br />"; 
    echo "<i>In <a href='viewcat.php?id=" . $row['cat_id'] . "'>" . $row['cat'] ."</a> - Posted on " . 
      date("D jS F Y g.iA", strtotime($row['dateposted'])) ."</i>"; 
    echo "<p>"; 
    echo nl2br($row['body']); 
    echo "</p>"; 

    require("footer.php"); 

?> 
+0

ありがとうございましたか! – Ghost

+1

あなたの '$ sql'ステートメントはどこにありますか私はそれを見ません。 – Franco

+0

"ヘッダはすでに送信されました"のようなエラーが表示されますか? –

答えて

0

まず、もはや記憶されるだろう。ここでは、関連する例ません。

+0

あなたはindex1.phpのコードを表示することができます –

+0

私に教えてくださいそれはあなたのために動作するかどうか –

0

出力バッファリングを使用していない限り、リダイレクトの前にエコーすることはできません。私はあなたのエコーをコメントアウトしました。

私はこれを常に使用しています。リダイレクト後にexitを呼び出す必要があります。

/** 
* This is designed to redirect using a quick, easy function. 
*/ 
function gfRedir($_NewPath){ 
    header('Location: '.$_NewPath); 
    exit(); 
} 

あなたはそれが好きで使用します。

<?php 
    require_once('header.php'); 
    require_once('config.php'); 

$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 

    //check to see if form has been submitted 
    if (isset($_POST['submit'])) { 
    if ($_POST['username'] == 'bruce' && ($_POST['password'] == 'wayne')) { 
     //you cannot echo before a redirect unless you are using output buffering. 
     //echo 'Correct username and password.'; 
    } 
    else { 
     //you cannot echo before a redirect unless you are using output buffering. 
     //echo 'Incorrect username or password. Please try again.'; 
    } 

    //where is this $sql at that you are passing into the below function? 

    $result = mysqli_query($db, $sql); 
    $numrows = mysqli_num_rows($result); 

    if($numrows == 1) { 
     $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
     $_SESSION['USERNAME'] = $row['username']; 
     $_SESSION['USERID'] = $row['id']; 

     gfRedir('index2.php'); 
    } 
    else { 

     gfRedir('login.php?error=1'); 
    } 
    } 

    else { 
     if(isset($_GET['error'])) { 
      echo "Incorrect login, please try again!"; 
     } 
    } 


    function gfRedir($_NewPath){ 
     header('Location: '.$_NewPath); 
     exit(); 
    } 
?> 

<form action="<?php echo $_SERVER['index1.php'] ?>" method="post"> 
<table id='login'> 
<tr> 
    <td>Username</td> 
    <td><input type="text" name="username"></td> 
</tr> 
<tr> 
    <td>Password</td> 
    <td><input type="password" name="password"></td> 
</tr> 
<tr> 
    <td></td> 
    <td><input type="submit" name="submit" value="Login!"></td> 
</tr> 
</table> 
</form> 
+0

これを試した、それは今や間違ったパスワードまたはユーザー名を言う – chris

+0

いいえ、これは単なるスニペットですあなたの現在のコード。 –

+0

"ヘッダーが既に送信されました"というエラーが表示された場合は、 –

-1

リダイレクトのために、この構文を使用してみてください。

header("Location:index1.php"); 
exit(); 

あなたに `$のdb`が定義されている

関連する問題