Question

I am working on PHP website. I've created a function to check user is logged in or not. And if user is logged in then it will fire db query and will check where to redirect user. But it is not working properly. I've tried by changing values in mysql db but it is not working and redirecting to locations.

<?php
function Is_User_Logged_In()
{
    require $_SERVER['DOCUMENT_ROOT'].'/configuration/configurationfile.php';
    if(isset( $_SESSION['uname'], $_SESSION['email']))
    {
        $uname = @$_SESSION['uname'];
        $email = @$_SESSION['email'];

        if($result = $db->query("SELECT * FROM `users` WHERE BINARY `uname` = '$uname' AND `email` = '$email'"))
        {
            if($result and $result->num_rows)
            {
                $result = mysqli_fetch_array($result);
                $step2 = $result['step2'];
                $vpic = $result['vpics'];
                $act = $result['active'];

                if($step2 == 0)
                {
                    header("Location: /steptwo");
                }
                return true;
                exit();

                if($step2 != 0 && $vpic == 0)
                {
                    header("Location: /idupload");
                }
                return true;

                if($step2 != 0 && $vpic != 0 && $act == 0)
                {
                    header("Location: /pending");
                }
                return true;
            }
        }
        return true;
    }
    return false;
    header ("Location: /");
}
?>

this is my function. Default value of step2, vpics and active in database is 0. First I want to check that if step2 is zero or what. If it is zero then it should redirect to "/steptwo" page. Then I want to check vpics and then active. If three of them are not zero then home page. Can anyone help me?

No correct solution

OTHER TIPS

You are returning false right before your header statement.

return false;
header ("Location: /");

Should be

header ("Location: /");
return false;

EDIT Just realized your if statements are all wonky. Change them:

if($step2 == 0)
{
     header("Location: /steptwo");
     exit();
}
if($step2 != 0 && $vpic == 0)
{
     header("Location: /idupload");
     exit();
}
if($step2 != 0 && $vpic != 0 && $act == 0)
{
     header("Location: /pending");
     exit();
}
   if($step2 == 0){
       header("Location: /steptwo");
   }return true; exit();

This should be

       if($step2 == 0){
        header("Location: /steptwo");
        exit();
      }

If the exit is outside if, then the lines below the if will never be executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top