Pages

Wednesday, 17 September 2014

PHP Security: Never trust your users

You should always clean your user input for safe use. This includes escaping strings correctly, stripping html and tags, preventing SQL injection, and other security issues. Anytime you use a $_POST, $_GET, or receiving any information that the user can modify, you must be sure that you properly escape it, and clean it in all cases.

Let's get into the details.

Clean All User Inputs

Anytime you use a $_POST, $_GET, or are receiving any information that the user can modify, you must be sure that you properly escape it, and clean it in all cases.

Preventing SQL Injection

Let's take a login form as an example to query our database and check to see if they logged in correctly. You might use a query like

select `username`, `password` from `users` where `username` = $uname' and `pass` = '$pass';

If someone were to type in ‘ or username like’%admin%’; into the login form, they would be logged in with an account that has the word admin in it. To fix this problem, after connecting to the MySQL server, you have to use

 mysql_real_escape_string($uname);

Using this will escape all characters that need to be escaped to prevent tampering with the MySQL query.

Another problem for MySQL is % and _, which can be escaped using

 addslashes($uname);

HTML filtering

Sometimes you may want to clean certain html entities in strings. To do this, you use

 $var = “<b>bold</b>”;
 htmlentities($uname);

This would output:

 &lt; b &gt; bold &lt; /b &gt;

To change it back to a usable form, you use

  html_entity_decode($var);

To strip the HTML tags from a string, and specify which strings you want to allow, you use

 $var = “<a><b>link</a></b>”;
 strip_tags($uname,'<a><b>');


The second argument is not needed, passing just the variable you want to clean will strip all tags from the string. This example would only allow <b> and <a> tags through. However, it is important to note that strip_tags() is not failsafe; that is, malformed tags can remove more or less than required.

To make sure that html does not render if it gets shown, you can use

  htmlspecialchars($uname);

If you have a string that is escaped from using mysql_real_escape_string() or addslashes(), you can use stripslashes($uname) to remove all of the slashes.
Putting it all together into a function, something like:

  function CleanStr($var) {
      stripslashes($var);
      htmlentities($var);
      strip_tags($var);
      return $var;
  }

Thursday, 11 September 2014

MYSQL: mysqldump error

I wanted to take a backup of the database using the mysqldump command and got the following error:

mysqldump: Got error: 1044: Access denied for user root'@'localhost' to database 'information_schema' when using LOCK TABLES 

It might be the possibility that because I was taking the backup from a remote server, I did not have the LOCK TABLES permissions. mysqldump locks the tables before taking the dump of the database.

A quick solution to this would be to pass the –single-transaction option to mysqldump:

$ mysqldump –single-transaction -u user -p database_name > backup_file.sql

Another solution would be to grant LOCK TABLES to the user you are using to take the mysqldump. But if you do not have grant rights its good to use --single-transaction.

NOTE: There might be some cases where --single-transaction might fail. In that case use -single-transaction [use single dash (-) instead of a double dash (--)].

Wednesday, 10 September 2014

Do hyphens (-) in MySQL table names cause issues?

Yes, it causes issues if you use it directly like

DESCRIBE my-table;

This will definately cause issues. You will get the following error:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-table' at line 1

We can resolve this issue enclosing the tablename in backticks (`). The identifier quote character is the backtick (`). So the syntax will be

DESCRIBE `my-table`;

For more details about the permitted characters in unquoted identifiers, check the MySQL identifiers HERE.


Monday, 8 September 2014

What is the difference between == and === in PHP?

The == operator just checks to see if the left and right values are equal. But, the === operator (note the extra “=”) actually checks to see if the left and right values are equal, and also checks the variable types.

Friday, 10 January 2014

How to get a free domain and free hosting?


Steps to make your free domain and free hosting:


- Click dot.tk and login [with google or facebook or email or whatever]

- Once you signed in go to the domain panel tab on top, and select add a domain domain Ex. coolwebsite.tk

- Select free domain and press next.

- Put the registration length to 12 months.

- Once you are done with your .tk domain on dot.tk, register with 000webhost.com and create your new .tk domain.

- Or login to your 000webhost.com account and create a new domain if already registered.

- Select "I want to host my own domain" and enter your .tk website name and set a password.

- When the account is created there will be a notice on top that your namedserver is not set.

- At the right you can view the account details.

- Copy the IP address and go to your dot.tk account and go to the domain panel.

- Click on "MODIFY" to add the IP address to your selected domain Ex. coolwebsite.tk

- Select "Dot TK DNS Service".

- Under the hostname put your .tk domain name [Ex. coolwebsite.tk] and under the IP enter the IP you copied from 000webhost.com and then click "Add New".

- Again under the hostname put your .tk domain name with www [Ex. www.coolwebsite.tk] and under the IP enter the IP you copied from 000webhost.com and then click "Add New".

- Click "Save Changes".

- Everything is now set perfectly.

- You will have to wait for a while around 15-20 mins to activate the server.

- You can check my website:  http://traffix.tk