douzai2562 2014-02-17 21:28
浏览 50

在PHP中获取Referer URL和FOLDER名称

I have several folders on my domain, within each folder contains an index.php file that checks to see if the database connection passes or fails, if it fails, the page is redirected to a top level file (outside of all folders) called offline.php. This part works great. The basic format I'm using to redirect if the db is offline is:

if ( !$dbconnect ) {
header("Location: https://www.test.com/offline.php");
}

Then, within the offline.php page, I need to check to see which folder brought the user to the offline.php page, and display a unique message to the user - based on the folder that brought them to the offline.php page.

For example:

test.com/test1/index.php redirects to offline.php, the message would say 'test1 brought you to this page'

test.com/test2/index.php redirects to offline.php, the message would say 'test2 brought you to this page'.

In multiple browsers I've tried the following code, which always results in 'unknown uri':

$url = 'https://' . $_SERVER['HTTP_REFERER'] ;
if ( strpos($url,'test') !== false ) {
echo 'test';
} elseif ( strpos($url,'test1') !== false ) {
echo 'test1';
} elseif ( strpos($url,'test2') !== false ) {
echo 'test2';
} else {
echo 'unknown uri';
}

Suggestions?

EDIT Due to the unreliable nature of HTTP_REFERER I've decided to put all of the conditions within the index.php page and forget about the offline.php page. A HUGE thank you to everyone who offered suggestions!

  • 写回答

2条回答 默认 最新

  • doufan6544 2014-02-17 21:33
    关注

    If you use the following code on your offline.php page, you can see all of the $_SERVER variables available (referring URL is in there)

    echo '<pre>',print_r($_SERVER),'</pre>';
    

    From there, you can take $_SERVER['HTTP_REFERER'] use a select case, or if then statement and accomplish your goal.

    Based on some of your questions in the comments and people pointing out the use of $_SERVER['HTTP_REFERER'] being unreliable, you could do something like this instead.

    On your index.php page with the dbconnect check, you could modify it to be something like this. header("Location: https://www.test.com/offline.php?org=".urlencode($_SERVER['REQUEST_URI']));

    Then, on the offline.php,

    $page = urldecode($_GET['org']);  
    $org = explode('/',$page);
    

    echo $org[1] to get the first value after the slash, $org[2] would get the next value etc..

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题