Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I was researching about how to make a php login but all I could come up with was.
<?PHP<br />
$uandp = file_exists($_POST["username"]."user".md5($_POST["password"]).".usr");<br />
if ($uandp=="1") {<br />
$myfile = fopen("access.port", "w") or die("Unable to login!");<br />
$txt = "John Doe\n";<br />
fwrite($myfile, $txt);<br />
$txt = "Jane Doe\n";<br />
fwrite($myfile, $txt);<br />
fclose($myfile);<br />
header("Location: adm1.php");<br />
} else {<br />
header("location: error.php");<br />
}<br />
?>

Which is not very secure. How can I do one with php and cookies(including moble users)?
Posted
Comments
vbmike 16-Jun-14 10:50am    
http://www.php-login.net/

1 solution

For really secure login, HTTP is not quite suitable. You would need an HTTPS page. Please see: http://en.wikipedia.org/wiki/HTTPS[^].

Even more important thing is: never ever store passwords anywhere. No one except the creator of the password should be able to read it, regardless the role and level of access. And this is absolutely not needed for authentication. Surprised? Disagree? Then think again. The usual schema is: use cryptographic hash function of a password and store only the hash, compare hash to hash for authentication. In all the password life cycle, it remains unknown to everyone (hash is not reversible, or, as they say, this is cryptographically infeasible) except the creator of a password. Please see:
http://en.wikipedia.org/wiki/Cryptographic_hash_function[^].

See also my past answers on the topic. None of them is related to PHP, but I tried to explain the ideas:
i already encrypt my password but when i log in it gives me an error. how can decrypte it[^],
Decryption of Encrypted Password[^],
storing password value int sql server with secure way[^],
TCP Connection with username and password[^].

Now, you should not use MD5 or SHA-1 family of algorithms, they have been found broken.
Use SHA-2 or even SHA-3 families. Please see:
http://en.wikipedia.org/wiki/MD5[^],
http://en.wikipedia.org/wiki/SHA-1[^],
http://en.wikipedia.org/wiki/SHA-2[^],
http://en.wikipedia.org/wiki/SHA-3[^].

This is how you can use implementation of SHA-2 in Javascript:
https://code.google.com/p/crypto-js[^],
and PHP: http://www.php.net//manual/en/function.hash.php[^].

SHA-3 is already implemented in PHP: https://github.com/strawbrary/php-sha3[^].
(And Javascript SHA-3 implementation is included in the Javascript library of Google Code referenced above.)

See also Javascript implementation of TLS:
https://github.com/digitalbazaar/forge[^],
see also: http://en.wikipedia.org/wiki/Transport_Layer_Security[^].

—SA
 
Share this answer
 
v4
Comments
Mohibur Rashid 16-Jun-14 23:02pm    
Plenty of valuable information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900