我有一个网站,我让用户输入他们的用户名/密码以登录并将这些信息存储在一个文件中。这是我当前的代码:
function getPassword( $user )
{
$passwords= array
(
'Admin' => '123456',
'Moderator' => 'abcde'
);
eval(file_get_contents('./login.info')); //<--- THIS is where usernames/passwords are stored
$password = $passwords[ $user ];
if ( NULL == $password )
return NULL;
return array( $user, $password );
}
这是我为用户创建新帐户的代码:
<?php
if((isset($_POST['username']))and(isset($_POST['password']))){
$file = "login.info";
$fh = fopen($file, 'a');
//prevent sql injection
function check_field($fh)
{
if(!preg_match("/[^a-zA-Z0-9\.\-\_\@\.\+\~]/",$fh))
return TRUE;
else
return FALSE;
}
if(!check_field($_POST[username]))
{
header("Location:illegalchars.html");
break;
}
if(!check_field($_POST[password]))
{
header("Location:illegalchars.html");
break;
}
fwrite($fh, '$passwords["'.$_POST['username'].'"]="'.$_POST['password'].'";');
fclose($fh);
header("Location:success.html");
break;
}
?>
我知道我的代码不漂亮.. 并且存在重大问题。
其中之一是:如果有人使用用户名 x 创建帐户,任何人仍然可以使用新密码创建 x 以获得控制权。
我的简单解决方案是将管理员帐户移到eval(file_get_contents('./login.info'));顶部,并将新帐户附加到新用户/通行证列表的顶部。但是,我不明白为什么将 eval 放在数组顶部不起作用。另外,我如何才能将代码附加到列表顶部。任何帮助是极大的赞赏。
==EDIT== 我知道对这段代码有很多批评,但是有人可以回答这个问题吗?我目前并没有试图提高安全性/性能(这是一个概念验证游戏,最终,这整个事情无论如何都必须重写)。我只想要一个功能脚本,请回答问题?:]