2014-09-03 57 views
0

我想重定向消息,當用戶輸入錯誤的用戶名或錯誤的單詞通我已經試過,但我沒有什麼,這是我的login.php如何重定向錯誤消息,如果用戶輸入錯誤的數據

<?php 
$uname= mysql_real_escape_string($_POST['uname']); 
$pass= mysql_real_escape_string($_POST['pass']); 
$hashed_pass=md5("$pass"); 
    function redirect_to($location){ 
     header('Location:'.$location);} 

$query="select id,uname from student where uname='{$uname}' and hashed_pass='{$hashed_pass}'"; 
$result=mysqli_query($cnn,$query); 
//if query returns a thing 
if($result){ 
     $num_rows = mysqli_num_rows($result); 
     //agar user peida shod 
     if($num_rows == 1){ 

     $found_user=mysqli_fetch_array($result); 
     redirect_to("main.php"); 
     } 
     //useri peida nashod 
     else{ 
     $_SESSION['error_message']="Wrong Username or Password"; 
     redirect_to("index.php"); 

     } 
} 

else{ 
echo "can not perform" . mysqli_error(); 
}?> 

這是代碼的我的index.php部分

<?php 
      if(isset($_SESSION['error_message'])){ 
    echo $_SESSION['error_message']; 
    unset($_SESSION['error_message']); 
} 
     ?> 

我想知道我是缺少在這裏

+1

你混合'mysql'和'mysqli',不是最好的主意。 – 2014-09-03 15:45:32

+0

你真的不應該使用md5來散列密碼。使用像BCRYPT這樣的東西 - [這個頁面](https://github.com/ircmaxell/password_compat)有一個關於如何使用它的有用指南,並且也與舊版本的PHP兼容。 – 2014-09-03 15:49:40

回答

1
session_start(); 

你應該把它放在你的文件的最上面。否則,會話可能不會在第二個請求上加載。

欲瞭解更多信息,請參閱http://php.net/manual/en/function.session-start.php

+0

沒關係我之前的評論......我錯過了問題代碼的下半部分! – 2014-09-03 15:47:14