if($_SERVER["REQUEST_METHOD"] == "POST"){
require('config.php');
$myusername=htmlentities($_POST['userName'], ENT_QUOTES, "UTF-8");
$mypassword=htmlentities($_POST['passWord'], ENT_QUOTES, "UTF-8");
// DO DELETE . .. WHERE user='".$myusername."'
// DO SELECT . .. WHERE user='".$myusername."'
// DO INSERT
// OR
// DO UPDATE . .. WHERE user='".$myusername."'
}
Hi @fumeeptc, not they are not, you should take care of them yourself. You should look into mysqli prepared statements. I suggest you even use the PDO extension instead as it makes it a bit easier to deal with prepared statements.
Cheers
Mysqli also has prepared statements, just not as convenient to use as PDO ones. They add a security layer to your queries. Please do some research and you’ll learn how to use them
Regarding your use of htmlentities(), was that same function applied to the strings when the data was inserted into the table? If not, it may create a mis-match. I would generally use it, or htmlspecialchars() rather, on outputting the data to an html page.
With the user name, if it’s not a “real name” you may restrict the character set available, then strip down any input to that set to ensure it is clean.
With the password, that needs to change. Don’t store raw passwords, it’s a security no no. The password should be hashed after creation, before insertion. The hashed string will be database safe and unreadable to anyone who happens to see it.
In the login, the password input should be checked against the hashed version in the database via password_verify()
As it stands, assuming the pseudo queries shown, it’s vulnerable. Handling the password like I said will take care of that part. With the user name, do as @Andres_Vaquero says:-
How you validate and sanitise depends of what you decide to accept as valid data for that field. do you allow any string, or a restricted set of characters?
I’ve got a full example but again, for PDO only.
Though I hope that the OP will change their mind and start using PDO prepared statements, which in the end will save their time, as rewriting mysqli to PDO prepared statements will take less time and effort than rewriting mysqli to mysqli prepared statements.
You don’t have to start with OOP.
Object oriented programming is a very complex science, takes several years of hard learning to take into. So for now just forget it.
What you really need is an Object oriented syntax, and it’s really simple, you can learn it in a few minutes for sure.
An object is a variable that may contain either another variable (called a property) or a function (called a method). Both addressed via Arrow operator (->). That’s all.
So instead of
mysqli_query($mysqli, $query);
you can write
$mysqli->query($query);
where query() is a method of the $mysqli object.
Similarly, instead of
mysqli_error($mysqli);
you can write
$mysqli->error;
where error is a property.
As you can see, OOP syntax which is shorter and cleaner.
As of PDO, which you may have probably confused with OOP, it’s really simple too - a couple of functions to tun your queries with prepared statements. And example you can find by the link above.
It is the way you will have to go when you start dealing with a lot of code and large applications. It basically is just a tool to help you organise, reuse and architect your code better. It takes some time to grasp the concepts as practical examples but once you do you wouldn’t go back to procedural coding.