繁体   English   中英

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

[英]If session exists redirect to other page PHP

我有 index.php 和 home.php 页面。 index.php 就像登录页面,但如果用户已登录或会话存在,我想将他从 index.php 重定向到 home.php,如果他尝试访问 index.php。 代码的重定向部分位于 header.php 中,它是包含在 home.php 和 index.php 中的代码的一部分。 问题是我认为我陷入了重定向循环, ERR_TOO_MANY_REDIRECTS这是我得到的错误。 我想我需要说如果这是 home.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 代码

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

index.php 代码

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

   //Some code not relevant to question

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

正如许多其他人所说,你最终陷入了无限循环

您可以像这样解决它,在用户需要登录的页面上的标题之前定义例如 RESTRICTED

主页.php:

<?php

define( 'RESTRICTED', true );

require( 'header.php' );

// etc

?>

索引.php:

<?php

require( 'header.php' );

// etc

?>

和标题:

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();
    }        
}

?>

编辑:

针对注销问题,使用注销按钮,将它们发送到 index.php?logout=true

索引.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();
    }        
}

?>

编辑 2

在回复评论中,一个关于如何处理登录用户的例子

在所有受限页面中:

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

在您希望将用户发送到 home.php(如果他们已登录)的所有页面中:

define( 'SEND_TO_HOME', true );
require( '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();
    }
}

?>

制作一个名为authenticate.php的 PHP 文件

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

那些需要身份验证的文件包含在该文件中,就像在您的主页中使用include('authenticate.php'); 在你的 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>
?>

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