The mysql_real_escape_string() function escapes data going into the database, and as such it should be invoked upon the data before it is used in the query. In your snippet above, it would be applied onto the $_POST['username'] variable whilst being assigned to the $username variable. You may also want to take a look into filtering your input data, where you can limit the user's input by validating the data entered. For example, if you'd only like usernames to contain alphanumerical characters, then you can use the ctype_alnum() function. These are basic concepts to enabling user input into your web application, and they are a necessity to learn.

In regards to your query, you could simplify the logic by querying for the username and password, and then use MySQL's COUNT() function to return the number of rows selected. This will also be a more optimised method, because we firstly aren't having to fetch all columns (using the * wildcard, which isn't even needed), and secondly we aren't requiring data to be returned; only the number of selected rows.

You may also want to look into a more modern API, such as MySQLi. In the MySQLi API, you can make use of another escaping method, prepared statements, along with much other functionality not seen in the original MySQL extension.

I'd personally avoid using the stripslashes() function, especially since this is user input, and we don't want to deform it before storing it for data persistence. It's a function that I'd only use if I knew that the HTML tags were well-formed (ie, stripping the already-parsed BBCode tags), and it would be applied upon data output. This is only really needed if you're looking to give a preview of part of the text, where the HTML styling is not needed/wanted. Otherwise, I'd still opt to go with htmlspecialchars().

vectorialpx
—
2013-02-21T19:42:12Z —
#4

modernW said:

I'd personally avoid using the stripslashes() function, especially since this is user input, and we don't want to deform it before storing it for data persistence. It's a function that I'd only use if I knew that the HTML tags were well-formed (ie, stripping the already-parsed BBCode tags), and it would be applied upon data output. This is only really needed if you're looking to give a preview of part of the text, where the HTML styling is not needed/wanted. Otherwise, I'd still opt to go with htmlspecialchars().

I think you miss the difference between stripslashes and striptags stripslashes will just make sure that if magic_quotes_gpc is ON, we will not get a double-escape (for quotes).

tpunt
—
2013-02-21T22:33:27Z —
#5

vectorialpx said:

I think you miss the difference between stripslashes and striptags stripslashes will just make sure that if magic_quotes_gpc is ON, we will not get a double-escape (for quotes).

Whoops, sorry. I misread it as striptags() for some reason or another. Because magic_quotes_gpc was deprecated in PHP 5.3, I don't really think it's overly necessary to strip the slashes beforehand though.

SpacePhoenix
—
2013-02-22T07:01:52Z —
#6

tpunt said:

You may also want to look into a more modern API, such as MySQLi. In the MySQLi API, you can make use of another escaping method, prepared statements, along with much other functionality not seen in the original MySQL extension.

Also another reason to migrate away from the mysql_* extension is that the mysql_* extension is now deprecated as of the current version of php. An alternative extension to migrate over to is PDO

ReGGaeBOSS
—
2013-02-22T07:52:22Z —
#7

tpunt said:

You may also want to look into a more modern API, such as MySQLi. In the MySQLi API, you can make use of another escaping method, prepared statements, along with much other functionality not seen in the original MySQL extension.

Hmm many people is telling me this, but my concern is, now i have spent ages building my little CMS, isnt it hard for me as a beginner in php and mysql to "convert" my code to mysli? Should i try to do it or check it out for my next project?

vectorialpx
—
2013-02-22T08:21:40Z —
#8

SpacePhoenix said:

An alternative extension to migrate over to is PDO

+1 for PDO. OOP is way better.

but my concern is, now i have spent ages building my little CMS, isnt it hard for me as a beginner in php and mysql to "convert" my code to mysli?

If it's your project, you know better what's there. However, PDO will show to you new limits

tpunt
—
2013-02-22T10:28:12Z —
#9

ReGGaeBOSS said:

Hmm many people is telling me this, but my concern is, now i have spent ages building my little CMS, isnt it hard for me as a beginner in php and mysql to "convert" my code to mysli? Should i try to do it or check it out for my next project?

Converting it to the procedural MySQLi API is simple because both APIs are very similar. Take the following script written with the original MySQL extension (taken from a PHPMaster article):

Aside from the changing of mysql_* to mysqli_*, the only real difference is that we are forced to pass the connection argument as the first parameter to the mysqli class functions (the first table), as opposed to the optional passing of it as the second argument in the original MySQL extension.

ReGGaeBOSS
—
2013-02-24T07:16:51Z —
#10

tpunt said:

Converting it to the procedural MySQLi API is simple because both APIs are very similar. Take the following script written with the original MySQL extension (taken from a PHPMaster article):

Aside from the changing of mysql_* to mysqli_*, the only real difference is that we are forced to pass the connection argument as the first parameter to the mysqli class functions (the first table), as opposed to the optional passing of it as the second argument in the original MySQL extension.