Ok before we go any further get off MySQL - this library is deprecated and removed from later versions of PHP. Convert to MySQLi - which is close enough to MySQL that the changes are not extensive.
Next - don't use your POST variables directly in your query - you are just asking for trouble.
2. Sanitize your variables before putting them in a query. This means, preferably, use a prepared statement and an escape string function to neutralise any unwanted malicious code in the variable.
A logon script is a prime target for a hacking attack so it is important you get this right.
3. It appears you are storing your password's in plain text - this is not a good idea - you should salt and hash your passwords password_hash() and password_verify() should be the default goto's here
Consider using token based security - don't store the username and password in the cookie - store an unguessable token and use this to do your auto logins.
A token is a value that cannot be guessed and is unique (think UUID / GUID) that is associated with a user account and optionally a time period. The token is exchanged (in a cookie or a header) with each interaction with the server. The token is checked by the server to see it is valid (exists and has not expired) before any data is sent back to the client.
5. Then these lines had me scratching my head a bit
You are setting your session variables to the constant strings 'username' and 'password' - are you not meaning to save the values in the variables (which you will have verified and sanitized) $username and $password?
AHMED SAMY
ASKER
can you tell me how to protect username and password texts
Next - don't use your POST variables directly in your query - you are just asking for trouble.
1. Don't assume they exist
$username = isset($_POST['username']) ? $_POST['username'] : false;
2. Sanitize your variables before putting them in a query. This means, preferably, use a prepared statement and an escape string function to neutralise any unwanted malicious code in the variable.
A logon script is a prime target for a hacking attack so it is important you get this right.
3. It appears you are storing your password's in plain text - this is not a good idea - you should salt and hash your passwords password_hash() and password_verify() should be the default goto's here
4. This is not a good idea
Open in new window
Consider using token based security - don't store the username and password in the cookie - store an unguessable token and use this to do your auto logins.A token is a value that cannot be guessed and is unique (think UUID / GUID) that is associated with a user account and optionally a time period. The token is exchanged (in a cookie or a header) with each interaction with the server. The token is checked by the server to see it is valid (exists and has not expired) before any data is sent back to the client.
5. Then these lines had me scratching my head a bit
Open in new window
You are setting your session variables to the constant strings 'username' and 'password' - are you not meaning to save the values in the variables (which you will have verified and sanitized) $username and $password?