I've been building a system for inputting and monitoring shifts for casual staff, who work across multiple sites with the ability to generate accounting information.
I've had some help from Stack Overflow in building this project, as I had no prior knowledge of PHP or MySQL, and each time I posted some of my code I had comments about the lack of security.
In my system, sensitive information like salaries, work hours and things are protected by having different levels of user accounts. The code below is a snippet of code from my Admin Area allowing me to edit an account's userLevel and password.
if(isset($_POST['submit'])) {
$editid = htmlentities($_POST['id'], ENT_QUOTES, 'UTF-8');
$userLevel = htmlentities($_POST['userLevel'], ENT_QUOTES, 'UTF-8');
if(!empty($_POST['password'])) {
$password = password_hash("$_POST['password']", PASSWORD_DEFAULT)
$sql = "UPDATE users SET userLevel = ?, password = ?, salt = ? WHERE id = ?";
$stmt = $connection->prepare($sql);
$stmt->bind_param('ssss', $userLevel, $password, $salt, $editid);
} else {
$sql = "UPDATE users SET userLevel = ? WHERE id = ?";
$stmt = $connection->prepare($sql);
$stmt->bind_param('ss', $userLevel, $editid);
}
$stmt->execute();
}
I specify UTF-8 charset at the top of my php files, and all raw output variables, posts, gets and sessions are wrapped in htmlentities tags. My account passwords are hashed and salted.
My system is only used locally and will never be used on the web (unless I post the code somewhere, which I could do to help other new people learn), but as I'm learning I was told I should get in the habit of writing secure code. So finally we get to the question.. Is the above code of a secure enough level for the web? If not, please offer suggestions.
$stmt->execute();
outside theif
statement so it doesn't have to be there twice. Although that's just being extremely nitpicky. \$\endgroup\$