The page isn't redirecting properly using isset

10,669

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");
}
Share:
10,669
Leroy Mikenzi
Author by

Leroy Mikenzi

Updated on June 04, 2022

Comments

  • Leroy Mikenzi
    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
      Shankar Narayana Damodaran about 10 years
      Try adding an exit; after the header functions.
    • Raptor
      Raptor about 10 years
      sidenote: stop using deprecated mysql_* functions. use MySQLi or PDO instead. Here is a good tutorial for PDO. Also, your code is subjected to SQL Injection attack, as you directly allow POST values to be inserted in your query.
    • Leroy Mikenzi
      Leroy Mikenzi about 10 years
      @ShankarDamodaran Nothing is happening same error. I had tried this earlier to stop this loop.
    • Raptor
      Raptor about 10 years
      sidenote: never suppress warning / error if you are debugging your codes. Remove those @.
    • Raptor
      Raptor about 10 years
      sidenote: remember to call exit; after header("Location: $url"); if it is not the last line
    • st15jap
      st15jap about 10 years
      Instead of using include try include_once
    • Raptor
      Raptor about 10 years
      instead of using include_once, use require_once. Throwing error is better than throwing warning when the file trying to include is missing.
    • Leroy Mikenzi
      Leroy Mikenzi about 10 years
      @all - include_once ,require_once , exit - have tried it all but shows nothing but the same error.
    • Raptor
      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 the header('Location: index.php') is reloading index.php non-stop
    • Leroy Mikenzi
      Leroy Mikenzi about 10 years
      @Raptor Please check my answer and it worked for me. Thanks for all you help.
  • Raptor
    Raptor about 10 years
    Instead of modifying the above logic, OP is better to re-design the flow.
  • KarelG
    KarelG about 10 years
    @Raptor : i agree with your opinion.
  • SajithNair
    SajithNair about 10 years
    Ok, I have given the check in header.php, so OP can go with current design
  • Leroy Mikenzi
    Leroy Mikenzi about 10 years
    @SajithNair Kindly check my answer and it worked. Thanks for all your help.
  • Raptor
    Raptor about 10 years
    That's incorrect. Your header() function sends the rest of the page to information.php too. Remember to add exit after header('Location: xxx');. Also, as said, require_once() is better than include_once()
  • Leroy Mikenzi
    Leroy Mikenzi about 10 years
    @Raptor Yes sure will deifnately do that . Thanks again for the help really appreciate.