简体   繁体   中英

PHP redirect user if logged in

I am a newbie in PHP and MySql. Kindly forgive me if i am silly

I am developing a web app for my project on top of UserSpice login management framework

Now i created a login form. Which on login will redirect to dashboard which is a bootstrap admin template.

My login page is

localhost/myproject/index.php

My Dashboard page is

localhost/myproject/account.php

Now what happens was, once after logging in i can see dashboard, suppose without logging out if i change the URL to

localhost/myproject

then it is showing login form instead of redirecting to my account.php. but at the same time if again change the URL to

localhost/myproject/account.php

without asking me to re-login it enters successfully taking the session.

i cross checked in other way now like i logged out and tried entering directly to

localhost/myproject/account.php

now without taking me in it logically asks me to login as i logged out already.

what i want was if i am logged in i want to redirect automatically to account.php

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.

//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->isLoggedIn())
    {
      Redirect::to(env('client').'index.php');
      return false;
    }

    else {
      //Retrieve list of permission levels with access to page

      $query = $db->query("SELECT permission_id FROM permission_page_matches WHERE page_id = ?",[$pageID]);

      $permission = $query->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->data()->id == $master_account){
        return true;
      }
      else {
        Redirect::to("index.php");
        return false;
      }
    } 

I tried to add the following code in that as

 elseif(!$user->isLoggedIn())
        {
          Redirect::to(env('client').'index.php');
          return false;
        }
 elseif($user->isLoggedIn())
        {
          Redirect::to(env('client').'account.php');
          return true;
        }

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.

UPDATE: (After Performing @DanHoover Solution)

As per the suggestion by @DanHoover i vardump user.

If i am logged out i am getting following dump

object(User)#3 (5) {
  ["_db":"User":private]=>
  object(DB)#4 (7) {
    ["_pdo":"DB":private]=>
    object(PDO)#5 (0) {
    }
    ["_query":"DB":private]=>
    object(PDOStatement)#6 (1) {
      ["queryString"]=>
      string(50) "SELECT id, page, private FROM pages WHERE page = ?"
    }
    ["_error":"DB":private]=>
    bool(false)
    ["_results":"DB":private]=>
    array(1) {
      [0]=>
      object(stdClass)#7 (3) {
        ["id"]=>
        string(2) "13"
        ["page"]=>
        string(9) "index.php"
        ["private"]=>
        string(1) "0"
      }
    }
    ["_resultsArray":"DB":private]=>
    array(1) {
      [0]=>
      array(3) {
        ["id"]=>
        string(2) "13"
        ["page"]=>
        string(9) "index.php"
        ["private"]=>
        string(1) "0"
      }
    }
    ["_count":"DB":private]=>
    int(1)
    ["_lastId":"DB":private]=>
    string(1) "0"
  }
  ["_data":"User":private]=>
  NULL
  ["_sessionName":"User":private]=>
  string(4) "user"
  ["_isLoggedIn":"User":private]=>
  NULL
  ["_cookieName":"User":private]=>
  string(18) "pmqesoxiw318374csb"
}

Suppose if i am logged in i am getting following dump

object(User)#3 (5) {
  ["_db":"User":private]=>
  object(DB)#4 (7) {
    ["_pdo":"DB":private]=>
    object(PDO)#5 (0) {
    }
    ["_query":"DB":private]=>
    object(PDOStatement)#8 (1) {
      ["queryString"]=>
      string(50) "SELECT id, page, private FROM pages WHERE page = ?"
    }
    ["_error":"DB":private]=>
    bool(false)
    ["_results":"DB":private]=>
    array(1) {
      [0]=>
      object(stdClass)#6 (3) {
        ["id"]=>
        string(2) "13"
        ["page"]=>
        string(9) "index.php"
        ["private"]=>
        string(1) "0"
      }
    }
    ["_resultsArray":"DB":private]=>
    array(1) {
      [0]=>
      array(3) {
        ["id"]=>
        string(2) "13"
        ["page"]=>
        string(9) "index.php"
        ["private"]=>
        string(1) "0"
      }
    }
    ["_count":"DB":private]=>
    int(1)
    ["_lastId":"DB":private]=>
    string(1) "0"
  }
  ["_data":"User":private]=>
  object(stdClass)#7 (29) {
    ["id"]=>
    string(1) "1"
    ["email"]=>
    string(25) "myid@mysite.com"
    ["username"]=>
    string(5) "admin"
    ["password"]=>
    string(60) "$2y$12$1v06jm2KMOXuuo3qP7erTuTIJFOnzhpds1Moa8BadnUUeX0RV3ex."
    ["fname"]=>
    string(4) "Raja"
    ["lname"]=>
    string(5) "Gopal"
    ["permissions"]=>
    string(1) "1"
    ["logins"]=>
    string(2) "27"
    ["account_owner"]=>
    string(1) "1"
    ["account_id"]=>
    string(1) "1"
    ["company"]=>
    string(9) "UserSpice"
    ["stripe_cust_id"]=>
    string(0) ""
    ["billing_phone"]=>
    string(0) ""
    ["billing_srt1"]=>
    string(0) ""
    ["billing_srt2"]=>
    string(0) ""
    ["billing_city"]=>
    string(0) ""
    ["billing_state"]=>
    string(0) ""
    ["billing_zip_code"]=>
    string(0) ""
    ["join_date"]=>
    string(19) "2016-01-01 00:00:00"
    ["last_login"]=>
    string(19) "2016-03-17 05:08:20"
    ["email_verified"]=>
    string(1) "1"
    ["vericode"]=>
    string(6) "322418"
    ["title"]=>
    string(0) ""
    ["active"]=>
    string(1) "0"
    ["custom1"]=>
    string(0) ""
    ["custom2"]=>
    string(0) ""
    ["custom3"]=>
    string(0) ""
    ["custom4"]=>
    string(0) ""
    ["custom5"]=>
    string(0) ""
  }
  ["_sessionName":"User":private]=>
  string(4) "user"
  ["_isLoggedIn":"User":private]=>
  bool(true)
  ["_cookieName":"User":private]=>
  string(18) "pmqesoxiw318374csb"
}

Then i tried your following suggested

if (isset($user)) {
Redirect::to('users/account.php');
}

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

在此处输入图像描述

I'm not 100% sure how you're getting the login form if you're already logged in. Are you sure you're logged in? A few things you can do...

When you're logged in to UserSpice you have access to a variable called $user. If that's not there, you're not logged in. This gives you a few options. For testing purposes anywhere on your page in php you can do

dump($user); //will give you a preformatted var_dump of the user variable.

If this comes back with an error, then you're not logged in. If it gives a beautifully formatted var_dump, you are. Once you're sure you're logged in, you can handle the redirect.

If I understand your question correctly, you want the site to behave differently if a user is logged in. Am I correct in saying you don't want logged in users to go to the home page.

So, what you want to do is go into the index.php in the root and at the top of the page after the line that checks if the page is a secure page (something like) if isSecurePage or whatever it's called), put.

if (isset($user)) {
Redirect::to('users/account.php');
}

If that's not it...that's really close. Basically...you're checking if that user variable is there, and if it is, you want to use the built in static function to redirect them somewhere.

Let me know if that solves it.

I solved it successfully by adding the following code in index.php

<?php
if ($user->isLoggedIn()) {
Redirect::to('account.php');
}
?>

I am thankful to @DanHoover as with the help of his lead only i proceeded . Thanks a ton mate.

Try this-

 <?php
  session_start();

  if((isset($_SESSION["user"]))) 
  {
    header("Location: account.php");
  }
 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM