Php database security-design, connection, and encryption

Source: Internet
Author: User
Tags php database
PHP Security-database security-design, connection, and encryption

Design Database

The first step is generally to create a database, unless you use a third-party database service. When creating a database, an owner is specified to execute and create statements. Generally, only the owner (or super user) has the right to perform any operation on objects in the database. If you want other users to use it, you must grant them permissions.

Applications should never use the database owner or superuser account to connect to the database, because these accounts can perform any operations, such as modifying the database structure (such as deleting a table) or clear the content of the entire database.

You should create different database accounts for each aspect of the program and grant extremely limited permissions to database objects. Assign only the permissions required to complete the functions of a user, so that the same user can complete the tasks of another user. In this way, even if attackers exploit a program vulnerability to gain database access permissions, they can only achieve the same impact scope as the program.

Do not use web applications (user scripts) to implement all Transaction logic. It is best to use a view, trigger, or rule at the database level. When the system is upgraded, you need to open up new interfaces for the database, and then you must redo all the database clients. In addition, triggers can process fields transparently and automatically, and provide useful information in real time for debugging programs and tracking.

Connect to database

Setting up a connection to the SSL encryption technology can increase the communication security between the client and the server, or use SSH to encrypt connections between the client and the database. If these technologies are used, it is very difficult for attackers to monitor server communication or obtain database information.

Encrypted storage model

SSL/SSH can protect the data exchanged between the client and the server, but SSL/SSH cannot protect the existing data in the database. SSL is just a protocol used to encrypt network data streams.

If attackers obtain a permit to directly access the database (bypassing the web server), sensitive data may be exposed or abused unless the database itself protects the information. Data encryption in databases is an effective way to reduce such risks, but only a few databases provide these encryption functions.

A simple solution to this problem is to create your own encryption mechanism and use it in a PHP program. PHP has several extension libraries to do this, such as Mcrypt and Mhash. they contain multiple encryption algorithms. The script encrypts the data before it is inserted into the database, and then decrypts the data when it is extracted.

If some truly hidden data does not need to exist in plain text (that is, it does not need to be displayed), you can use the hash algorithm. The most common example of using the hash algorithm is to store the hash encrypted by MD5 in the database to replace the original plaintext password.

Example #1 Hash Encryption of password fields

 0) {echo 'Welcome, $ username! ';} Else {echo 'authentication failed for $ username.';}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.