how to redirect an error message if the user inputs wrong data

I want to redirect a message when the user inputs wrong username or wrong pass word i already tried this but i got nothing this is my 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();
}?>

and this is my index.php part of the code

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

i am wondering what i am missing here

Upvotes: 0

Views: 66

Answers (1)

Peter M.
Peter M.

Reputation: 1269

session_start();

You should place it in the very top of your file(s). Otherwise the session might not be loaded on the second request.

For more info, see http://php.net/manual/en/function.session-start.php

Upvotes: 1

Related Questions