您好,欢迎访问一九零五行业门户网

PHP函数crypt()的功能介绍_PHP教程

我们知道在中有实现数据加密的功能,我们今天将为大家介绍的是其中一个可以实现数据加密功能的函数——php函数crypt()。 作为php函数crypt()的一个例子,考虑这样一种情况,你希望创建一段php脚本程序限 制对一个目录的访问,只允许能够提供正确的用户名和口令的用户访问这一目录。
我将把资料存储在我喜欢的数据库mysql的一个表中。下面我 们以创建这个被称作members的表开始我们的例子:
mysql>create table members (   ->username char(14) not null,   ->password char(32) not null,   ->primary key(username)   ->);  
然后,我们假定下面的数据已经存储在该表中:
用户名 密码
clark kelod1c377lke
bruce ba1t7vnz9awgk
peter paluvrwsrlz4u
php函数crypt()中的这些加密的口令对应的明码分别是kent、banner和parker。注意一下每个口令的前二个字母, 这是因为我使用了下面的代码,根据口令的前二个字母创建干扰串的:
$enteredpassword.   $salt = substr($enteredpassword, 0, 2);   $userpswd = crypt($enteredpassword, $salt);   // $userpswd然后就和用户名一起存储在mysql 中  
我将使用apache的口令-应答认证配置提示用户输入用户名和口令,一个鲜为人知的有关php的信息是,它可以把apache 的口令-应答系统输入的用户名和口令识别为$php_auth_user和$php_auth_pw,我将在身份验证脚本中用到这二个变量。花一些时间仔细阅读下 面的脚本,多注意一下其中的解释,以便更好地理解下面的代码:
php函数crypt()和apache的口令-应答验证系统的应用
 ?php   $host = localhost;   $user = zorro;   $pswd = hell odolly;   $db = users;   // set authorization to false   $authorization = 0;   // verify that user has entered
 username and password   if (isset($php_auth_user) && 
isset($php_auth_pw)) :   mysql_pconnect($host, $user, 
$pswd) or die(can't connect to mysql   server!);   mysql_select_db($db) or die
(can't select database!);   // perform the encryption   $salt = substr($php_auth_pw, 0, 2);   $encrypted_pswd = crypt($php_auth_pw, $salt);   // build the query   $query = select username from members where   username = '$php_auth_user' and   password = '$encrypted_pswd';   // execute the query   if (mysql_numrows(mysql_query($query)) == 1) :   $authorization = 1;   endif;   endif;   // confirm authorization   if (! $authorization) :   header('www-authenticate: 
basic realm=private');   header('http/1.0 401 unauthorized');   print you are unauthorized 
to enter this area.;   exit;   else :   print this is the secret data!;   endif;   ?>  
上面就是一个核实用户访问权限的简单身份验证系统。在使用php函数crypt()保护重要的机密资料时,记住在缺省状态下使用的php函数crypt()并不是最安全的,只能用在对安全性要求较低的系统中,如果需要较高的安全性能,就需要我在本篇文章的后面介绍的算法。
http://www.bkjia.com/phpjc/446265.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/446265.htmltecharticle我们知道在中有实现数据加密的功能,我们今天将为大家介绍的是其中一个可以实现数据加密功能的函数php函数crypt()。 作为php函数crypt()的...
其它类似信息

推荐信息