简体   繁体   English

PHP 如果登录则重定向用户

[英]PHP redirect user if logged in

I am a newbie in PHP and MySql. Kindly forgive me if i am silly我是PHP和MySql的新手,傻了还请见谅

I am developing a web app for my project on top of UserSpice login management framework我正在 UserSpice 登录管理框架之上为我的项目开发一个 web 应用程序

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本地主机/我的项目/index.php

My Dashboard page is我的仪表板页面是

localhost/myproject/account.php本地主机/我的项目/account.php

Now what happens was, once after logging in i can see dashboard, suppose without logging out if i change the URL to现在发生的事情是,登录后我可以看到仪表板,假设如果我将 URL 更改为而无需注销

localhost/myproject本地主机/我的项目

then it is showing login form instead of redirecting to my account.php.然后它显示登录表单而不是重定向到我的帐户.php。 but at the same time if again change the URL to但同时如果再次将 URL 更改为

localhost/myproject/account.php本地主机/我的项目/account.php

without asking me to re-login it enters successfully taking the session.在不要求我重新登录的情况下,它成功进入了 session。

i cross checked in other way now like i logged out and tried entering directly to我现在以其他方式交叉检查,就像我注销并尝试直接进入

localhost/myproject/account.php本地主机/我的项目/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我想要的是,如果我登录了,我想自动重定向到 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.我稍微挖掘了一下,在框架中发现了以下代码,它检查用户是否未登录然后重定向到 index.php 但它不检查是否已登录然后它应该 go 到 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.请帮我重定向到 account.php 如果用户已经登录并且不小心将 url 更改为登录表单。

UPDATE: (After Performing @DanHoover Solution)更新:(执行@DanHoover 解决方案后)

As per the suggestion by @DanHoover i vardump user.根据@DanHoover i vardump 用户的建议。

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.现在发生的情况是,如果我登录,它会完美地重定向到 account.php。 But, suppose if i am logged out it says Following error但是,假设如果我注销,它会显示 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? 如果您已经登录,我不确定100%如何获得登录表单。确定要登录吗? A few things you can do... 您可以做的几件事...

When you're logged in to UserSpice you have access to a variable called $user. 登录UserSpice后,您可以访问名为$ 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 为了测试目的,您可以在php中的任何页面上执行

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. 如果再次出现错误,则表明您尚未登录。如果它给出了格式精美的var_dump,则您可以。 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. 因此,您要执行的操作是,在检查页面是否为安全页面(类似于isSecurePage或类似名称的页面)的行之后,进入根目录和页面顶部的index.php中。

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 我通过在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 . 我对@DanHoover表示感谢,因为只有在我继续前进的过程中,他才发挥了领导作用。 Thanks a ton mate. 多谢同伴。

Try this-试试这个-

 <?php
  session_start();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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