Jump to content


BrowserBugs

Member Since 07 Feb 2012
Online Last Active Today, 12:23 PM
*****

Posts I've Made

In Topic: Problems caused by a 404 error page

Today, 09:41 AM

When viewing the 404.php page it still throws up and error:

Warning: Cannot modify header information - headers already sent by (output started at /home/ /public_html/somewhere.co.uk/404.php:1) in /home/ /public_html/somewhere.co.uk/404.php on line 38

 

It does not show up if I rem out:

header('HTTP/1.0 404 Not Found');

 

This is after changing the top line to:

<?php function tellOwner($var) { 
$ip = getenv ("REMOTE_ADDR");
....

The email function works.

 

Take a look at https://stackoverflo...nt-error-in-php as they really cover in depth the typical causes, they also cover using http://php.net/headers_sent to check. Removing the header('HTTP/1.0 404 Not Found'); would stop the error, but i'm pretty sure if you check the 404.php direct without it using http://www.webconfs.com/http-header-check.php you'd be getting a 200 ok which defies the point.


In Topic: Problems caused by a 404 error page

17 November 2017 - 01:10 PM

 

Thanks very much for your continued help, it is looks like something that I am after.

 

What I am getting though is an error:

Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/somewhere.co.uk/404.php:1) in /home/public_html/somewhere.co.uk/404.php on line 41

 

 

If I rem out header('HTTP/1.0 404 Not Found'); then the above error does not appear.

 

Any ideas what it could be?

 

 

Sorry probably my bad, the notes and line break at the top can trigger this, change at the top;

<?php function tellOwner($var) { // << Notice no gap 
$ip = getenv ("REMOTE_ADDR");
....

... this would have been triggered as you were looking at /404.php.

 

Edit: Ah hang on also check your mail loop doesn't echo $message, your original script had it in.


In Topic: Problems caused by a 404 error page

17 November 2017 - 11:29 AM

 

BrowserBugs  Thanks for the code, this looks like something more useful than what I currently have. So what I need to do is include that code into <head></head>

 

 

No it needs to go above the head. The thing with headers is once they are sent they can't be resent. When you request a php page anything above the opening <html> is processed before any headers are returned, like the server pauses before returning the header. Normally you request a file, the server looks for the file and if it exists displays it, this by default has a 200 ok header, and if it does not find the file it returns a 404 header, and if you have a custom 404 page it will use that.

 

What you can do with PHP is change headers before the output, and then choose what you want to do about it. This is very powerful if you want clean urls without relying upon the band aid canonical link, but that's another topic.

 

So I've made a basic page for this, you can customise your heart out with styles etc but the principal is the same. Also wrapped your mailer into a function, then you can just call it in any case you want an email, saves it firing all the time.

<?php
// New function if your mailer is working fine, I've not tested the mailer.
function tellOwner($var) {
$ip = getenv ("REMOTE_ADDR");
$requri = getenv ("REQUEST_URI");
$servname = getenv ("SERVER_NAME");
$combine = $ip . " tried to load " . $servname . $requri ;
$httpref = getenv ("HTTP_REFERER");
$httpagent = getenv ("HTTP_USER_AGENT");
$today = gmdate("l, j F Y, g:i a ");
$note = "Page Not Found" ;
$message = " ";
$message2 = "$today \n
$combine \n
User Agent = $httpagent \n
$note \n
$httpref ";
$to = "jbloggs@somewhere.co.uk";
$subject = "404 Error Page";
$from = "From: jbloggs@somewhere.co.uk\r\n";
mail($to, $subject, $message2, $from);
}
$site_url = "http://www.somewhere.co.uk";
if($_SERVER['HTTP_REFERER']!="") { // Gets the referrer
  if(substr($_SERVER['HTTP_REFERER'], 0, strlen($site_url))==$site_url) { 
    // If the referrer start is the same as $site_url then they clicked a link from your website.
    $msg = "<p>Please accept our apologies for this, it appears this broken. Head back to our <a href=\"/\">home page</a>.</p>";
    tellOwner(); // New function to send a mail
  } else {
    // They clicked a link from a 3rd party website.
    $referer = parse_url($_SERVER['HTTP_REFERER']); $referer = $referer['host'];
    $msg = "<p>You were incorrectly referred to this page by <b>" . $referer . "</b>, not too sure why. Head back to our <a href=\"/\">home page</a>.</p>";
    tellOwner(); // New function to send a mail
  }
} elseif(substr($_SERVER['REQUEST_URI'], 0, strlen("/404.php"))=="/404.php"){
    $msg = "<p>It appears you wanted to see our super duper 404 error page? Well here it is in all its glory; hope you like it!</p>";  
    // Because they went to http://www.somewhere.co.uk/404.php it does exist. The server wants to return a header of 200 ok, but we don't want that else we end up with /404.php say in the index, so instead we change to 404 headers so the search engine or crawler thinks it's not here.
    header('HTTP/1.0 404 Not Found');
} else {
	$msg = "<p>Sorry but the page you have requested cannot be accessed, this could possibly be a mis-typed URL or out-of-date bookmark. Head back to our <a href=\"/\">home page</a>.</p>";
}
 ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>404 | Page Not Found</title>
<meta name="robots" content="noindex">
</head>

<body>
<h1>Page not found</h1>
<?php echo $msg; ?>
</body>
</html>

In Topic: Problems caused by a 404 error page

17 November 2017 - 09:07 AM

Wow, brave move on the email for every 404 but kudos for wanting to investigate making a better user experience. First, can I just urge you to take off the 301 all to the home page, it defeats the purpose of 404 and also leaves you wide open to potential problems. 301 tells the search engines 'hi, I know the page you wanted but I have permanently moved the content to this new page', and in a sinister context if I linked to a random url on your site such as 'yourdomain.com/cheap_meds' the answer to the search engine would be 301, the cheap meds content has moved to the home page instead of 404 wtf are you asking for.

 

Google Search Console will let you know about 404s where the page was indexed but has disappeared, but tracking every 404 would be time consuming, urls on a domain root are pretty much infinite in combinations and parameters. I would suggest not using tracking code on the 404 page, instead make a minimal design page.

 

Take a look at what I use, this is in the head of the 404 page assuming it's kept at root (/404.php) and htaccess uses;

<?php 
$site_url = "http://www.yourdomain.com";
if($_SERVER['HTTP_REFERER']!="") { // They were referred.
   if(substr($_SERVER['HTTP_REFERER'], 0, strlen($site_url))==$site_url) {
        // They followed a link on your site to this location, possibly use your email idea here.
        $msg = "<p>Sorry message for the broken link as it's your fault.</p>";
    } else {
        // They were referred here from somewhere else, you might want to know this and ask them to update their link.
        $referer = parse_url($_SERVER['HTTP_REFERER']);$referer = $referer['host'];
        $msg = "<p>You were incorrectly referred to this page by <b>" . $referer . "</b>.</p>";
    }
} elseif(substr($_SERVER['REQUEST_URI'], 0, strlen("/404.php"))=="/404.php"){
    // They wanted to see your 404 page ;) - remember the php version shouldn't exist directly
    $msg = "<p>It appears you wanted to see our super duper 404 error page? Well here it is in all its glory; hope you like it!</p>";
    header('HTTP/1.0 404 Not Found');
} elseif(substr($_SERVER['REQUEST_URI'], 0, strlen("/wp-login.php"))=="/wp-login.php" || substr($_SERVER['REQUEST_URI'], 0, strlen("/wp-admin/"))=="/wp-admin/"){
    // They think you run WordPress
    $msg = "<p>It appears you think this is built with WordPress - Hahahahahahaha!</p>";
} else {
    $msg = "<p>Sorry but the file you have requested cannot be accessed, this could possibly be a mis-typed URL or out-of-date bookmark.</p>";
}
echo $msg; ?>

I'm racking my brains as I read a great blog article on ways to improve error pages, if I can remember where I'll post it.

 

Edit: Forgot to say in all cases of messing around with headers make sure you test them, I use http://www.webconfs....eader-check.php


In Topic: Any lightweight alternative of Wordpress?

16 November 2017 - 01:54 PM

 

Yeah, I have this debate at work quite a lot. You always get a portion of clients that say they want a CMS, and never use it. Or, you get people saying "well, there's a possibility they will need to do X feature in the future", but then that never happens anyway.

 

Hahaha and most don't even know what CMS stands for! I just helped a company out getting a site back online. The host was very helpful and when i said 'I'll transfer the site' they tried to be helpful and installed WordPress. My life, took ages to delete all those files, it had overwritten loads in the ini and omg the wildcard htaccess, what a mare!