stickynote427 Posted April 10, 2009 Share Posted April 10, 2009 I have a custom 404 error page for my website. When a visitor of my site tries to load a folder or file that doesn't exist, this error page is loaded. When this page is loaded, I want the URL of the page that the visitor tried to load sent to my email using PHP. However, I cannot get the URL of the actual page, just the 404 page (I tried using $_SERVER['PHP_SELF'], but that returns "404.php", which is not what I want). How can I get the URL of the page the visitor tried to load using PHP? For example, if someone tried to load "http://www.mysite.com/mypage.php," and it didn't exist, I want that URL sent to me. Thanks. Quote Link to comment Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 you could try $_SERVER['HTTP_REFERER'] and see what that outputs. The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted. Not sure how else to get it otherwise. Quote Link to comment Share on other sites More sharing options...
stickynote427 Posted April 11, 2009 Author Share Posted April 11, 2009 I don't think that's the solution. As far as I know, that will just return the page that I just came from (if I clicked on the bad link or typed in a bad URL from ../page1.php, the page I was just on would be returned, which in this case would be page1.php). Quote Link to comment Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted April 11, 2009 Share Posted April 11, 2009 You can get it by .htaccess/php .htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ somepage.php?page=$1 [QSA,L] Now every request that not a file or a directory will be sent to somepage.php In your php you can get it by <?php echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?> Or if you don't need it when the query is made you can also make a cronjob to parse the apache access_log and get all the line with a 404 response and put that in a db or whatever you want. Quote Link to comment Share on other sites More sharing options...
stickynote427 Posted April 11, 2009 Author Share Posted April 11, 2009 Grrr, I just figured out that $_SERVER['REQUEST_URI'] was all I needed... I just never bothered to email that to myself. T_T Sorry for the trouble. Quote Link to comment Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 ah nice. good to know. thx theonlydrayk. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.