Jump to content

Getting window elements (URL, title, etc.) with PHP?


stickynote427

Recommended Posts

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.

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.

:( 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).

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.