@accra
A part Hiroshe's suggestion which I would strongly consider, to explain the pratical issue with your script, the problem is this:
$salt = uniqid(mt_rand()+microtime(true), true);
$password=hash('sha256',$password.$salt); // Encrypted password
Both in your registration and forget pages you are appending a $salt
variable to the password. If you do this, then you have to save the salt value to the database, separated from the password, for example:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`password` char(64) NOT NULL,
`salt` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
At the moment in your login page you are not considering anymore the salt, which will change and if you don't save the original value created during the registration or recovery (forget page) process, then you cannot sign into the secure session.
So, when you verify the login, the query at line 16
of the login page becomes:
$query = "select * from users where email = '$email' and password = (sha2(concat('$password', salt), 256))";
Here we're using the sha2()
function in MySQL which allows to use SHA-224, SHA-256, SHA-384, and SHA-512 hash functions. The first argument of sha2: is a concat()
between the password in clear text and the salt column that matches with the searched email; the second argument is the hash value, which in this case is 256
. So with a single query you can check if the user is allowed.
Note 1: uniquid()
is …