Is there way to track web URL redirect with PHP?


For example, when I click on some link, my web browser redirect me to completely different page or even website. I know I can use PHP header() function to do redirect within my php code, or I can redirect with commands in the .htaccess But how can I check in hpp code if there is redirects behind URL? Thanks in advance.

You can use curl to read http headers section of url - there is information about redirects, location of redirect etc. You will just need to parse headers and check if there are redirects and what is destination location. I’ll create a small php script and post it here later.
Also, if you are on Linux, you can run curl in command line to trace sequence of redirects. Here is an example of such command:

curl -I -L

And here is the output for the command above:

$ curl -I -L HTTP/1.1 301 Moved Permanently Date: Tue, 16 Nov 2010 21:52:37 GMT Server: Apache/2.2.3 (CentOS) Location: Vary: Accept-Encoding Connection: close Content-Type: text/html; charset=iso-8859-1

The shortest php code for redirect checker would be this:
exec(“curl -I -L”,$out);
echo implode("

If you want to use php curl library, you will have to loop through each location in redirects sequence (if there are more than one), parse headers for each redirect to determine new location, and so on.

Sponsor our Newsletter | Privacy Policy | Terms of Service