Đặng Quốc Trung
Đặng Quốc Trung

Reputation: 123

Detect if was redirected from specific urls or domains

Like If i'm on a url like this: "http://specificdomain.com/path/".
and i clicked on a button on that page with a code like this:

<a href="http://afterclicked.url/path/">Go!!!</a>

How can i detect if i was redirect from that domain or that url (http://specificdomain.com/path/) but not any else/not directed open. edit: and the "afterclicked.url" is my website, so that is the place that have the code to detect.

Upvotes: 3

Views: 399

Answers (1)

Alexandre Tranchant
Alexandre Tranchant

Reputation: 4580

$_SERVER['HTTP_REFERER'] will give you the referrer page's URL. If users had click on a bookmark or directly visit your site by typing the URL in the navigation bar, http_referer will be empty.

You can analyse these variable by using : parse_url

<?php
    $url = 'http://username:[email protected]:9090/path?arg=value#anchor';

    echo parse_url($url, PHP_URL_HOST);

Output will be : afterclicked.url

In your case, use this :

<?php
    echo parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);

Here is a sandbox to test it

Be careful, some browsers don't provide the HTTP_REFERER to respect user's privacy.

Upvotes: 2

Related Questions