簡體   English   中英

如果用戶未登錄,重定向到登錄索引頁面?

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

我的網站上有多個頁面,其中大部分是會員頁面,用戶只能在登錄后訪問這些頁面。

當用戶登陸我的頁面時,他們會自動登陸索引/主頁 (index.php)。 如果用戶試圖導航到僅供會員使用的dashboard.php,那么他們應該被重定向回 index.php 以便他們可以登錄。

在我所有成員頁面的頂部,如dashboard.php 和 manage_account.php,我包含了一個 header.php 文件,如下所示:

include 'header.php';

用戶登錄后,我創建會話 '$_session['user']'

我正在使用以下標頭重定向來檢查會話是否存在,如果不存在,則重定向該用戶。

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

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

?>

我的問題不是將標題重定向代碼剪切並粘貼到每個成員頁面,我只想將它放在 header.php 頁面中,因為這包含在我的所有成員頁面中,包括我的主頁 index.php。

但是它創建了一個連續的重定向並且不加載頁面,它說網絡

可能是因為標題也包含在索引中,對嗎? 您可以在重定向之前檢查條件:

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

}

希望能幫助到你!

在會員頁面內做:

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

在 header.php 中:

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

在公共頁面(非會員可用)中,您只需:

include 'header.php'

不用擔心$memberOnly

如果我正確理解您的問題,您的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;
}
?>

你得到一個重定向循環,這意味着這段代碼也在index.php頁面中執行。 也許 header.php 文件也包含在 index.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;
  }
}
?>

索引.php:

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

秘密.php:

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

這對我有用。 使用header是最好的,但必須在任何其他內容發送到瀏覽器之前使用。 對我來說,在 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")

您將 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