Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Scheme and Host from HTTP_REFERER

Tags:

string

php

I have $_SERVER['HTTP_REFERER'] — pretend it is http://example.com/i/like/turtles.html. What would I need to do to get just the http://example.com part out of the string, and store it in its own variable?

like image 887
jiexi Avatar asked Jul 17 '09 18:07

jiexi


1 Answers

In this example, the best solution would be to use PHP's parse_url method. This splits up the URL into an associative array. You would then build your final value by combining the scheme with the host:

if ( $parts = parse_url( "http://example.com/i/like/turtles.html" ) ) {
    echo $parts[ "scheme" ] . "://" . $parts[ "host" ];
}
like image 80
Sampson Avatar answered Oct 01 '22 14:10

Sampson