简体   繁体   English

如果用户未登录,重定向到登录索引页面?

[英]redirect to login index page if user not logged in?

I have multiple pages on my site, most of which are member only pages which a user should have access to only once logged in.我的网站上有多个页面,其中大部分是会员页面,用户只能在登录后访问这些页面。

When a user lands at my page they automatically land on the index/home page (index.php).当用户登陆我的页面时,他们会自动登陆索引/主页 (index.php)。 If a user tried to navigate to dashboard.php which is for members only, then they should be redirected back to index.php so they can log in.如果用户试图导航到仅供会员使用的dashboard.php,那么他们应该被重定向回 index.php 以便他们可以登录。

at the top of all of my member pages like dashboard.php and manage_account.php i am including a header.php file like so:在我所有成员页面的顶部,如dashboard.php 和 manage_account.php,我包含了一个 header.php 文件,如下所示:

include 'header.php';

once a user is logged in i create the session '$_session['user']'用户登录后,我创建会话 '$_session['user']'

And i am using the following header redirect to check if the session exists and if it doesn't then redirect that user.我正在使用以下标头重定向来检查会话是否存在,如果不存在,则重定向该用户。

<?php
session_start(); 
include 'config.php';

if (empty($_SESSION['user'])) {
    header('Location: index.php');
    exit;
}

?>

My problem is rather than cut and paste a header redirect code to each and every member page I just want to place it in the header.php page as this is being included in all of my member pages including my home page index.php.我的问题不是将标题重定向代码剪切并粘贴到每个成员页面,我只想将它放在 header.php 页面中,因为这包含在我的所有成员页面中,包括我的主页 index.php。

however it creates a continuous redirect and does not load the page, it says the web但是它创建了一个连续的重定向并且不加载页面,它说网络

Probably because the header is included as well in the index, right?可能是因为标题也包含在索引中,对吗? You can check for that on the condition before redirecting:您可以在重定向之前检查条件:

<?php
session_start(); 
include 'config.php';

if (empty($_SESSION['user']) && parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) != '/index.php') {
    header('Location: index.php');
    exit;
}

?>

You could set an array in your config.php with which pages need be validate and then compare with current page to define if will validate.您可以在 config.php 中设置一个数组,其中需要验证哪些页面,然后与当前页面进行比较以定义是否验证。

For example:例如:

$member_pages = array('dashboard', 'member-page', 'etc');

$current = $_SERVER['REQUEST_URI'];
if (empty($_SESSION['user']) && array_search($current, $member_pages) !== FALSE) {
   header('Location: index.php');
   exit;

} }

Hope it helps!希望能帮助到你!

Within member pages do:在会员页面内做:

$memberOnly = true;
include 'header.php';

and in header.php:在 header.php 中:

if (isset($memberOnly)) {
    if (empty($_SESSION['user'])) {
      header('Location: index.php');
      exit;
    }
}

In public pages (non-member available) you simply:在公共页面(非会员可用)中,您只需:

include 'header.php'

without worrying about $memberOnly不用担心$memberOnly

If i understand your problem correctly, your header.php file is included in every page.如果我正确理解您的问题,您的header.php文件将包含在每个页面中。 Though this header.php file contains code which is executed:虽然这个 header.php 文件包含执行的代码:

header.php:头文件.php:

<?php
// This code is executed whenever you include this file
session_start(); 
include 'config.php';

if (empty($_SESSION['user'])) {
    header('Location: index.php');
    exit;
}
?>

You get a redirection loop, what means that this code is also executed in the index.php page.你得到一个重定向循环,这意味着这段代码也在index.php页面中执行。 Maybe the header.php file is included in the index.php file as well.也许 header.php 文件也包含在 index.php 文件中。

If you would extract the code to a function and call it only in the pages which require a logged in user, you would avoid this loop.如果您将代码提取到一个函数中并仅在需要登录用户的页面中调用它,您将避免此循环。

header.php:头文件.php:

<?php
// The code in this function is not called automatically when the file is included
function redirectToLoginIfNecessary()
{
  if (!isset($_SESSION['user'])) {
    header('Location: index.php');
    exit;
  }
}
?>

index.php:索引.php:

<?php
session_start();
include 'header.php';
// Public accessible pages do not call the function
...
?>

secret.php:秘密.php:

<?php
session_start();
include 'header.php';
// Protected pages do call the function
redirectToLoginIfNecessary();
...
?>

This works for me.这对我有用。 Using header is best, but has to be used before any other content is sent to the browser.使用header是最好的,但必须在任何其他内容发送到浏览器之前使用。 Which, for me, developing in schmurdpress, makes it hard to implement.对我来说,在 schmurdpress 中开发,这使得它难以实施。

if ( is_user_logged_in() ) {
    echo 'Cool!';
} else {
    $url = "https://yourdomain.com/log-in/";
    echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $url . '">';
}

在重定向之前添加此检查

if ($_SERVER["PHP_SELF" ] != "index.php")

You redirect index.php to index.php - if the access file is index.php your redirection shouldn't be fired.您将 index.php 重定向到 index.php - 如果访问文件是 index.php,则不应触发您的重定向。

<?php
session_start(); 
include 'config.php';

$basename = substr(strtolower(basename($_SERVER['PHP_SELF'])),0,strlen(basename($_SERVER['PHP_SELF']))-4);


if ((empty($_SESSION['user'])) && ($basename!="index")) {
 header('Location: index.php');
 exit;
}
?>

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

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