How to prevent sql injection in php website?
The operational security of the website is definitely an issue that every webmaster must consider. As we all know, most hackers attack websites by using SQL injection. This is what we often say why? The most original static website is the safest. Today we will talk about the security specifications of PHP injection to prevent your website from being injected by SQL. Nowadays, the mainstream website development language is still PHP, so let’s start with how to prevent SQL injection on PHP websites: Security Prevention of Php Injection Through the above process, we can understand the principles and techniques of php injection. Of course, we can also develop corresponding prevention methods: The first is the security settings of the server. Here are mainly the security settings of php+mysql and the security settings of the Linux host. To prevent php+mysql injection, first set magic_quotes_gpc to On and display_errs to Off. If it is an id type, we use intval() to convert it into an integer type, as shown in the code: $idintval($id); mysql_query”*fromexamplewherearticieid’$id’”; Or write like this: mysql_query(”SELECT*FROMarticleWHEREarticleid”.intval($id).””) If it is a character type, use addslashes() to filter it, and then filter "%" and "_", such as: $searchaddslashes($search); $searchstr_replace(“_”,”_”,$search); $searchstr_replace(“%”,”%”,$search); Of course, you can also add PHP universal anti-injection code: /***************************** PHP universal anti-injection security code Description: Determine whether the passed variable contains illegal characters Such as $_POST, $_GET Function: Anti-injection
Then add include("checkpostget.php"); before each php file ****************************/ In addition, the administrator username and password are md5 encrypted, which can effectively prevent PHP injection. There are also some security precautions that need to be strengthened on the server and mysql. For security settings of linux server: To encrypt the password, use the "/usr/sbin/authconfig" tool to turn on the password shadow function and encrypt passwd. To prohibit access to important files, enter the Linux command interface and enter at the prompt: #chmod600/etc/inetd.conf//Change the file attributes to 600 #chattr+I /etc/inetd.conf // Ensure that the file owner is root #chattr–I /etc/inetd.conf //Restrict changes to this file It is forbidden for any user to change to the root user through the su command Add the following two lines at the beginning of the su configuration file, that is, the /etc/pam.d/ directory: Auth sufficient /lib/security/pam_rootok.sodebug Auth required /lib/security/pam_whell.sogroupwheel Delete all special accounts #userdel lp etc. Delete user #groupdellpetc delete group Ban unused suid/sgid programs #find/-typef(-perm-04000 -o–perm-02000)-execls–lg{}; http://hi.baidu.com/bigideaer/bl... 7e76e11a4cffd0.html To determine whether the passed variables contain illegal characters, we put the following code into a public file, such as security.inc.php. Include this file in each file, then all the files submitted to any program can be After the variables are filtered, we achieve the effect once and for all. Brief description: /*************************** Description: Determine whether the passed variable contains illegal characters Such as $_POST, $_GET Function: Anti-injection **************************/ The code is as follows:
Method 2 The code is as follows:
The second method is written in a separate file and imported into every PHP file Then you can escape every data functionsaddslashes($string){ if(is_array($string)){ feach($stringas$key>$val){ $string[$key]saddslashes($val); } }else{ $stringaddslashes($string); } return$string; } ################################################ ############### $magic_quoteget_magic_quotes_gpc(); if(empty($magic_quote)){ $_GETsaddslashes($_GET); $_POSTsaddslashes($_POST); } |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
