开发者

PHP Redirect Loop

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, 开发者_如何学CI have no clue how to do this and I can't find any information on how to fix it. Anyone know a quick solution?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜