title: PHP redirect user if logged in
body: <p>I am a newbie in PHP and MySql . Kindly forgive me if i am silly </p>
  
  <p>I am developing a web app for my project on top of UserSpice login management framework </p>
  
  <p>Now i created a login form. Which on login will redirect to dashboard which is a bootstrap admin template . </p>
  
  <p>My login page is </p>
  
  <blockquote>
    <p>localhost/myproject/index.php</p>
  </blockquote>
  
  <p>My Dashboard page is</p>
  
  <blockquote>
    <p>localhost/myproject/account.php</p>
  </blockquote>
  
  <p>Now what happens was , once after logging in i can see dashboard , suppose without logging out if i change the URL to </p>
  
  <blockquote>
    <p>localhost/myproject</p>
  </blockquote>
  
  <p>then it is showing login form instead of redirecting to my account.php . but at the same time if again change the URL to </p>
  
  <blockquote>
    <p>localhost/myproject/account.php</p>
  </blockquote>
  
  <p>without asking me to re-login it enters successfully taking the session . </p>
  
  <p>i cross checked in other way now like i logged out and tried entering directly to </p>
  
  <blockquote>
    <p>localhost/myproject/account.php</p>
  </blockquote>
  
  <p>now without taking me in it logically asks me to login as i logged out already. </p>
  
  <p>what i want was if i am logged in i want to redirect automatically to account.php </p>
  
  <p>since i am using a user management framework i thought that it will take care of that but this issue i am facing now . i little bit dig and found the following code in the framework which makes check whether if the user is not logged in it will then redirect to index.php but it is not checking whether if it is logged in then it should go to account.php . </p>
  
  <pre><code>//If page does not exist in DB, allow access
      if (empty($pageDetails)){
        return true;
      }
      //If page is public, allow access
      elseif ($pageDetails['private'] == 0) {
        return true;
      }
      //If user is not logged in, deny access
      elseif(!$user-&gt;isLoggedIn())
      {
        Redirect::to(env('client').'index.php');
        return false;
      }
  
      else {
        //Retrieve list of permission levels with access to page
  
        $query = $db-&gt;query("SELECT permission_id FROM permission_page_matches WHERE page_id = ?",[$pageID]);
  
        $permission = $query-&gt;results();
        $pagePermissions[] = $permission;
  
        //Check if user's permission levels allow access to page
        if (checkPermission($pagePermissions)){
          return true;
        }
        //Grant access if master user
        elseif ($user-&gt;data()-&gt;id == $master_account){
          return true;
        }
        else {
          Redirect::to("index.php");
          return false;
        }
      } 
  </code></pre>
  
  <p>I tried to add the following code in that as </p>
  
  <pre><code> elseif(!$user-&gt;isLoggedIn())
          {
            Redirect::to(env('client').'index.php');
            return false;
          }
   elseif($user-&gt;isLoggedIn())
          {
            Redirect::to(env('client').'account.php');
            return true;
          }
  </code></pre>
  
  <p>But, it doesn't help me. Kindly help me to redirect to account.php if the user is already logged in and changed the url to log in form accidentally . </p>
  
  <p><strong><em>UPDATE:</em></strong> <em>(After Performing @DanHoover Solution)</em> </p>
  
  <p>As per the suggestion by @DanHoover i vardump user . </p>
  
  <p>If i am logged out i am getting following dump </p>
  
  <pre><code>object(User)#3 (5) {
    ["_db":"User":private]=&gt;
    object(DB)#4 (7) {
      ["_pdo":"DB":private]=&gt;
      object(PDO)#5 (0) {
      }
      ["_query":"DB":private]=&gt;
      object(PDOStatement)#6 (1) {
        ["queryString"]=&gt;
        string(50) "SELECT id, page, private FROM pages WHERE page = ?"
      }
      ["_error":"DB":private]=&gt;
      bool(false)
      ["_results":"DB":private]=&gt;
      array(1) {
        [0]=&gt;
        object(stdClass)#7 (3) {
          ["id"]=&gt;
          string(2) "13"
          ["page"]=&gt;
          string(9) "index.php"
          ["private"]=&gt;
          string(1) "0"
        }
      }
      ["_resultsArray":"DB":private]=&gt;
      array(1) {
        [0]=&gt;
        array(3) {
          ["id"]=&gt;
          string(2) "13"
          ["page"]=&gt;
          string(9) "index.php"
          ["private"]=&gt;
          string(1) "0"
        }
      }
      ["_count":"DB":private]=&gt;
      int(1)
      ["_lastId":"DB":private]=&gt;
      string(1) "0"
    }
    ["_data":"User":private]=&gt;
    NULL
    ["_sessionName":"User":private]=&gt;
    string(4) "user"
    ["_isLoggedIn":"User":private]=&gt;
    NULL
    ["_cookieName":"User":private]=&gt;
    string(18) "pmqesoxiw318374csb"
  }
  </code></pre>
  
  <p>Suppose if i am logged in i am getting following dump </p>
  
  <pre><code>object(User)#3 (5) {
    ["_db":"User":private]=&gt;
    object(DB)#4 (7) {
      ["_pdo":"DB":private]=&gt;
      object(PDO)#5 (0) {
      }
      ["_query":"DB":private]=&gt;
      object(PDOStatement)#8 (1) {
        ["queryString"]=&gt;
        string(50) "SELECT id, page, private FROM pages WHERE page = ?"
      }
      ["_error":"DB":private]=&gt;
      bool(false)
      ["_results":"DB":private]=&gt;
      array(1) {
        [0]=&gt;
        object(stdClass)#6 (3) {
          ["id"]=&gt;
          string(2) "13"
          ["page"]=&gt;
          string(9) "index.php"
          ["private"]=&gt;
          string(1) "0"
        }
      }
      ["_resultsArray":"DB":private]=&gt;
      array(1) {
        [0]=&gt;
        array(3) {
          ["id"]=&gt;
          string(2) "13"
          ["page"]=&gt;
          string(9) "index.php"
          ["private"]=&gt;
          string(1) "0"
        }
      }
      ["_count":"DB":private]=&gt;
      int(1)
      ["_lastId":"DB":private]=&gt;
      string(1) "0"
    }
    ["_data":"User":private]=&gt;
    object(stdClass)#7 (29) {
      ["id"]=&gt;
      string(1) "1"
      ["email"]=&gt;
      string(25) "myid@mysite.com"
      ["username"]=&gt;
      string(5) "admin"
      ["password"]=&gt;
      string(60) "$2y$12$1v06jm2KMOXuuo3qP7erTuTIJFOnzhpds1Moa8BadnUUeX0RV3ex."
      ["fname"]=&gt;
      string(4) "Raja"
      ["lname"]=&gt;
      string(5) "Gopal"
      ["permissions"]=&gt;
      string(1) "1"
      ["logins"]=&gt;
      string(2) "27"
      ["account_owner"]=&gt;
      string(1) "1"
      ["account_id"]=&gt;
      string(1) "1"
      ["company"]=&gt;
      string(9) "UserSpice"
      ["stripe_cust_id"]=&gt;
      string(0) ""
      ["billing_phone"]=&gt;
      string(0) ""
      ["billing_srt1"]=&gt;
      string(0) ""
      ["billing_srt2"]=&gt;
      string(0) ""
      ["billing_city"]=&gt;
      string(0) ""
      ["billing_state"]=&gt;
      string(0) ""
      ["billing_zip_code"]=&gt;
      string(0) ""
      ["join_date"]=&gt;
      string(19) "2016-01-01 00:00:00"
      ["last_login"]=&gt;
      string(19) "2016-03-17 05:08:20"
      ["email_verified"]=&gt;
      string(1) "1"
      ["vericode"]=&gt;
      string(6) "322418"
      ["title"]=&gt;
      string(0) ""
      ["active"]=&gt;
      string(1) "0"
      ["custom1"]=&gt;
      string(0) ""
      ["custom2"]=&gt;
      string(0) ""
      ["custom3"]=&gt;
      string(0) ""
      ["custom4"]=&gt;
      string(0) ""
      ["custom5"]=&gt;
      string(0) ""
    }
    ["_sessionName":"User":private]=&gt;
    string(4) "user"
    ["_isLoggedIn":"User":private]=&gt;
    bool(true)
    ["_cookieName":"User":private]=&gt;
    string(18) "pmqesoxiw318374csb"
  }
  </code></pre>
  
  <p>Then i tried your following suggested </p>
  
  <pre><code>if (isset($user)) {
  Redirect::to('users/account.php');
  }
  </code></pre>
  
  <p>Now what happens was if i am logged in it perfectly redirects to account.php . But, suppose if i am logged out it says Following error</p>
  
  <p><a href="https://i.stack.imgur.com/FlD9h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FlD9h.png" alt="enter image description here"></a></p>