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

Php 如果用户未登录,是否重定向到登录索引页?,php,header,Php,Header,我在我的网站上有多个页面,其中大多数都是用户登录后才应该访问的会员页面 当用户登陆我的页面时,他们会自动登陆索引/主页(index.php)。如果用户试图导航到dashboard.php(仅供成员使用),则应将其重定向回index.php,以便登录 在dashboard.php和manage_account.php等所有成员页面的顶部,我包含一个header.php文件,如下所示: include 'header.php'; 用户登录后,我创建会话“$\u session['user']” 我

我在我的网站上有多个页面,其中大多数都是用户登录后才应该访问的会员页面

当用户登陆我的页面时,他们会自动登陆索引/主页(index.php)。如果用户试图导航到dashboard.php(仅供成员使用),则应将其重定向回index.php,以便登录

在dashboard.php和manage_account.php等所有成员页面的顶部,我包含一个header.php文件,如下所示:

include 'header.php';
用户登录后,我创建会话“$\u session['user']”

我使用下面的头重定向检查会话是否存在,如果不存在,然后重定向该用户

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

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

?>

我的问题不是将标题重定向代码剪切粘贴到每个成员页面,而是将其放在header.php页面中,因为它包含在包括主页index.php在内的所有成员页面中


但是,它会创建一个连续重定向,并且不会加载页面,它表示成员页面中的web会:

$memberOnly = true;
include 'header.php';
在header.php中:

if (isset($memberOnly)) {
    if (empty($_SESSION['user'])) {
      header('Location: index.php');
      exit;
    }
}
<?php
// This code is executed whenever you include this file
session_start(); 
include 'config.php';

if (empty($_SESSION['user'])) {
    header('Location: index.php');
    exit;
}
?>
<?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;
  }
}
?>
在公共页面(非会员可用)中,您只需:

include 'header.php'

不用担心
$memberOnly

可能是因为标题也包含在索引中,对吗?您可以在重定向前的条件下检查:

<?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;
}

?>

您可以在config.php中设置一个数组,使用该数组验证页面,然后与当前页面进行比较,以定义是否进行验证

例如:

$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;
}


希望有帮助

在重定向之前添加此检查

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

将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;
}
?>

如果我正确理解您的问题,您的
header.php
文件将包含在每个页面中。尽管此header.php文件包含执行的代码:

header.php:

if (isset($memberOnly)) {
    if (empty($_SESSION['user'])) {
      header('Location: index.php');
      exit;
    }
}
<?php
// This code is executed whenever you include this file
session_start(); 
include 'config.php';

if (empty($_SESSION['user'])) {
    header('Location: index.php');
    exit;
}
?>
<?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
session_start();
include 'header.php';
// Public accessible pages do not call the function
...
?>

secret.php:

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

这对我很有用。 使用标题是最好的,但必须在将任何其他内容发送到浏览器之前使用。对我来说,在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(用户是否已登录()){
回声‘酷!’;
}否则{
$url=”https://yourdomain.com/log-in/";
回声';
}

我个人会
如果($users->isLogged){display buttons等..}或者{//display login or nothing?}
为什么不将必要的代码提取到一个函数中(例如
ensureUserIslogged()
)并调用这个函数,这比包含一个带有执行代码的头文件要干净得多。编写一个处理此问题的类将允许在登录后重定向到原始页面。@martinstoeckli很抱歉,我对php非常陌生,有什么方法可以告诉我你的意思吗?谢谢你需要向我们展示你其他页面的部分代码。下面的一些答案被否决了,问题也不清楚。@Fred ii-你需要看什么代码?其他页面上的代码只是基本的div和其他东西,对您来说没有任何真正重要的东西。是的,正确的index.php还包含“header.php”;但是当我尝试你的代码时,它会说同样的事情,网页不能显示,因为它有一个重定向循环,因为如果uri包含GET变量,它仍然会执行重定向。如果这不起作用,可能是因为你的网页在一个子文件夹中,例如yourdomain.com/whatever/,然后,为了实现这一点,您应该与完整路径(/whater/index.php)进行比较,但我宁愿尽量避免硬编码代码中的路径……使用PHP_SELF真的是个坏主意,而且存在安全风险。@Forien关于安全风险,那么围绕PHP SELF有没有办法使代码更安全,因为它似乎是目前唯一有效的解决方案?@JamesDaley
SCRIPT_FILENAME
也许?或者至少在PHP_SELF上执行
htmlspecialchars($_SERVER[“PHP_SELF”],ENT_QUOTES,“utf-8”)
以去除可能的XSS代码