2017-05-08 31 views
-2

我想從login.php中得到的結果顯示在登錄表單我的錯誤信息,這裏要說的是,我有use.The第一部分的示例代碼是在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> 

這裏我已經爲了把錯誤信息else語句下的login.php將信息傳遞到登錄表單

<?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

使用'header'設置'$ error'變量的值將立即丟失後,當頁面重定向 – RamRaider

回答

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

和登錄表單檢查

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

無需添加header你已經有包括login.php文件中index.php

$error="username/passwod combination incorrect"; 
//header("location: index.php");//remove this line 
0
//header("location: index.php"); 

刪除此內容,或者您​​可以在一段時間後重定向頁面,如果您想這樣。

header("refresh:5;url=index.php"); // page redirect after 5sec