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
https://forums.phpfreaks.com/topic/197580-redirect-in-header/
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
https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1036937
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
https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1036939
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
https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1037098
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
https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1037192
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
https://forums.phpfreaks.com/topic/197580-redirect-in-header/#findComment-1037504
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.