Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get client's domain name using php [duplicate]

Tags:

php

curl

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";
    }
like image 713
Ariff Azmi Avatar asked Jul 10 '26 05:07

Ariff Azmi


1 Answers

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.

like image 129
Vandal Avatar answered Jul 11 '26 20:07

Vandal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!