How to Track File Redirection when using file_get_contents()?

Linda Hamilton
Release: 2024-11-01 13:19:02
Original
866 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!