简体   繁体   English

如果会话存在重定向到其他页面 PHP

[英]If session exists redirect to other page PHP

I have index.php and home.php pages.我有 index.php 和 home.php 页面。 index.php is like landing page, but if user is logged in or if session exist, i want to redirect him from index.php to home.php, if he tries to access index.php. index.php 就像登录页面,但如果用户已登录或会话存在,我想将他从 index.php 重定向到 home.php,如果他尝试访问 index.php。 And that redirect part of code is in header.php, which is part of code that is included both in home.php and index.php.代码的重定向部分位于 header.php 中,它是包含在 home.php 和 index.php 中的代码的一部分。 The problem is i think that i got stuck in redirect loop, ERR_TOO_MANY_REDIRECTS this is error i am getting.问题是我认为我陷入了重定向循环, ERR_TOO_MANY_REDIRECTS这是我得到的错误。 I think i need to say that if this is home.php stop redirecting, but i am not sure how to do that我想我需要说如果这是 home.php 停止重定向,但我不知道该怎么做

This is my code in header.php这是我在 header.php 中的代码

<?php include('database.php');
session_start();
if(isset($_SESSION['id'])) {
    header('Location: home.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>

home.php code home.php 代码

<?php include('header.php'); ?>
<?php include('nav.php'); ?>
<?php include('footer.php'); ?>

index.php code index.php 代码

<?php include 'header.php';?>
<?php include 'nav.php';?>

   //Some code not relevant to question

<?php include 'footer.php';?>

As many others have stated you end up in an infinite loop正如许多其他人所说,你最终陷入了无限循环

You could solve it like this, define eg RESTRICTED before the header on pages where user need to be logged in您可以像这样解决它,在用户需要登录的页面上的标题之前定义例如 RESTRICTED

home.php:主页.php:

<?php

define( 'RESTRICTED', true );

require( 'header.php' );

// etc

?>

index.php:索引.php:

<?php

require( 'header.php' );

// etc

?>

And the header:和标题:

include('database.php');
session_start();

if ( defined( 'RESTRICTED' ) ) {
    if ( !isset( $_SESSION['id'] ) ) {
      header( 'Location: index.php' );
      exit();
    }
}
else {
    if ( isset( $_SESSION['id'] ) ) {
      header( 'Location: home.php' );
      exit();
    }        
}

?>

EDIT:编辑:

In response to the logout issue, with logout button, send them to index.php?logout=true针对注销问题,使用注销按钮,将它们发送到 index.php?logout=true

index.php索引.php

<?php 

include('database.php');
session_start();

if ( defined( 'RESTRICTED' ) ) {
    if ( !isset( $_SESSION['id'] ) ) {
      header( 'Location: index.php' );
      exit();
    }
}
else {
    if ( isset( $_GET['logout'] ) ) {
      $_SESSION = array();
    }
    if ( isset( $_SESSION['id'] ) ) {
      header( 'Location: home.php' );
      exit();
    }        
}

?>

EDIT 2编辑 2

In reply to comment, an example on how to handle logged in users在回复评论中,一个关于如何处理登录用户的例子

In all restricted pages:在所有受限页面中:

define( 'RESTRICTED', true );
require( 'header.php' );

In all pages where you want to send users to home.php if they are logged in:在您希望将用户发送到 home.php(如果他们已登录)的所有页面中:

define( 'SEND_TO_HOME', true );
require( 'header.php' );

header.php:头文件.php:

<?php

if ( defined( 'RESTRICTED' ) ) {
    if ( !isset( $_SESSION['id'] ) ) {
      header( 'Location: index.php' );
      exit();
    }
}
else {
    if ( isset( $_GET['logout'] ) ) {
      $_SESSION = array();
    }
    if ( defined( 'SEND_TO_HOME' ) && isset( $_SESSION['id'] ) ) {
      header( 'Location: home.php' );
      exit();
    }
}

?>

Make a PHP file called authenticate.php制作一个名为authenticate.php的 PHP 文件

<?php
if(!isset($_SESSION['id'])) {
    header('Location: index.php');
    exit();
}
?>

those file need authentication include this on that file like in you home page use include('authenticate.php');那些需要身份验证的文件包含在该文件中,就像在您的主页中使用include('authenticate.php'); In you index.php file make a form like在你的 index.php 文件中制作一个像

<?php
<form action="process.php" method="post">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" value="login">
</form>
?>

In process.php do the followingprocess.php执行以下操作

<?php
$username = $_POST['username'];
$password = $_POST['password'];
//make authenticate this info to your database if user is valid then redirect to home page

session_start();
$_SESSION['id'] = 1;//here id will be the retrive id from your database
header('Location: home.php');
exit();
?>

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

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