I am trying to create a signup form that allows potential users to input their first and last name, a username and a password. With this password, I want to use PHP's hash function and some "salts" to make the password secure. To do so, I created a function inside my PHP script to save on redundancy. To accomplish this, I changed variables $connection, $iniSalt, and $endSalt to global variables - thinking this would allow me to use them inside the function addUser(). Can someone tell me what I am doing wrong here? Any advice on how to go about this would be greatly appreciated. Here is my code:
global $connection;
$connection = new mysqli($db_host, $user, $pass, $db);
if ($connection->connect_error) {
die($connection->connect_error);
}
$query = "CREATE TABLE users (
firstname VARCHAR(32) NOT NULL,
lastname VARCHAR(32) NOT NULL,
username VARCHAR(32) NOT NULL UNIQUE,
password VARCHAR(32) NOT NULL
)";
$result = $connection->query($query);
if (!$result) {
die($connection->error);
}
global $iniSalt, $endSalt;
$iniSalt = "xb&z*";
$endSalt = "nb!@";
function addUser($conn, $firstname, $lastname, $username, $password) {
$token = hash('ripemd128', "$iniSalt$password$endSalt");
$query = "INSERT INTO users VALUES('$firstname', '$lastname', '$username', $token)";
$result = $connection->query($query);
if (!$result) {
die($connection->error);
}
}
addUser($connection, 'Bill', 'Murray', 'bmurray', 'mysecret');
addUser($connection, 'Jacki', 'Hughes', 'jhughes', 'somepw');