The page isn't redirecting properly using isset
You are redirecting to index.php
, however index.php
already has header.php
included, which has the redirect login.
Try to redirect to a page like login.php
which does not have header.php
included.
You can also have a check in header, whether you are already in index.php
using PHP_SELF
, and not redirect.
if (!isset($_SESSION['username']) && !strstr($_SERVER['PHP_SELF'], "index.php")) {
header("Location:index.php");
}
Leroy Mikenzi
Updated on June 04, 2022Comments
-
Leroy Mikenzi almost 2 years
I'm getting The page isn't redirecting properly in mozilla and in chrome This webpage has a redirect loop .
This is what I'm trying to do. In my root directory there is an index.php and information.php also a folder common
Common fodler includes : common.php , db.php , header.php
In the index.php file in the root this is the code that calls the header.php from common folder.
index.php
<?php include('common/header.php'); ?> <p>this is some content in Index file.</p>
common.php
<?php @session_start(); // Retrieve username and password from database according to user's input if(isset($_POST['l_submit'])){ $user = $_POST['l_name']; $pass = $_POST['l_pass']; $stmt = mysql_query("SELECT * FROM users WHERE username = $user and password = $pass"); $num_rows = mysql_num_rows($stmt); // Check username and password match if($num_rows > 0) { // Set username session variable $_SESSION['username'] = $_POST['l_name']; // Jump to secured page header('Location:information.php'); }else{ echo "<p class='message' align='center'>Wrong Username OR Password...!!</p>"; } } ?>
and header.php
<?php include('db.php'); include('common.php'); if (!isset($_SESSION['username'])) { header("Location:index.php"); } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>..::University of Westminster::..</title> <link rel="stylesheet" href="css/stylesheet.css"/> <link rel="stylesheet" href="css/lightbox.css" media="screen"/> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script type="text/javascript" src="js/light.js"></script> <!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]--> <!--[if lte IE 7]> <script src="js/IE8.js" type="text/javascript"></script><![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" media="all" href="css/ie6.css"/><![endif]--> </head> <body>
I dont understand why there is a redirect loop occuring I'm calling the header.php file in my index.php file as you can see and the header.php file contain the isset function to check if the user exists and redirect also in the common.php file I'm setting the session variable.
-
Shankar Narayana Damodaran about 10 yearsTry adding an
exit;
after theheader
functions. -
Raptor about 10 years
-
Leroy Mikenzi about 10 years@ShankarDamodaran Nothing is happening same error. I had tried this earlier to stop this loop.
-
Raptor about 10 yearssidenote: never suppress warning / error if you are debugging your codes. Remove those
@
. -
Raptor about 10 yearssidenote: remember to call
exit;
afterheader("Location: $url");
if it is not the last line -
st15jap about 10 yearsInstead of using include try include_once
-
Raptor about 10 yearsinstead of using
include_once
, userequire_once
. Throwing error is better than throwing warning when the file trying to include is missing. -
Leroy Mikenzi about 10 years@all - include_once ,require_once , exit - have tried it all but shows nothing but the same error.
-
Raptor about 10 years@Lorenzo read the answer. Your code has logic issue. If the page is not sent by POST method,
$_SESSION['username']
is never assigned, and theheader('Location: index.php')
is reloadingindex.php
non-stop -
Leroy Mikenzi about 10 years@Raptor Please check my answer and it worked for me. Thanks for all you help.
-
-
Raptor about 10 yearsInstead of modifying the above logic, OP is better to re-design the flow.
-
KarelG about 10 years@Raptor : i agree with your opinion.
-
SajithNair about 10 yearsOk, I have given the check in
header.php
, so OP can go with current design -
Leroy Mikenzi about 10 years@SajithNair Kindly check my answer and it worked. Thanks for all your help.
-
Raptor about 10 yearsThat's incorrect. Your
header()
function sends the rest of the page toinformation.php
too. Remember to addexit
afterheader('Location: xxx');
. Also, as said,require_once()
is better thaninclude_once()
-
Leroy Mikenzi about 10 years@Raptor Yes sure will deifnately do that . Thanks again for the help really appreciate.