Home >Backend Development >PHP Tutorial >How to Track File Redirection when using file_get_contents()?
File Redirection Tracking with file_get_contents
When utilizing file_get_contents(), it's possible to obtain content from external URLs even if they redirect to different locations. However, determining the actual target URL after such redirection can prove challenging.
Obtaining the Redirected URL
To address this, consider disabling automatic redirection handling with file_get_contents(). This can be achieved through the stream_context_create() function:
<code class="php">$context = stream_context_create( array( 'http' => array( 'follow_location' => false ) ) ); $html = file_get_contents('http://www.example.com/', false, $context);</code>
By setting follow_location to false, the script won't follow any redirects. To retrieve the redirected URL, examine the $http_response_header variable:
<code class="php">var_dump($http_response_header);</code>
This will provide an array containing response headers, including the actual target URL in the Location header.
The above is the detailed content of How to Track File Redirection when using file_get_contents()?. For more information, please follow other related articles on the PHP Chinese website!