Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果用户输入错误数据,如何重定向错误消息_Php_Session - Fatal编程技术网

Php 如果用户输入错误数据,如何重定向错误消息

Php 如果用户输入错误数据,如何重定向错误消息,php,session,Php,Session,当用户输入错误的用户名或密码时,我想重定向一条消息。我已经尝试过了,但没有得到任何结果。这是我的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(

当用户输入错误的用户名或密码时,我想重定向一条消息。我已经尝试过了,但没有得到任何结果。这是我的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']);
}
        ?>  

我想知道我在这里错过了什么

session_start();
你应该把它放在文件的最上面。否则,会话可能无法在第二个请求上加载


有关更多信息,请参见

您正在混合使用
mysql
mysqli
,这不是最好的主意。您真的不应该使用md5散列密码。使用类似BCRYPT的东西-有一个关于如何使用它的有用指南,并且与旧版本的PHP兼容。。。我错过了问题代码的下半部分!