Home Backend Development PHP Tutorial PHP中使用crypt()实现用户身份验证的代码_php技巧

PHP中使用crypt()实现用户身份验证的代码_php技巧

May 17, 2016 am 09:09 AM
crypt User authentication

了解crypt()

  只要有一点使用非Windows平台经验的读者都可能对crypt()相当熟悉,这一函数完成被称作单向加密的功能,它可以加密一些明码,但不能反过来将密码重新转换为原来的明码。crypt()函数定义如下。

  string crypt (string input_string [, string salt])

  其中,input_string参数是需要加密的明文字符串,第二个可选的salt是一个位字串,能够影响加密的暗码,进一步排除被破解的可能性。缺省情况下,PHP使用一个2个字符的DES干扰串,如果系统使用的是MD5(参考下一节内容),PHP则会使用一个12个字符的干扰串。可以通过执行下面的命令发现系统将要使用的干扰串的长度。

  print "My system salt size is: ". CRYPT_SALT_LENGTH;

  crypt()支持4种加密算法,表19.1显示了其支持的算法和相应的salt参数的长度。

  表crypt()支持四种加密算法

算法 Salt长度
CRYPT_STD_DES 2-character (Default)
CRYPT_EXT_DES 9-character
CRYPT_MD5 12-character beginning with $1$
CRYPT_BLOWFISH 16-character beginning with $2$

从表面上看,crypt()的函数似乎没有什么用处,但该函数的确被广泛用来保证系统密码的完整性。因为,单向加密的口令即使落入第三方的手里,由于不能被还原为明文,也没有什么大用处。
  用crypt()实现用户身份验证
  上一部分简单介绍了crypt()函数的功能,下面利用其来实现用户的身份验证,其所要实现的目标同19.2.3节所介绍的一致。
复制代码 代码如下:


$user_name=$_POST["user_name"];
require_once("sys_conf.inc"); //系统配置文件,包含数据库配置信息
//连接数据库
$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);
mysql_select_db($DBNAME); //选择数据库my_chat
//查询是否存在登录用户信息
$str="select name,password from user where name ='$user_name'";
$result=mysql_query($str,$link_id); //执行查询
@$rows=mysql_num_rows($result); //取得查询结果的记录笔数
$user_name=$_SESSION["user_name"];
$password=$_POST["password"];
$salt = substr($password, 0, 2);
$password_en=crypt($password,$salt); //使用crypt()对用户密码进行加密
//对于老用户
if($rows!=0)
{
list($name,$pwd)=mysql_fetch_row($result);
//如果密码输入正确
if($pwd==$password_en)
{
$str="update user set is_online =1 where name ='$user_name' and password='$password_en'";
$result=mysql_query($str, $link_id);//执行查询
require("main.php"); //转到聊天页面
}
//密码输入错误
else
{
require("relogin.php");
}
}
//对于新用户,将其信息写入数据库
else
{
$str="insert into user (name,password,is_online) values('$user_ name','$password_en',1)";
$result=mysql_query($str, $link_id); //执行查询
require("main.php"); //转到聊天页面
}
//关闭数据库
mysql_close($link_id);
?>


示例与上一节所介绍的使用XOR加密算法来保护用户信息非常类似,其核心部分在于第16、17行使用crypt()函数获取加密后的密码,而通过在第25行比较数据库中的密码和加密后的密码是否相等来检查用户是否合法。

  下面,通过一个实例来看一下加密后的密码会变成什么样子。

  例如,用户名为rock,密码为123456,则加密后的密码为:

  12tir.zIbWQ3c

  上面就实现了一个简单的用户身份验证系统。在使用crypt()保护重要的机密信息时,需要注意的是,在缺省状态下使用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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
Golang development: Implementing JWT-based user authentication Golang development: Implementing JWT-based user authentication Sep 20, 2023 am 08:31 AM

Golang development: Implementing JWT-based user authentication With the rapid development of the Internet, user authentication has become a crucial part of web applications. The traditional cookie-based authentication method has gradually been replaced by the JWT (JSONWebToken)-based authentication method. JWT is a lightweight authentication standard that allows the server to generate an encrypted token and send the token to the client. The client puts the token into Authori when sending a request.

How to configure Nginx proxy server to protect user authentication information for web services? How to configure Nginx proxy server to protect user authentication information for web services? Sep 05, 2023 pm 12:40 PM

How to configure Nginx proxy server to protect user authentication information for web services? Introduction: In today’s Internet world, protecting users’ authentication information is crucial. Nginx is a powerful proxy server that can help us protect authentication information. This article will describe how to configure an Nginx proxy server to protect user authentication information for web services and provide some code examples. 1. Install Nginx First, we need to install Nginx. On most Linux

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.

Cryptocurrency trading platform app Top ten recommendations for currency trading platform apps Cryptocurrency trading platform app Top ten recommendations for currency trading platform apps Mar 18, 2025 am 11:09 AM

Crypto Trading Platform App Ranking: 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.

Which of the top ten must-see websites in the currency circle? Which of the top ten must-see websites in the currency circle? Mar 04, 2025 pm 10:54 PM

In the ever-changing cryptocurrency space, a reliable source of information is crucial. To help investors and traders understand industry trends, this article lists the top ten must-see cryptocurrency websites. These sites provide in-depth market analysis, the latest industry news, technology updates and data-based insights to provide cryptocurrency enthusiasts with comprehensive resources to assist them in making informed investment decisions.

Top 10 easy-to-use and reliable digital currency trading software rankings recommended 2025 Top 10 easy-to-use and reliable digital currency trading software rankings recommended 2025 Feb 08, 2025 pm 08:15 PM

In an era of booming digital currencies, choosing a useful and reliable trading software is crucial. This article recommends the top 10 most trusted digital currency trading software in 2025, including Binance, Coinbase, FTX, Kraken, Huobi Global, KuCoin, Crypto.com, Phemex, Gemini and Gate.io. These platforms are evaluated based on factors such as user base, cryptocurrency selection, functionality, security, compliance and transaction fees, giving you the best choice to meet different needs.

Analysis of user authentication methods between DingTalk interface and PHP Analysis of user authentication methods between DingTalk interface and PHP Jul 05, 2023 am 10:57 AM

Analysis of User Authentication Methods of DingTalk Interface and PHP With the rapid development of the Internet, mobile office has become more and more common. DingTalk, as a mobile application that focuses on corporate office work, has been welcomed by a large number of enterprises. DingTalk provides a rich interface to facilitate developers to carry out secondary development. This article will introduce how to use the DingTalk interface for user authentication and provide corresponding PHP code examples. Before using the DingTalk interface for user authentication, we need to first understand DingTalk's open platform and application development. First, we need

How to implement user authentication using Go language and Redis How to implement user authentication using Go language and Redis Oct 26, 2023 am 08:27 AM

How to use Go language and Redis to implement user authentication 1. Introduction In web applications, user authentication is an essential function. Users need to provide valid credentials to access specific resources or perform certain actions. Go language is a powerful programming language, and Redis is a fast, highly available in-memory data storage system. Combining these two, we can implement an efficient user authentication system. 2. Preparation Before starting to write code, we need to install and configure the Go language and Re

See all articles