2017-05-08 13 views
-2

私はここで私はuse.The最初の部分を持っているサンプルコードは、login.phpから結果を取得することにより、ログインフォームでの私のエラーメッセージを表示したいのindex.phpなぜ私はログインページにエラーメッセージを表示できませんか?

<? 
include("login.php");?> 

<!DOCTYPE html> 

<html> 
<head> 
<title>Login Form in PHP with Session</title> 
<link href="style.css" rel="stylesheet" type="text/css"> 
</head> 
<body> 
    <div id="main"> 
     <h1>Hup Seng</h1> 
     <div id="login"> 
      <h2>Login Form</h2> 
      <form action="login.php" method="post"> 
       <label>UserName :</label> 
       <input id="name" name="username" placeholder="username" type="text" required> 
       <br> 
       <br> 
       <label>Password :</label> 
       <input id="password" name="password" placeholder="**********" type="password" required> 
       <br> 
       <input name="submit" type="submit" value=" Login "> 
        <span><?php echo $error; ?></span> 
      </form> 
     </div> 
</div> 
</body> 
</html> 

はこちられます私は、ログインフォーム

<?php 
include("dbconfig.php"); 
session_start(); // Starting Session 
$error=''; // Variable To Store Error Message 
//if (isset($_POST['submit'])) { 
if (isset($_POST['username']) || isset($_POST['password'])) { 

    // Define $username and $password 
    $username=$_POST['username']; 
    $password=$_POST['password']; 
    $pw = encode($password); 
    $sql = "SELECT count(ID) as cid FROM tblUser WHERE UserId = '$username' and Password1 = '$pw'"; 
    $rs = odbc_exec($link_mssql,$sql); 
    while (odbc_fetch_row($rs)) { 
     $count=odbc_result($rs,"cid"); 
    } 
    if ($count == 1) { 
     $_SESSION['username']=$username; // Initializing Session 
     header("location: homepage.php"); // Redirecting To Other Page 
    } else { 
     $error="username/passwod combination incorrect"; 
     header("location: index.php"); 
    } 
    odbc_close($link_mssql); // Closing Connection 
} 
//} 
?> 
+0

で – RamRaider

答えて

0
session_start(); 
$_SESSION['error']="username/passwod combination incorrect"; 

、ページリダイレクト値がすぐに失われた `$のERROR`変数を設定した後、` header`を使用して、ログインフォームチェック

session_start(); 
if(isset($_SESSION['error'])){ 
echo $_SESSION['error']; 
} 
1

headerすでにindex.php

login.phpファイルなど、あなたを追加する必要が情報を渡していないためにelse文の下にエラーメッセージを入れているlogin.php
0
//header("location: index.php"); 

これを削除するか、このようにしたい場合はしばらくしてからページをリダイレクトできます。

header("refresh:5;url=index.php"); // page redirect after 5sec 
関連する問題