0

On my index page I have a link to my login.php page with this code:

      <?php     
    if(isset($_SESSION['username'])) { 
    echo "<div id='logout'><a href='logout.php'>Logout (".$_SESSION['username'].")</a></div>";
    } else { 
    echo "<div id='login'><a href='login.php'>Login (Regular)</a></div>";
    }      
    ?>

On the login.php page I have

<?php
include('check.php');

$ref = getenv('HTTP_REFERER');

if (isset($ref)) {
  header("Location: " . $ref);
  exit;
} else {
  header("Location: index.php");
  exit;
}
?>

check.php is the code for the login form and it checks the users level to make sure they can access the page. I was told that I need to add a check to see if the referral is login.php, otherwise it will go in an infinite loop and I am of course getting "This webpage has a redirect loop". However, I have no clue how to do this and I can't find any information on how to fix it. Anyone know a quick solution?

Alex
  • 3
  • 2
  • 1
    bad code bad bad code very bad code HTTP_REFERER is nothing you would want to rely on it can be very easily manipulated and isn't even set by some browsers... if anything use the session to check stuff – Breezer Sep 02 '11 at 09:19

1 Answers1

0

You should be able to just do

if (isset($_SERVER['HTTP_REFERER']) && end(explode('/',$_SERVER['HTTP_REFERER'])) != 'login.php') {
  header("Location: " . $_SERVER['HTTP_REFERER']);
  exit;
} else {
  header("Location: index.php");
  exit;
}

Note that this is a simplified code - you may need to be a bit smarter than that.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Yeah, got a Fatal error: Call to undefined function on the if line. – Alex Sep 02 '11 at 09:20
  • 1
    I updated the code. However you should actually understand the code provide and not blindly copy it. We gladly help others, but don't like doing their work for them. – Aleks G Sep 02 '11 at 09:55
  • :) It worked. Thanks. Yeah, I'm trying to learn PHP but I just couldn't figure this one out. I will try and be better about that. – Alex Sep 02 '11 at 10:15