简体   繁体   中英

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). 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.

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:

include 'header.php';

once a user is logged in i create the session '$_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.

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.

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:

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

If i understand your problem correctly, your header.php file is included in every page. Though this header.php file contains code which is executed:

header.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. Maybe the header.php file is included in the index.php file as well.

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
// 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();
...
?>

This works for me. Using header is best, but has to be used before any other content is sent to the browser. Which, for me, developing in schmurdpress, makes it hard to implement.

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.

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

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