我正在构建一个私人 CMS 供我自己使用,并且我将开始构建用户名和密码存储功能。我正在考虑将所有管理员用户名、密码和用户详细信息存储在 PHP 文件中的多维数组中的可能性,而不是使用 SQL 将它们存储在数据库中。
我想要使用这种非传统方法来存储用户信息的原因是相信这将使攻击者更难获得对用户信息(用户名、密码、IP 地址等)的未经授权的访问,因为我不会连接到 MySQL 数据库。
粗略的代码大纲:
add_user.php
// set the last referrer session variable to the current page
$_SESSION['last_referrer'] = 'add_user.php';
// set raw credential variables and salt
$raw_user = $_POST['user'];
$raw_pass = $_POST['pass'];
$raw_IP = $_SERVER['REMOTE_ADDR'];
$salt = '&^${QqiO%Ur!W0,.#.*';
// set the username if its clean, else its false
$username = (is_clean($raw_user)) ? $raw_user : false; // is_clean() is a function I will build to check if strings are clean, and can be appended to an array without creating a parsing error.
// set the salted, sanitized, and encrypted password if its clean, else its false
$password = (is_clean($raw_pass)) ? $salt . encrypt($raw_pass) : false; // encrypt() is a function I will build to encrypt passwords in a specific way
// if username and password are both valid and not false
if( $username && $password ) {
// set the users IP address
$IP = sanitize($raw_IP);
// create a temporary key
$temp_key = $_SESSION['temp_key'] = random_key();
// random_key() is a function I will build to create a key that I will store in a session only long enough to use for adding user info to the database.php file
// add user details array to main array of all users
$add_user = append_array_to_file('database.php', array($username, $password, $IP));
// append_array_to_file() is a function I will build to add array's to the existing multidimensional array that holds all user credentials.
// The function will load the database.php file using cURL so that database.php can check if the temp_key session is set, the append_array_to_file() function will stop and return false if the database.php file reports back that the temp_key is not set.
// The function will crawl database.php to read the current array of users into the function, will then add the current user's credentials to the array, then will rewrite the database.php file with the new array.
// destroy the temporary session key
unset($_SESSION['temp_key']);
}
else {
return false;
}
数据库.php
$users_credentials = array(1 => array('username' => 'jack',
'password' => '&^${QqiO%Ur!W0,.#.*HuiUn34D09Qi!d}Yt$s',
'ip'=> '127.0.0.1'),
2 => array('username' => 'chris',
'password' => '&^${QqiO%Ur!W0,.#.*8YiPosl@87&^4#',
'ip'=> '873.02.34.7')
);
然后,我将创建自定义函数来模拟 SQL 查询(如SELECT),以用于验证尝试登录的用户。
我的问题
这是一个坏主意,如果是,为什么?
我是否正确地认为这将减少黑客试图获得未经授权的访问、嗅探/窃取密码等的可能性,因为我没有连接到远程数据库?