PHP用户名密码解决方案

时间:2009-07-05 20:55:37

标签: php security

我正在开发我的第一个基于PHP的网站,我想知道用户名/密码系统有哪些解决方案?我已经尝试使用.htaccess文件来实现基本的安全性,虽然它有效但我希望外行人能够管理一些更容易的东西。我可以尝试其他解决方案吗?我没有可用的数据库服务器,所以它必须支持平面文件数据库...谢谢!

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

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

6 个答案:

答案 0 :(得分: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 :(得分:2)

看看Zend_Auth。它是开源的,因此您可以嗅一下,以了解应如何(或可能)实现身份验证模块。来自doc:

  

Zend_Auth只关注   身份验证而不是   授权。身份验证是   松散地定义为确定是否   一个实体实际上是它所声称的   基于的(即识别)   一些凭证。

答案 2 :(得分:1)

当然,有很多平面文件数据库PHP安全系统可用。快速进行Google搜索会获得很多结果。这是一个教程:

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

答案 3 :(得分:1)

检查您是否支持sqlite,它不需要服务器,因此它可能适合您。

不要忘记哈希密码。 ;)

要检查创建文件(例如php_info.php),请添加:

<?php
     phpinfo();

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

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

此外,一旦完成,您应该从您的网站删除php_info.php文件,因为它确实提供了有关您的设置的一些信息,这对于破解者很有帮助。

答案 4 :(得分:0)

你看过你有SQLite吗?它是PHP内置的数据库。如果没有,你可以使用读/写file希望这有点帮助

答案 5 :(得分:-3)

根据Apache网站的this page

通常,除非您无权访问主服务器配置文件,否则不应使用.htaccess个文件。例如,存在一种普遍的误解,即用户身份验证应始终在.htaccess个文件中完成。这根本不是那么回事。您可以将用户身份验证配置放在主服务器配置中,事实上,这是执行操作的首选方式。

很容易理解为什么会这样。它最好是集中控制,而不是在调试错误配置时挖掘每个单一目录。

为了您自己的利益,我建议您尽快将.htaccess文件配置转移到主配置文件中!