开发者

How to check in PHP whether page is directly loaded or being redirected from other page?

开发者 https://www.devze.com 2023-03-19 12:43 出处:网络
Suppose I am having two pages Page1.php and P开发者_运维知识库age2.php. Sometimes Page1.php is directly loaded and sometimes it is being redirected from Page2.php. My question is that can I check whet

Suppose I am having two pages Page1.php and P开发者_运维知识库age2.php. Sometimes Page1.php is directly loaded and sometimes it is being redirected from Page2.php. My question is that can I check whether Page1.php is directly loaded or it's being redirected from the Page2.php.(if query string is not used)


Set a session variable in Page2.php and control for it in Page1.php

Page2.php

<?php
session_start();
$_SESSION['from2'] = true;
header('Location: /Page1.php');
?>

Page1.php

<?php
session_start();
if(isset($_SESSION['from2']) && $_SESSION['from2']) {
  /* from Page2.php logic here */

  unset($_SESSION['from2']);
}else{
  /* not from Page2.php */
}
?>


You can read the value of global variable $_SERVER['HTTP_REFERER'].


$_SERVER['HTTP_REFERER']

Though check out the manual to see why it cannot be trusted: http://uk3.php.net/manual/ro/reserved.variables.server.php

0

精彩评论

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

关注公众号