2009-07-05 178 views
4

我正在研究我的第一个基于PHP的网站,我想知道用户名/密码系统有哪些解决方案?我已经尝试过使用.htaccess文件进行基本安全性保护,虽然它可以工作,但我希望一些外行人能够更轻松地进行管理。有没有其他解决方案可以尝试?我没有可用的数据库服务器,因此它必须支持平面文件数据库......谢谢!PHP用户名密码解决方案

编辑我已经确定我有SQLite支持,所以我确实有一个数据库选项可用。另外,我觉得我应该提一点我需要的一些要求。我最初希望使用.htaccess来保护我的网站,因为我需要整个目录的安全性。我试图保护的大多数文件都是.pdf和.doc ...任何解决方案都必须允许我保护这些文件以及目录中的任何网页。

如果我能找到一个或多或少的“皮肤”的锁定目录的.htaccess方法,以便我可以做一些事情,如有一个实际的登录/注册页面等,那么我只会坚持.htaccess方法。然而,我想要一些更易于管理的东西,我只需要目录安全。

回答

2

我写了这个代码很快,它是句法正确的,但我没有测试过它。
有两件事我没有在这里做,首先,我没有提供删除用户的功能,其次我没有提供更改用户密码的功能,您必须自己写。
但是,这应该提供一个很好的起点。

这些功能将存储您的用户名/文件中的密码称为以下格式

username0:password0 
username1:password1 
username2:password2 
... 

密码。

function authenticate($username, $password) 
{ 

    //ALWAYS use a salt to secure the encryption of your passwords, this can be any value of any 
    //length, the longer and the more characters the better 
    //I like to use a "perfect password" from Steve Gibbson's https://www.grc.com/passwords.htm 
    //This must the exactly the same as the salt in theaddUser() function 
    $salt = 'voDeaFWckErOPPGwiapYBwEoc4O2d1M60m2QsYc7A15PUshrLamoVioG1wUmEgF'; 

    //First we need to get the contents of the file that has the usernames/passwords in it. 
    //we don't want to use fopen() or we may end up with a locked file error if another access is 
    //attempted before we've closed it. 

    //this line will get the contents of the file named passwords and store it in the $fh variable 
    $fh = file_get_contents('passwords'); 

    //Now lets take the file and split it into an array where each line is a new element in the array. 
    $fh = split("\n", $fh); 

    //Now lets loop over the entire array spliting each row into it's username/password pair 
    foreach($fh as $r) 
    { 
     //Every time this loop runs $r will be populated with a new row 

     //Lets split the line into it's username/password pairs. 
     $p = split(':', $p); 

     //Since we don't need all the usernames/password to be in memory lets stop when we find the one we need 
     if($p[0] == $username && $p[1] == sha1($salt . $password)) 
     { 
      //We've found the correct use so lets stop looping and return true 
      return true; 
     } 
    } 
    //If we've reached this point in the code then we did not find the user with the correct password in the 'database' 
    //so we'll just return false 
    return false; 
} 
function addUser($username, $password) 
{ 
    //ALWAYS use a salt to secure the encryption of your passwords, this can be any value of any 
    //length, the longer and the more characters the better 
    //I like to use a "perfect password" from Steve Gibbson's https://www.grc.com/passwords.htm 
    //This must the exactly the same as the salt in the authenticate() function 
    $salt = 'voDeaFWckErOPPGwiapYBwEoc4O2d1M60m2QsYc7A15PUshrLamoVioG1wUmEgF'; 

    //We need to parse out some preticularly bad characters from the user name such as : which is used to seperate the username and password 
    //and \r and \n which is the new line character which seperates our lines 
    $username = preg_replace('/\r|\n|\:/', '', $username); 

    //Now lets encrypt our password with the salt added 
    $password = sha1($salt . $password); 

    //Lets build the new line that is going to be added 
    $line = $username . ':' . $password . "\n"; 

    //Lets open the file in append mode so that the pointer will be placed at the end of the file 
    $fh = fopen('passwords', 'a'); 

    //Write the new entry to the file 
    fwrite($fh, $line); 

    //Close the file 
    fclose($fh); 

    //Typicaly one would write a bunch of error handling code on the above statments and if something 
    //goes wrong then return false but if you make it this far in the code then return true 
    return true; 
} 
1

当然,有很多平面文件数据库PHP安全系统可用。做一个快速的谷歌搜索将拉动许多结果。这里是一个教程:

http://www.devshed.com/c/a/PHP/Private-Pages-with-PHP-and-Text-Files/

+0

这看起来不错,但我希望目录级别的安全性,我也希望注册表单类型的交易。我想这些东西不会很难编码,我只是很新的PHP,并且不希望我的客户(可以这么说)必须等很长时间才能学习如何编写代码PHP! – 2009-07-05 21:16:20

1

检查,如果你有sqlite的支持,它不需要服务器,因此它可能为你工作。

不要忘记哈希你的密码。 ;)

要检查创建一个文件(例如php_info.php)地址:

<?php 
    phpinfo(); 

然后将文件上传到你的主机,在您的浏览器(example.com/php_info.php加载)和。搜索sqlite。

您应该在页面中看到几个对sqlite的引用,显示您是否有支持。带有“SQLite Library”的行会告诉你你拥有的sqlite版本(如果你有的话)。

此外,一旦你完成你应该从您的网站删除php_info.php文件,因为它确实给你的设置一些信息,这可能有助于饼干。

+0

还没有想过sqlite ......我会研究一下,谢谢! – 2009-07-05 21:17:11

+0

我将如何从我的主机发现是否支持sqlite? – 2009-07-05 21:23:39

+0

更新了我的文章,提供了关于如何检查支持的信息。 – MitMaro 2009-07-05 21:35:40

0

你见过吗?如果你有SQLite可用?这是PHP内置的数据库。如果不是,你可以使用读/写到一个file希望这有助于一点

-3

this page从Apache网站:

一般情况下,你不应该使用.htaccess文件,除非你没有访问主服务器配置文件。例如,有一种普遍的误解,认为用户认证应始终在.htaccess文件中完成。这根本不是那么回事。您可以将用户身份验证配置置于主服务器配置中,事实上,这是做事的首选方式。

它很容易看出为什么这也是如此。在调试错误的配置时,最好采用集中控制,而不是通过每个单目录进行挖掘。

我强烈建议您将您的.htaccess文件配置尽快转移到您的主配置文件中,为您自己做好!

2

看看Zend_Auth。它是开源的,所以你可以嗅探一下认识模块应该(或可能)如何实现的感觉。从DOC:

Zend_Auth只 认证,而不是与 授权有关。认证是松散地定义为基于 确定实体是否实际上是它声称的 是(即,识别)的,基于 某一组凭证。