Home  >  Article  >  Backend Development  >  How to rewrite php 404 error

How to rewrite php 404 error

藏色散人
藏色散人Original
2020-07-13 09:11:232321browse

php 404 error rewriting method: first set the URL and set the time zone; then call the PHP built-in function Mail to send the email; then set the extension to be accessed; finally set the relevant code file to "404.php "That's it.

How to rewrite php 404 error

Use PHP to rewrite the 404 error page

404 error, many people know that if you want to access the url This page is read and displayed when it does not exist. In the past, our usual approach to dealing with 404 was to simply write a few lines of text, and some interested people might beautify it a little, and a small number of people who want to take advantage of it may even Use meta tags to do delayed redirection, that's all. In fact, 404 can also help us do a lot of things. This is the focus of what we are going to discuss today.

First of all, let me explain what I mean. What exactly can 404 do for me:

1. He can tell me when and where users want to access my URL and the result fails.

2. He can tell me why the other party wants to visit this URL and help me optimize the website

3. He can tell me the other party’s IP address

4. He can tell me what this person’s intentions are

5. I can ask him to shut up what he doesn’t want to tell me

6. He can send me what he wants to tell me via email

7. He can help me make a 301 permanent redirect.

8. He can tell me that someone is trying to invade my website or host

It sounds very mysterious and cool, okay, let’s continue Next, let’s discuss how he did it. Analyze one by one:

1. We can use PHP to capture the user’s source URL and current URL, so the first point we It can be done very easily.

2. Because of the source URL, you can intuitively guess the user's intention. Let me give you an example, just like mine Website http://lailinlin.com, if the source URL of a user is http://lailinlin.com, and the current URL captured is http://lailinlin.com/user.php?id=1, then It means that when this person browsed my website and clicked on a person's personal information page, he could not find the person's information and made an error. It means: the product with ID=1 may not exist and has been deleted, then you Just check to see if there is a hyperlink to this URL on the website. If so, find a way to delete it!

3. This function is relatively simple. , you only need to use PHP to capture the other party's IP. Then you can know where the person comes from by using the IP to physical address function of other websites.

4. This function is very useful Okay, very powerful. Because my website http://lailinlin.com is a self-configured server, I have to handle the security aspects by myself, and there are often some unscrupulous guys who are interested in other people's servers and they will use programs. Try various methods to enter the server, and at this time you will find that you receive a large number of 404 errors in a short period of time, and the source URL is empty. The URLs they want to access are all very sensitive URLs. At this time, you need to All you need to do is open your firewall settings, and then block this IP cleanly. Recently, I have used this method to block dozens of IPs. It’s really a small effort that makes a big difference! I forgot to mention that the IP of the Ministry of Information Industry is also I blocked it. My server is in Taiwan. Why are you trying to verify it? Really! Thanks to him, I receive hundreds of emails every day, some of which are caused by users browsing normally, some of which are caused by various search engines collecting content, and some of which are caused by bad guys who want to invade. In short, I don’t tire of it, and later I made a slight change and added all the extensions that meet the conditions or IPs that meet the conditions to the verification. As long as these meet the conditions, there is no need to send emails, which makes it much more refreshing.

6 . All of this is inseparable from PHP's ability to send emails instantly. My approach is to modify PHP.INI to implement PHP's internal mail function for sending. Of course, you can also use other methods, such as installing sendmail or developing a mail class. You can send it with the help of a third-party SMTP. As long as you feel comfortable (the specific details are not the scope of today’s discussion).

7. Since this page can be accessed, it means that the URL visited by the user is Doesn't exist, so we have to guide users to access the correct URL. So since we are guiding users to access the correct URL, you might say that just using a meta tag or a JS delay is enough. Why bother with a 301 redirect? ?Because I don’t want search engines to think I’m cheating! Answer completed.

8. I have already explained this point above. Basically, the judgment of the little bastard should be based on It depends on your experience and familiarity with your own website. What's more, you need to have a general understanding of which URLs on your website are backlinked by other websites (experience, it cannot be achieved overnight).

What should be said and what should be said is over. The show is about to be staged next. Save the code below as 404.php, and then use apache or nginx to save your 404 error. Just point to the page.

<?PHP
  
  
#设置URL,注意没有后划线/
$MyURL = (isSet($_SERVER[&#39;HTTP_HOST&#39;])) ? Str_iReplace(&#39;http://&#39;,&#39;&#39;,StrToLower(rTrim($_SERVER[&#39;HTTP_HOST&#39;],&#39;/&#39;))) : &#39;www.7di.net&#39;;
  
#设置URL,注意没有后划线/
$MyDomain = (isSet($_SERVER[&#39;HTTP_HOST&#39;])) ? &#39;http://&#39;.Str_iReplace(&#39;http://&#39;,&#39;&#39;,StrToLower(rTrim($_SERVER[&#39;HTTP_HOST&#39;],&#39;/&#39;))) : &#39;http://www.7di.net&#39;;
  
//設定時區.主要用來修正8小時時差
Date_Default_Timezone_Set(&#39;Etc/GMT-8&#39;);
  
//输出头部
Header(&#39;Content-type:text/html; charset=utf-8&#39;);
  
/**
发送電郵
参数:
收件人,郵件標題(不可有換行符),郵件內容(行與行之間必須用\n分隔,每行不可超過70個字符)
說明:
調用PHP內置函式Mail發送電郵
返回:
返回布爾值
用法:
$IsSend=Fun::Mail2($email,$tit,$msg);
/**/
Function Mail2($to,$tit,$msg) {
IF(Filter_var($to,FILTER_VALIDATE_EMAIL)==&#39;&#39;){
throw new Exception(&#39;電郵地址錯誤!&#39;);
}
  
$tit=&#39;=?UTF-8?B?&#39;.Base64_Encode($tit).&#39;?=&#39;;
$msg = str_replace("\n.","\n..",$msg); //Windows如果在一行开头发现一个句号则会被删掉,要避免此问题将单个句号替换成两个句号
  
Return Mail($to,$tit,$msg,&#39;From:see7di@gmail.com&#39;."\n".&#39;Content-Type:text/html;charset=utf-8&#39;);
}
  
$msg=&#39;<table cellspacing="0" cellpadding="0" border="0"><tr><td style="padding:5px;background-color:#F57900;font-size:13px;border:2px solid #222;color:#222;">&#39;;
$msg.=(isSet($_SERVER[&#39;REMOTE_ADDR&#39;])) ? "<b> 來訪者IP是:</b><br><a href=&#39;http://www.ip138.com/ips138.asp?ip={$_SERVER[&#39;REMOTE_ADDR&#39;]}&action=2&#39; target=&#39;_blank&#39;>{$_SERVER[&#39;REMOTE_ADDR&#39;]}</a><br>" : &#39;&#39;;
$msg.="<b>來訪時間:</b><br>".date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br><br>&#39;;
$msg.=(isSet($_SERVER[&#39;REMOTE_HOST&#39;])) ? "<a href=&#39;http://www.ip138.com/ips138.asp?ip={$_SERVER[&#39;REMOTE_HOST&#39;]}&action=2&#39; target=&#39;_blank&#39;>{$_SERVER[&#39;REMOTE_HOST&#39;]}</a><br><br>" : &#39;<br>&#39;;
$msg.=(isSet($_SERVER[&#39;HTTP_USER_AGENT&#39;])) ? "<b>來訪者詳細資料:</b><br>{$_SERVER[&#39;HTTP_USER_AGENT&#39;]}<br><br>" : &#39;&#39;;
$msg.=(isSet($_SERVER[&#39;REQUEST_URI&#39;])) ? "<b>要訪問的頁面是:</b><br>{$MyDomain}{$_SERVER[&#39;REQUEST_URI&#39;]}<br><br>" : &#39;&#39;;
$msg.=(isSet($_SERVER[&#39;HTTP_REFERER&#39;]) And Trim($_SERVER[&#39;HTTP_REFERER&#39;])!=&#39;&#39;) ? "<b>來源地址是:</b><br>{$_SERVER[&#39;HTTP_REFERER&#39;]}<br><br>" : &#39;&#39;;
$msg.=&#39;</td></tr></table>&#39;;
  
//哪些類型的URL不發Email,如果不屏蔽掉這些的話能煩死我
$arr=Array(&#39;mp3&#39;,&#39;rm&#39;,&#39;swf&#39;,&#39;jpg&#39;,&#39;gif&#39;);
  
//哪些IP不發Email,這些基本都是搜索引擎的蜘蛛
$arrIP=Array(&#39;66.249.77.217&#39;,&#39;66.249.74.67&#39;);
  
//要訪問的擴展名
$needEx=Explode(&#39;.&#39;,StrToLower(Trim($_SERVER[&#39;REQUEST_URI&#39;])));
$needEx=end($needEx);
  
IF(!In_Array($needEx,$arr) And !In_Array(Trim($_SERVER[&#39;REMOTE_ADDR&#39;]),$arrIP)) {
Mail2(&#39;see7di@gmail.com&#39;,&#39;來自【&#39;.$MyURL.&#39;】的404錯誤!&#39;,$msg);
}
  
unSet($MyURL,$msg,$needEx,$arr);
Header(&#39;HTTP/1.1 301 Moved Permanently&#39;);
Header ("Location:{$MyDomain}");
Die();

For more related knowledge, please visit

PHP Chinese website

!

The above is the detailed content of How to rewrite php 404 error. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Why is php cold?Next article:Why is php cold?