doumaque6551 2015-10-15 06:35
浏览 36
已采纳

使用php获取请求域

I have problem with getting domain name using HTTP_REFERER. The condition is like this:

http://www.example.com send a curl post to my server. The things is, example.com does not send their url in curl_setopt(CURLOPT_REFERER). So is it possible on my server side to get their domain name ?

Thanks a lot for helping me

My code so far:


on abc.com side

$data = array('username' => $username ,
              'email' => $email,
              'phone' => $phone );

    $string = http_build_query($data);
    // For debugging purpose
    // echo $string;

    $ch =  curl_init("http://localhost/test/str_pos.php");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    curl_close($ch);

    header("Location: str_pos.php");

On my server side:

$domain = parse_url($_SERVER['HTTP_REFERER']);

    if (isset($domain['host'])) {

        echo $domain['host'];
    }
    else{
        echo "No host found";
    }
  • 写回答

1条回答 默认 最新

  • drgd73844 2015-10-15 07:01
    关注

    I would advise a different command. Instead of HTTP_REFERER try HTTP_HOST or SERVER_NAME.

    Example:

    $domain = parse_url($_SERVER['HTTP_HOST']);

    $domain = parse_url($_SERVER['SERVER_NAME']);


    HTTP_REFERER - The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

    Source http://php.net/manual/en/reserved.variables.server.php

    Check out the following threads here and here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件