Home Backend Development PHP Tutorial Function introduction of PHP function crypt()_PHP tutorial

Function introduction of PHP function crypt()_PHP tutorial

Jul 15, 2016 pm 01:30 PM
crypt php introduce function Function encryption accomplish us data yes of

We know that there is a function to implement data encryption in PHP. What we will introduce to you today is one of the functions that can implement data encryption function - PHP function crypt(). As an example of the PHP function crypt(), consider a situation where you wish to create a PHP script that restricts access to a directory to only users who can provide the correct username and password.

I will store the data in a table in MySQL, my favorite database. Let's start our example by creating the table called members:

<ol class="dp-xml">
<li class="alt"><span><span>mysql</span><span class="tag">></span><span>CREATE TABLE members (   </span></span></li>
<li>
<span>-</span><span class="tag">></span><span>username CHAR(14) NOT NULL,   </span>
</li>
<li class="alt">
<span>-</span><span class="tag">></span><span>password CHAR(32) NOT NULL,   </span>
</li>
<li>
<span>-</span><span class="tag">></span><span>PRIMARY KEY(username)   </span>
</li>
<li class="alt">
<span>-</span><span class="tag">></span><span>);  </span>
</li>
</ol>
Copy after login

Then, we assume that the following data is already stored in the table:

Username Password
clark keloD1C377lKE
bruce ba1T7vnz9AWgk
peter paLUvRWsRLZ4U

The plain codes corresponding to these encrypted passwords in the PHP function crypt() are kent, banner and parker respectively. Pay attention to the first two letters of each password. This is because I used the following code to create a interference string based on the first two letters of the password:

<ol class="dp-xml">
<li class="alt"><span><span>$enteredPassword.   </span></span></li>
<li>
<span>$</span><span class="attribute">salt</span><span> = </span><span class="attribute-value">substr</span><span>($enteredPassword, 0, 2);   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">userPswd</span><span> = </span><span class="attribute-value">crypt</span><span>($enteredPassword, $salt);   </span>
</li>
<li><span>// $userPswd然后就和用户名一起存储在MySQL 中  </span></li>
</ol>
Copy after login

I will be using Apache's password-response authentication configuration to prompt the user for a username and password. A little known fact about PHP is that it can recognize the username and password entered by the Apache password-response system as $PHP_AUTH_USER and $PHP_AUTH_PW, I will use these two variables in the authentication script. Take some time to read the following script carefully and pay more attention to the explanations to better understand the following code:

Application of PHP function crypt() and Apache's password-response verification system

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php   </span></span></li><li><span>$</span><span class="attribute">host</span><span> = </span><span class="attribute-value">"localhost"</span><span>;   </span></li><li class="alt"><span>$</span><span class="attribute">user</span><span> = </span><span class="attribute-value">"zorro"</span><span>;   </span></li><li><span>$</span><span class="attribute">pswd</span><span> = </span><span class="attribute-value">"hell odolly"</span><span>;   </span></li><li class="alt"><span>$</span><span class="attribute">db</span><span> = </span><span class="attribute-value">"users"</span><span>;   </span></li><li><span>// Set authorization to False   </span></li><li class="alt"><span>$</span><span class="attribute">authorization</span><span> = </span><span class="attribute-value">0</span><span>;   </span></li><li><span>// Verify that user has entered<br /> username and password   </span></li><li class="alt"><span>if (isset($PHP_AUTH_USER) && <br />isset($PHP_AUTH_PW)) :   </span></li><li><span>mysql_pconnect($host, $user, <br />$pswd) or die("Can't connect to MySQL   </span></li><li class="alt"><span>server!");   </span></li><li><span>mysql_select_db($db) or die<br />("Can't select database!");   </span></li><li class="alt"><span>// Perform the encryption   </span></li><li><span>$</span><span class="attribute">salt</span><span> = </span><span class="attribute-value">substr</span><span>($PHP_AUTH_PW, 0, 2);   </span></li><li class="alt"><span>$</span><span class="attribute">encrypted_pswd</span><span> = crypt($PHP_AUTH_PW, $salt);   </span></li><li><span>// Build the query   </span></li><li class="alt"><span>$</span><span class="attribute">query</span><span> = "SELECT username FROM members WHERE   </span></li><li><span class="attribute">username</span><span> = '$PHP_AUTH_USER' AND   </span></li><li class="alt"><span class="attribute">password</span><span> = '$encrypted_pswd'";   </span></li><li><span>// Execute the query   </span></li><li class="alt"><span>if (mysql_numrows(mysql_query($query)) == 1) :   </span></li><li><span>$</span><span class="attribute">authorization</span><span> = </span><span class="attribute-value">1</span><span>;   </span></li><li class="alt"><span>endif;   </span></li><li><span>endif;   </span></li><li class="alt"><span>// confirm authorization   </span></li><li><span>if (! $authorization) :   </span></li><li class="alt"><span>header('WWW-Authenticate: <br />Basic </span><span class="attribute">realm</span><span>=</span><span class="attribute-value">"Private"</span><span>');   </span></li><li><span>header('HTTP/1.0 401 Unauthorized');   </span></li><li class="alt"><span>print "You are unauthorized <br />to enter this area.";   </span></li><li><span>exit;   </span></li><li class="alt"><span>else :   </span></li><li><span>print "This is the secret data!";   </span></li><li class="alt"><span>endif;   </span></li><li><span class="tag">?></span><span>  </span></span></li></ol>
Copy after login

The above is a simple authentication system to verify user access rights. When using the PHP function crypt() to protect important confidential information, remember that the PHP function crypt() used by default is not the most secure and can only be used in systems with lower security requirements. If necessary Higher security performance requires the algorithm I introduce later in this article.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446265.htmlTechArticleWe know that there is a function to implement data encryption in. What we will introduce to you today is one of them that can implement data encryption. The function of the encryption function is the PHP function crypt(). As PHP function crypt()...
Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Top 10 cryptocurrency trading platforms, top ten recommended currency trading platform apps Top 10 cryptocurrency trading platforms, top ten recommended currency trading platform apps Mar 17, 2025 pm 06:03 PM

The top ten cryptocurrency trading platforms include: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Get the gate.io installation package for free Get the gate.io installation package for free Feb 21, 2025 pm 08:21 PM

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles