开发者

PHP Password Securing

I have the following piece of PHP coding:

<?php
session_start();

$data=array("user1"=>array("url"=>"file1.php","password"=>"pass1"),
"user2"=>array("url"=>"file2.php","password"=>"pass2"));

if(isset($_POST['username']) && isset($_POST['password'])) {
    if($data[$_POST['username']]['password'] == $_POST['password']) {
        $_SESSION['username'] = $_POST['username'] . " " . $_POST['password'];
        header('Location: ' . $data[$_POST['username']]['url']);
    } else {
        login('Wrong user name or password. <br>');
    }
} else {
    login();
}
?>

I would lik开发者_如何学编程e to have the $data=array be processed from another file so that it is a bit more secure. How would I accomplish this? Any idea?


data.php:

<?php

$data = array(.... user/password data here ...);

login.php:

<?php

include('data.php');

... process login as usual

This isn't any more "secure" than your version, as they both still have the passwords stored in plaintext, and anyone with file-level access to the webserver will be able to steal your user "database" without even blinking.

If you insist on this method, at least put the 'data.php' file somewhere OUTSIDE of your site's document root, so that it's not in an area that's easily reachable by remote web users.


This is not the correct way to secure content. You should never be storing the password in the session because it can be compromised. Here is a tutorial on loggin in:

http://www.phpeasystep.com/phptu/6.html

and here is a overview of php security:

http://www.phpfreaks.com/tutorial/php-security


You're on the right track when you say that you should store the password elsewhere. I would recommend a database, but if you must use a file, then you could just read in the file at the start of your PHP file.

The real concern about security here is that you're storing passwords in plaintext. If anyone gets a hold of your password file, they can see the passwords as-is. This is very dangerous. You should use techniques known as hashing and salting:

When the user registers a password, you hash a combination of the password and a user-unique random string known as a salt. Hashing is a one-way function, so you get a string that cannot be reversed to the original password but is still a "fingerprint" of it. You then store this "fingerprint" and the corresponding salt in the database.

When a login attempt is made, you calculate the hash in the same way, and see if the hashes match. That way, you never store the password in plaintext.

The reason for the salt is to prevent the usage of an attack tool known as rainbow tables. Using a per-user random salt prevents attackers from using precomputed password-to-hash tables.

Have a look at http://codahale.com/how-to-safely-store-a-password/ for more information on this topic.


I wanted to expand on Marc B's answer.

Keeping the passwords in plain text is never a good idea and keeping passwords in SESSION data is bad practice and will also not be persistent.

Here is a link to some notes that I took when I was learning about authenticating users with php.

http://pastebin.com/sFmBJda1

But long story short, you need to hash and store the hashed values in a database somewhere. Even if it's not a true database. Storing hashed values in a text file that you can parse is better than storing plain text passwords.

Just make sure that when it's time to authenticate the user, you hash their password input the same way you hash the passwords and then compare to your table/list of passwords you have stored.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜