2014-09-03 56 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