search
HomeBackend DevelopmentPHP TutorialSecurity Summary of PHP Project Code_PHP Tutorial
Security Summary of PHP Project Code_PHP TutorialJul 13, 2016 pm 05:10 PM
phpcodeexistSafetydevelopSummarizeushourtimeModularofproject

When developing projects with PHP, we spend a lot of time in modular development. At this time, there may be a lot of security hidden. The following is a security summary of some PHP project codes that I have summarized. Students who need to know more can refer to it.

1: Basic type,
include $module.'.php'; If $module is obtained directly using GET, then this is a very devastating bug. It will make you miserable under Linux and make you bankrupt under Windows. This kind of security is usually directly ignored. Discover and effectively prevent. If you haven’t even done this, can you be called a qualified programmer?

2: Quality safety.
Many people will say that external variables should be addedlashes once. We don’t care whether the addslashes function is efficient and safe. Many people actually forget that before using addslashes, you must also pay attention to safety. index.php?aa[]=222&, this is the url, addslashes ($aa); The system will report an error. It can be seen that when we use the function, we should know what will happen if the data type is different. An article has said before that the function is registered to pass parameters >0 numeric type, What should I do if the user passes in letters? Many people think it’s a function error. In my opinion, it’s a user error. Although it’s a custom function, you passed the parameters without knowing the reason. This attitude has changed. Very unsafe.

3: Pressure safe.
Nowadays, if you occasionally watch the news on 163, you will find that the browser will suddenly crash, freeze, or even be "closed". This reason may be a problem with the web page program code, or it may be a problem with the front-end js, especially js, because The characteristics of js are relatively flexible. In terms of using loops and assignments, it is not easy to check for errors. The most prominent point is the value acquisition and error handling. For example, getelementbyid('mydiv'); At this time, have you ever thought about whether mydiv exists? Has it been modified twice? Another point is the picture. Many people use onerror to display the default picture again when the picture Security Summary of PHP Project Code_PHP Tutorial cannot be loaded. Have you ever thought about what if the default picture cannot be displayed? Enter Infinite loop?

4: PHP performance safety.
No one can say that they have never encountered an infinite loop that caused Apache to crash. No matter how good people are, they all have the same experience. What kind of pressure does the loop create? What performance is affected? See a piece of code:
foreach($array AS $val){
$payarr = include('pay.inc.php');
If($payarr[0] >= 360 * 1.25 + 568){
$err[] = $payarr[0];
}
}
This code can run normally. Regardless of whether your include is successful or not, there is no need to judge whether it is meaningful. Anyway, it can run successfully. Of course, it is meaningless. To truly realize the functional requirements, this code needs to add several lines. , it’s all about judgment. Before introducing a file, should we first use is_file to confirm whether the file exists? Since we are in a loop, should we use include_once? The condition value is a numerical operation, is it better to write a direct value? Before judging, should we use include_once? First determine whether the $payarr array value exists?

5: When writing a program or writing a judgment.
The performance safety mentioned above lacks many judgments. Does PHP have documentation to show whether unnecessary judgments affect performance? See the code.
If(is_resource($a) === false || is_array($a) === false || is_bool($a) === false)
echo $a;
What if we judge like this every time we use a variable? Is it safer? The code is correct, echo only prints string types, and the previous judgment is also in line with logical thinking. This may be more like strict security, but if PHP can omit it With this little performance pressure, writing like this is not necessarily a bad thing. At least it meets the requirements and increases security. Of course, we will also wonder, if I write like this, am I writing a program or writing a judgment?

6: Blocking errors.
When many people were learning PHP, the teacher said something about security that deeply stuck in his mind, which is to block error messages. Personally, I think there should be prerequisites for blocking errors. That is, the display can be blocked, but the records cannot be blocked. . One day, a web page was blank, and I asked the programmer to check for errors. He told me that the errors were blocked. Do you think this is reasonable? I asked you to block the errors, but I didn’t ask you to block yourself, even yourself. I don’t even know where the error is, but ordinary users can know it? So, when you use php.ini shielding or the @ symbol, remember that you have to know where the error occurs.

7: Interface security.
I believe everyone has interface security, whether you have it or not, I have it anyway. The usual method is to request the URL, and then PHP prints the value to the client, so that the client can do judgment processing or display processing. Fact In this way, ordinary PHP staff can easily get the URL through firebug, and then know what parameters you get and what value is returned. This is a potential danger, such as product reviews and product prices. For this reason , I suggest that the interface be built with a layer of token, first get the token and then pass the parameters. The work done by the token is very meaningful. It can calculate whether the user is normal, IP, and request time. In the next step, when requesting the interface, it can be judged. The token value is encrypted , users cannot use or retain it. You may ask, token is just a string of characters, and it can always retain the request interface. Oh my God. Please use the request time.

8: Show security.
In fact, I don’t fully understand the issue of display security. For example, I need to record the user’s signature and support html code. After submission, before entering mysql, is the data addslashes or htmlspecialchars? If it is addslashes, then the front-end display When it is used, it may be injected by XSS. If htmlspecialchars is used, how to display the HTML code when it is displayed in the foreground? At the same time, can XSS be prevented? There are so many variables in a website, do you think about this problem one by one? How to prevent attacks?

9: Damn safe.
Regardless of whether you believe it or not, I believe sohpex is a pain in the ass. The code encrypted by zend seems to be unusable after version php5.3. Your security is too unreliable. It turns out that it is public relations security, but it is handed over to programmers. Realization. The business model does not work like this

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629660.htmlTechArticleWhen developing projects with PHP, we spend a lot of time in modular development. At this time, there may be many security hidden things. , the following is a safety summary of some PHP project codes that I summarized, if necessary...
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools