简体   繁体   中英

If session exists redirect to other page PHP

I have index.php and home.php pages. 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. 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. The problem is i think that i got stuck in redirect loop, ERR_TOO_MANY_REDIRECTS this is error i am getting. I think i need to say that if this is home.php stop redirecting, but i am not sure how to do that

This is my code in 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

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

index.php code

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

home.php:

<?php

define( 'RESTRICTED', true );

require( 'header.php' );

// etc

?>

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

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

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:

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

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

<?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'); In you index.php file make a form like

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

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

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