How can the HTTP_REFERER be accessed in PHP when using a RewriteRule in .htaccess?
When using a RewriteRule in .htaccess to redirect requests, the HTTP_REFERER header may not be passed to the redirected page. To access the HTTP_REFERER in PHP, you can set a custom header in the RewriteRule to pass the referer information along with the request. Then, in the PHP code of the redirected page, you can retrieve the referer from the custom header. ```apache # .htaccess RewriteEngine On RewriteRule ^example$ example.php [L,E=HTTP_REFERER:%{HTTP_REFERER}] ```
// example.php
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
echo $referer;
Keywords
Related Questions
- What are some best practices for efficiently reading form data in PHP without repetitive code?
- Are there specific PHP frameworks or libraries that can facilitate the creation of a follow-up overview with search functionality?
- What are the advantages of using arrays to organize and sort file data in PHP?