That code is dangerous, you should not use it in it's current state.
- It blindly trusts
$_COOKIE
and creates SQL queries using string concatenation, without properly escaping input. Malicious users can easily perform SQL injection on any application using that class.
- The presence of the
var
keyword, the presence of a constructor using the same name as the class and the lack of access control keywords in front of the methods tells me that the code was written for PHP4, not PHP5. It will still run, though
Both of these are fixable problems. First, let's address your questions.
- The code written by @Delan Azabani is a good example of how to use the class.
- The code is confusing. At first, it looks like the code is using a real database object instead of the old, low-level functions provided by the
mysql
extension. But then it goes and calls functions from the mysql
extension! It's probably someone's custom-written wrapper. The code isn't actually creating a database object, it's simply referencing the value of $db
in the global scope, as you suspect.
You've mentioned that you're using the mysql
extension. I urge you to reconsider and use PDO or mysqli instead. Not only will this class work with either (though with some changes to mitigate the glaring security hole), but your own code will be better off using the more modern, safer techniques used in PDO
and mysqli
.
Let's fix the login
method, using PDO.
public function login($username, $password) {
global $db;
$sth = $db->prepare("SELECT user_id FROM users WHERE username = ? AND password = ?");
$sth->execute(array($username, $password));
if($sth->rowCount() == 1) {
$this->user_id = $sth->fetchColumn();
...
Let's go over what changed. First, we added query placeholders, those are the question marks. The prepare
method returns a statement handle, which you're already probably familiar with. Next, we tell the prepared statement to run itself using the execute
method, passing an array of variables. The variables will be automatically escaped for you and then inserted into the query in place of the placeholders.
After the query has run, we use rowCount
to pull back the number of matching rows. We want exactly one. Because we're pulling back the first column of the first row, we then use fetchColumn to grab just that bit of data.
The rest of the login method is just fine.
The check
method needs similar fixing up.
public function check($username, $password) {
global $db;
$sth = $db->prepare("SELECT user_id, password FROM users WHERE username = ?");
$sth->execute(array($username));
if($sth->rowCount() == 1) {
list($db_user_id, $db_password) = $sth->fetch(PDO::FETCH_NUM);
if(md5($db_password . $this->salt) == $password) {
$this->user_id = $db_user_id;
...
Once again, we prepare a query with placeholders, then execute with the actual variables. Again we want only one row, but this time we want everything in the row. We'll use PDO::FETCH_NUM
to tell fetch
that we want a numerically indexed array, then we'll take the two resulting entries in the array and stuff them in $db_user_id
and $db_password
, using them where the old code called mysql_result
. PDO doesn't let you pick and choose different columns from the same row using fetchColumn
.
If you wanted to use the mysqli
extension instead, there's a bit more work to be done. I hope I've convinced you to use PDO instead, though!
The rest of the class is, eh, adequate. Make sure to adjust the $domain
class variable at the top to match your actual domain name, it's used in the cookies.