Use php to rewrite 404 error pages to make your pages more user friendly

  • 2020-05-27 04:38:08
  • OfStack

404 error, a lot of people know, if you want to visit url does not exist when reading display this page. In the past we usually practice is in dealing with a 404 or simply write a few lines, but a civic-minded individuals may also take on the beautification, the other part 1 less to opportunistic meta standard � � * even made delay, that's all, actually 404 can also help us to do many things, that is the focus of today we are going to discuss.

First of all, let me explain what my 404 can do for me:
1. He can tell me when and where the user from which URL tried to access me failed.
2. Can he tell me why he wants to visit URL and help me optimize the site
He can tell me the IP address of the other party
He can tell me what the man is up to
5. I can tell him to shut up what he doesn't want to tell me
6. He can send me what he wants to tell me by email
7. He can help me do a 301 permanent redirect.
8. He can tell me that someone is trying to hack into my website or host

Well, let's talk about how he did it.
1. We can use PHP to capture the user's source URL and the current URL, so we can easily do the first point.
URL 2. Because of the source, so you can very intuitive guess to the user's intention, for example, I like my website https: / / www ofstack. com, if is the source of a particular user URL https: / / www ofstack. com, and capture his present URL is https: / / www ofstack. com/user php? id = 1 which means the person when browse my website when click on someone's personal information page results can't find this person's information so as to make a mistake, note: this may not exist ID = 1 goods have already been deleted, then you have to see if 1 net website and this URL hyperlinks, if any will be to find a way to have to delete it!
3. This function is relatively simple, you just need to use PHP to capture the other person's ip, and then cooperate with other websites' IP to transfer the physical address to know where the person is from.
4. This function is very good, very powerful. Because my website www. 7 di. net is his own configuration of the server, so the safety should be on their own to deal with, but often have some dirty � groups interested in someone else's server, they would use the program to try various ways to enter the server, and you'll find you in a short time, received a lot of 404 errors, and source URL is empty, they will visit URL URL are more sensitive, you need to do is open your fire � Settings, and then the sealing off the dry fall � li IP. This is how I've been able to seal up 10 IP recently. Forget to say, the ministry of information industry IP has also been blocked by me, my server in Taiwan you to verify a what power, really ~ ~!
5. To be honest, this plug-in in the beginning let me very upset, because his blessing I will receive hundreds of letters every day Email, have a plenty of because users browse, partly because each search engine to � set content, because some small flat to invasion in 3, in a word repeatedly, then I make a little change, the extension accords with a condition or IP eligible to join the validation, as long as these need not send Email that accords with a condition, so that 1 to much more relaxed.
6. The 1 don't cut out PHP send instantly Email function, my way is to modify PHP. INI to implement internal mail PHP function to send, of course, if you can also use other methods such as: installation sendmail or develop a mail class to smtp sent by the third party. As long as you comfortable scope details (not today).
7. Since we are able to access this page, it means that the URL accessed by the user does not exist, so we need to guide the user to access the correct URL. If we are able to guide the user to access the correct URL, you might say, just use one meta logo or one JS sentence for a delay. Because I don't want the search engines to think I'm cheating! That's all.
8. As I explained above, the judgment of beavis 3 should be based on your experience and familiarity with your own website. What's more, you should generally know which URL of your website has been unlinked by other websites.

That's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it, that's it.
 
<?PHP 
# Set up the URL , notice there is no underline / 
$MyURL = (isSet($_SERVER['HTTP_HOST'])) ? Str_iReplace('http://','',StrToLower(rTrim($_SERVER['HTTP_HOST'],'/'))) : 'www.7di.net'; 
# Set up the URL , notice there is no underline / 
$MyDomain = (isSet($_SERVER['HTTP_HOST'])) ? 'http://'.Str_iReplace('http://','',StrToLower(rTrim($_SERVER['HTTP_HOST'],'/'))) : 'https://www.ofstack.com'; 
// Set the time zone . Mainly used to correct 8 Hours time difference  
Date_Default_Timezone_Set('Etc/GMT-8'); 
// Output the head  
Header('Content-type:text/html; charset=utf-8'); 
/** 
 Send an email  
 parameter : 
 The recipient , Email title ( Cannot have a newline character ), Email content ( Must be used between lines \n separated , Each line may not exceed 70 A character ) 
 instructions : 
 call PHP The built-in function Mail Send an email  
 return : 
 Returns a Boolean value  
 usage : 
$IsSend=Fun::Mail2($email,$tit,$msg); 
/**/ 
Function Mail2($to,$tit,$msg) { 
IF(Filter_var($to,FILTER_VALIDATE_EMAIL)==''){ 
throw new Exception(' Email address error !'); 
} 
$tit='=?UTF-8?B?'.Base64_Encode($tit).'?='; 
$msg = str_replace("\n.","\n..",$msg); //Windows If the 1 Line finding 1 A period will be deleted , Avoid this problem by replacing a single period with two periods  
Return Mail($to,$tit,$msg,'From:see7di@gmail.com'."\n".'Content-Type:text/html;charset=utf-8'); 
} 
$msg='<table cellspacing="0" cellpadding="0" border="0"><tr><td style="padding:5px;background-color:#F57900;font-size:13px;border:2px solid #222;color:#222;">'; 
$msg.=(isSet($_SERVER['REMOTE_ADDR'])) ? "<b> The visitors IP is :</b><br><a href='http://www.ip138.com/ips138.asp?ip={$_SERVER['REMOTE_ADDR']}&action=2' target='_blank'>{$_SERVER['REMOTE_ADDR']}</a><br>" : ''; 
$msg.="<b> Visiting time :</b><br>".date('Y-m-d H:i:s',time()).'<br><br>'; 
$msg.=(isSet($_SERVER['REMOTE_HOST'])) ? "<a href='http://www.ip138.com/ips138.asp?ip={$_SERVER['REMOTE_HOST']}&action=2' target='_blank'>{$_SERVER['REMOTE_HOST']}</a><br><br>" : '<br>'; 
$msg.=(isSet($_SERVER['HTTP_USER_AGENT'])) ? "<b> Visitor details :</b><br>{$_SERVER['HTTP_USER_AGENT']}<br><br>" : ''; 
$msg.=(isSet($_SERVER['REQUEST_URI'])) ? "<b> The page to visit is :</b><br>{$MyDomain}{$_SERVER['REQUEST_URI']}<br><br>" : ''; 
$msg.=(isSet($_SERVER['HTTP_REFERER']) And Trim($_SERVER['HTTP_REFERER'])!='') ? "<b> The source address is :</b><br>{$_SERVER['HTTP_REFERER']}<br><br>" : ''; 
$msg.='</td></tr></table>'; 
// What types of URL Don't send Email, It would bother me if I didn't block that out  
$arr=Array('mp3','rm','swf','jpg','gif'); 
// What are the IP Don't send Email, These are basically search engine spiders  
$arrIP=Array('66.249.77.217','66.249.74.67'); 
// The extension to access  
$needEx=Explode('.',StrToLower(Trim($_SERVER['REQUEST_URI']))); 
$needEx=end($needEx); 
IF(!In_Array($needEx,$arr) And !In_Array(Trim($_SERVER['REMOTE_ADDR']),$arrIP)) { 
Mail2('see7di@gmail.com',' From the '.$MyURL.' 】 the 404 error !',$msg); 
} 
unSet($MyURL,$msg,$needEx,$arr); 
Header('HTTP/1.1 301 Moved Permanently'); 
Header ("Location:{$MyDomain}"); 
Die(); 

Note: if you feel that the functionality implemented in the above code does not meet your expectations, then encourage you to rewrite it, improve it, and enhance it. I have no objection, but please share your work with me, thank you!

Related articles: