Get request domain using php

15,060

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.

Share:
15,060
Ariff Azmi
Author by

Ariff Azmi

Updated on June 22, 2022

Comments

  • Ariff Azmi
    Ariff Azmi almost 2 years

    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";
        }