The page isn't redirecting properly using isset

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.


<p>this is some content in Index file.</p>


    // Retrieve username and password from database according to user's input

        $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
            echo "<p class='message' align='center'>Wrong Username OR Password...!!</p>";

and header.php

if (!isset($_SESSION['username'])) {
<!DOCTYPE html>
<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=""></script>
<script type="text/javascript" src="js/light.js"></script>

<!--[if IE]>
    <script src=""></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]-->

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.


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")) {

If you don't have a session for a user that is logged in, you'll toss them around endlessly.

In your index you include header.php, which if you aren't logged in will send you back to index.php, where the same issue applies as long as there's no session. Since you're being tossed around endlessly you don't get any chance of making this session.

This is what I did to make it work with the current logic . I just had to alter index.php and rest stays the same.

in index.php

if(isset($_SESSION['username'])) {
  header("Location: information.php");
   <div class="row">

I checked here whether the session is set and redirected it to information.php file.

Need Your Help

Bool Variable and string value compare in C#

c# string compare

If I declared a bool isTrue = false; // init it to false

Spring 4 Security access denied (user is not anonymous)

java spring spring-mvc spring-security

I'm trying to login using spring security, but i'm getting an error access denied.