-2

I made a script to get some urls, in fact when this script for example get abcd.com and efgh.com this domain is redirected to zxyw.com and I want just to get zxyw.com like result my question is how to get the redirected domain with PHP

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • Do you mean which of the following domains did the user come from? – Alicia Sykes May 11 '14 at 11:17
  • Please precise if you want the referer or the active domain. And look to $_SERVER doc. $_SERVER['SERVER_NAME'] gives you the active domain, and $_SERVER['HTTP_REFERER'] gives you the referer. – Vincent Decaux May 11 '14 at 11:21
  • If you have the curl extension installed, you can use the curl handle and obtain the *effective url*, that is the one you're looking for: `$urlEndpoint = curl_getinfo($handle, CURLINFO_EFFECTIVE_URL);` - see as well http://stackoverflow.com/a/9571305/367456 – hakre May 11 '14 at 11:24

2 Answers2

1

You can use $_SERVER['HTTP_REFERER'] to get the complete URL of where the user came from.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Alicia Sykes
  • 5,997
  • 7
  • 36
  • 64
0

If I understand correctly what you want to do then something like this should work. Assuming you have the curl extension installed:

   <?php
   $ch = curl_init("http://www.link1.com/whatever");

   curl_setopt($ch, CURLOPT_HEADER, true);
   curl_setopt($ch, CURLOPT_NOBODY, true);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

   $response = curl_exec($ch);
   curl_close($ch);

   $header = "Location: ";
   $pos = strpos($response, $header);
   $pos += strlen($header);
   $redirect_url = substr($response, $pos, strpos($response, "\r\n", $pos)-$pos);
   echo $redirect_url;
   ?>
Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 1
    *If* I would have the curl extension installed I would have done it differently, compare http://stackoverflow.com/a/14436877/367456 and http://stackoverflow.com/a/9571305/367456 – hakre May 11 '14 at 11:21
  • 1
    That is an exact copy paste from this [**answer**](http://www.webmasterworld.com/php/3681920.htm). Please add the source from where you copied the content from. Thanks :) – Shankar Narayana Damodaran May 11 '14 at 11:23
  • you code return sometimes **00 OK** – Yassir Gourram May 11 '14 at 11:37
  • @YassirGourram: That code is fragile. [See my comment](http://stackoverflow.com/questions/23591875/how-to-get-the-redirected-domain#comment36210581_23591875) how it works if you're using curl. – hakre May 11 '14 at 11:48
  • Yes, it is a cut& paste I've tried on my test server what's wrong? – Simone Spagna May 11 '14 at 11:59
  • BTW, I think ther is a good starting point to appreciatre the problem. Dear colleague your contribute will allways appreciated. ciao – Simone Spagna May 12 '14 at 08:35