Skip to content

Instantly share code, notes, and snippets.

@atsea
Created October 7, 2014 14:46
Show Gist options
  • Save atsea/7a216554e64a573600b5 to your computer and use it in GitHub Desktop.
Save atsea/7a216554e64a573600b5 to your computer and use it in GitHub Desktop.
Enahnce 404 Error Page
<div class="entry">
<!--If no results are found-->
<h1><?php esc_html_e('Oops!','Nimble'); ?></h1>
<h3 style="margin-bottom:3em">The page you requested, <em style="color:#00539f">'<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] ; ?>'</em> could not be found. </h3>
<p>Choose a search option below or use the navigation above to locate your software.</p>
<?php get_search_form();
/**
* Enhance 404 Error Page
* http://codemug.com/php/enhance-404-error-page-make-it-powerful/
*/
#Set your site's URL, no '/' in the end.
$MyURL = (isset($_SERVER['HTTP_HOST'])) ? str_replace('http://','',strtolower(rtrim($_SERVER['HTTP_HOST'],'/'))) : 'udeploy.udel.edu';
#Set your site's domain, no '/' in the end.
$MyDomain = (isset($_SERVER['HTTP_HOST'])) ? 'http://'.str_replace('http://','',strtolower(rtrim($_SERVER['HTTP_HOST'],'/'))) : 'http://udeploy.udel.edu';
//Set timezone, no need to change
date_default_timezone_set('America/New_York');
//Header
header('Content-type:text/html; charset=utf-8');
/*
Send Email
Arguments:
receiver,title,content,(use '\n' to between lines, 70 characters max limited for each line)
Description:
Use PHPmail() function to send emails
Return:
Boolean
Usage:
$IsSend=Fun::SendMail($email,$tit,$msg);
*/
function SendMail($to,$tit,$msg) {
if(Filter_var($to,FILTER_VALIDATE_EMAIL)==''){
throw new Exception('Invalid Email address!');
}
$tit='=?UTF-8?B?'.Base64_Encode($tit).'?=';
$msg = str_replace("\n.","\n..",$msg); //in Windows, lines begin with a single dot will be deleted, so we need to replace single dot to double.
return mail($to,$tit,$msg,'From:[email protected]'."\n".'Content-Type:text/html;charset=utf-8');
}
$msg.=(isset($_SERVER['REQUEST_URI'])) ? "Request Page: {$MyDomain}{$_SERVER['REQUEST_URI']}". "\n" : '';
$msg.=(isset($_SERVER['REMOTE_ADDR'])) ? "IP: {$_SERVER['REMOTE_ADDR']}". "\n" : '';
$msg.="Time:".date('Y-m-d H:i:s',time()). "\n";
$msg.=(isset($_SERVER['REMOTE_HOST'])) ? "Host: {$_SERVER['REMOTE_HOST']}". "\n" : '';
$msg.=(isset($_SERVER['HTTP_USER_AGENT'])) ? "User Agent: {$_SERVER['HTTP_USER_AGENT']}". "\n" : '';
$msg.=(isset($_SERVER['HTTP_REFERER']) And Trim($_SERVER['HTTP_REFERER'])!='') ? "Referer: {$_SERVER['HTTP_REFERER']}". "\n" : '';
//File extension filter, do filter these media files or they will drive you crazy
$arr=array('mp3','rm','swf','jpg','gif');
//IP filter, you should filter out search engine spiders
$arrIP=array('66.249.77.217','66.249.74.67');
//File extension needed
$needEx=Explode('.',strtolower(Trim($_SERVER['REQUEST_URI'])));
$needEx=end($needEx);
if(!In_array($needEx,$arr) And !In_array(Trim($_SERVER['REMOTE_ADDR']),$arrIP)) {
mail('[email protected]','404 error from ['.$MyURL.']!',$msg);
}
// unSet($MyURL,$msg,$needEx,$arr);
// Header('HTTP/1.1 301 Moved Permanently');
// Header ("Location:{$MyDomain}");
// Die();
?>
</div>
<!--End if no results are found-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment