Skip to content
PHP Logo

How can one prevent automatic redirection when including a file from a remote server in PHP?

When including a file from a remote server in PHP, the server may automatically redirect the request, causing unexpected behavior. To prevent this, you can use the `CURLOPT_FOLLOWLOCATION` option with cURL to disable automatic redirection.

php
<?php
$url = 'http://example.com/remote_file.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); // Disable automatic redirection
$response = curl_exec($ch);
if($response === false) {
    echo 'Error: ' . curl_error($ch);
} else {
    echo $response;
}
curl_close($ch);
?>