There is a link for example

When you click on it throws for example an some

How to programmatically determine the url address of this

Google hour did not give anything, I do not even know which way to dig.

Find out the address where the redirect occurs, for example: I have a link when clicking on it will forward me to the main page of the hashcode, I need to pass a parameter to the script, for example $ url = "http: // vk. cc / cObdk "; he gave me $ endurl = "";

I tried this:

$ch = curl_init(); //GET запрос указывается в строке URL curl_setopt($ch, CURLOPT_URL, $pley[1]); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_REFERER, 'http://сайт.com/'); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2'); $endurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); $data = curl_exec($ch); curl_close($ch); 

But in the end I get the source address, not the final one.

  • I didn't understand anything at all ... What does it mean to define? - Palmervan
  • find out the address where the redirect occurs, an example I have is the link when clicking on it will send me to the main page of the hashcode, I need to pass a parameter to the script such as $ url = " "; he gave me $ endurl = " "; - arashvg
  • Why curl to redirect? - Palmervan

5 answers 5

 $url = array ( 'blabla' => '', 'ololo' => '' ); if(isset($_GET['url'])) { if(isset($url[$_GET['url']])) { echo 'Redirect url: ' . $url[$_GET['url']]; } else { echo 'URL Not Found'; } } //?index.php?url=blabla => //?index.php?url=ololo => 

All the same can be done when querying the database.


 CREATE TABLE `redirect` ( `id` int(11) NOT NULL auto_increment, `short_code` varchar(255) NOT NULL, `url` varchar(255) NOT NULL, PRIMARY KEY(`id`), UNIQUE KEY(`short_code`) ); INSERT INTO `redirect` VALUES('1', 'ololo', ''); if(isset($_GET['url'])) { $query = musql_query("SELECT * FROM `redirect` WHERE `short_code` = '".$_GET['url']."'"); //Дальше сами) } 
     $url = ''; $headers = get_headers($url, 1); print "<pre>"; print_r($headers['Location']); 
    • Welcome to Stack Overflow in Russian ! Please try to leave a little more detailed answers. - aleksandr barakin
    • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky

    A full example of the function can be found at: Follow redirects

      In general, this URL honestly gives 302:

       wget -S "" --15:58:47-- => `cObdk' Resolving done. Connecting to[]:80... connected. HTTP request sent, awaiting response... 1 HTTP/1.1 302 Found 2 Server: nginx/1.2.4 3 Date: Mon, 05 Nov 2012 11:58:47 GMT 4 Content-Type: text/html; charset=windows-1251 5 Content-Length: 0 6 Connection: keep-alive 7 X-Powered-By: PHP/5.2.6-1+lenny8 8 Pragma: no-cache 9 Cache-control: no-store 10 Location: 11 Vary: Accept-Encoding Location: [following] 

      See what you have in CURLINFO_HTTP_CODE. If something is different, then something is wrong somewhere :) Yes, and enable CURLOPT_HEADER and see what comes up in the headers.

         $endurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
        • Can you write more detailed and detailed answers? - Vadim Ovchinnikov
        • of course I can) is it possible to explain something in this line? same direct answer to the question. quite exhaustive - Ledd