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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.