Jump to content

Redirect in header


RobMs

Recommended Posts

Hey all,

 

I have a "header" page that I include in every page. If I want to put a script in their to redirect to a page if not logged in, how can I do this without creating a redirect loop? (which would be caused because it would be included in the login page too).

for example using: header ("Location: index.php"); in index.php would create a redirect loop.

 

Thanks,

Link to comment
Share on other sites

Should be fairly simple and straightforward.

 

<?php
session_start(); //You need this at top on every page using sessions.

if(!isset($_SESSION['id'])) {
   header('login.php');
   exit; //Required
}

 

That'll redirect them to wherever you wish if their session isn't set (IE: Their ID). Change 'id' to whatever session variable you use for them when logged in.

Link to comment
Share on other sites

But that will cause a loop on login.php?

 

You could place unique code on just login.php:

if (isset($_SESSION['id'])) {
   print "You are already logged in!";
   exit;
} else {
   //<form .. log in form.
}

 

Of course that is only a crude example, but as you see it's fairly simple to implement. Once the session is set, It will not let them 're-login' and it will bypass the checks on other pages as the session's ID value will be there.

Link to comment
Share on other sites

in any page where you want the user to be logged in add header("Location: login.php") if they aren't logged in. Then in login.php if the user is logged in redirect to index.php (since your logged in this wont be a recuring loop) or if they aren't print the form

Link to comment
Share on other sites

Thanks, I still dont think you all understand.

ok file header.php is included in EVERY page, this would include login.php

This means:

if (!loggedIn()) {
	header ("Location: login.php");
	exit;
}

would be included in login.php also, thus login.php would loop. I want to be able to stop it looping in this page.

Link to comment
Share on other sites

You can check for the login.php filename and if found, then don't redirect to login, like

 

if(!loggedIn() && basename($_SERVER['PHP_SELF']) != 'login.php') // if not logged in AND the current script is not login.php
{
   
   header ("Location: login.php");
   exit;  
}

Link to comment
Share on other sites

Thanks, I still dont think you all understand.

ok file header.php is included in EVERY page, this would include login.php

This means:

if (!loggedIn()) {
	header ("Location: login.php");
	exit;
}

would be included in login.php also, thus login.php would loop. I want to be able to stop it looping in this page.

 

Sorry i didnt realise

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.