I just wondering how to get the real url of a url that redirect to other url for example this link http://hotscripts.com/jump.php?listing_id=51066&jump_type=0 Using php how can I get the target url?
function get_final_location($url) { $headers = @get_headers($url); $pattern = '/Location\s*:\s*(https?:[^;\s\n\r]+)/i'; if ($locations = preg_grep($pattern, $headers)) { preg_match($pattern, end($locations), $redirect); return $redirect[1]; } return $url; } PHP:
<?php /** * @author Interviolet * @package [$package] * @filename [$filename] * @copyright 2007 */ if( !function_exists('get_headers') ) { function get_headers( $url, $format = 0 ) { $host = parse_url( $url ); $headers = null; if( $host['query'] ) $host['path'] = sprintf( "%s?%s", $host['path'], $host['query'] ); if( ( $socket = fsockopen( $host['host'], $host['port'] ? $host['port'] : 80 ) ) ) { if( fwrite( $socket, sprintf( "HEAD /$host[path] HTTP/1.0\r\nConnection: close\r\n\r\n" ) ) ) { while( !feof( $socket ) and ( $buffer = fgets( $socket ) ) ) { if( trim( $buffer ) ) { if( $format ) { if( !count( $headers ) ) { $headers[0] = trim( $buffer ) ; } else { list( $key, $value ) = explode(':', $buffer, 2 ); $headers[ trim( $key ) ] = trim( $value ); } } else { $headers .= $buffer ; } } } fclose( $socket ); } } return $headers ; } } ?> PHP: get_headers for php4 and also ... $head = get_headers( "http://theurlthatforwardsthebrowser.com" ); echo $head['Location']; PHP:
Nice one, except that get_headers() automatically redirects if there's a "Location" header and returns the headers of the new locations as well. In some rare cases URLs are redirected multiple times, and if that's the case, the combination of both codes would not work properly.