How build a secure PHP application

[vc_row][vc_column][vc_column_text]

Disclaimer: Nothing is 100% secure and this article in no ways guarantees the security of your web application.

That being said, I would like to emphasize that even though nothing is 100% secure it is possible to make it harder for the attacker to do any harm. So in this article we’re going to look at some of the good practices to secure your PHP application.

Although it is not possible to cover the entire security aspects of securing a PHP application in a single article, I’ve tried my best to include the overall ideas that need to be implemented. Will write more detailed articles for every point in here in coming days.

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]

1. Sanitize and Validate User Inputs

In my experience majority of the attacks on PHP applications are successful only because the user inputs were not validated and sanitized. It is good to be paranoid and trust absolutely no one when it comes to accepting inputs. Not even in the admin section.

If you’re using any of the PHP frameworks like Laravel, CodeIgniter, PhalconPHP or Zend,  there are functions built in to sanitize user inputs. But if you’re building an application using core PHP, it would make sense to write a method to accept raw input string and return sanitized safe string.

Besides sanitizing the user input, perform necessary validations on input strings wherever necessary.
For e.g., ensuring email address is in the for format [email protected] and does not contain any special symbols.

Just by having proper sanitization and validation  rules in place, you can make it harder for an attacker to perform following attacks:

  • SQL Injection
  • Cross Site Scripting (XSS)
  • Cross Site Request Forgery (CSRF)

[/vc_column_text][vc_column_text]

2. Secure Query Execution

There are 2 main techniques to securely execute queries and prevent SQL injection:

a. Escape Quotes
It is important that you escape the quotes in inputs passed to the SQL query. If you’re using MySQL, you can use mysqli_real_escape_string() to get a string compatible with MySQL.

b. Prepared Statements
Prepared queries are the best way to avoid SQL injection attacks because prepared statements are like compiled templates to execute a query.
Values are passed into the compiled template and if they don’t match the placeholders in the template, SQL query fails.[/vc_column_text][vc_column_text]

3. File Permissions

Setting proper permissions and ownership to files and folders can prevent them from being accessed by unauthorized user.

  • Never ever use 777 for folders containing file uploads. Instead use 755.
  • Use 644 for all other (including php) files.

[/vc_column_text][vc_column_text]

4. File Uploads

I believe majority of the PHP applications at some point require file uploads funtionality to be implemented. For e.g., to upload profile picture of a user.

Although it may seem like a fairly simple task, it carries significant security risk if the file is not validated before storing it.

The most basic thing you can do is check if the file extension is valid for a particular file upload or not. For e.g., Only .jpg, .jpeg, .png should be allowed for profile picture and other extensions should be banned.

As a form of additional security measure, create a .htaccess file in your “uploads” folder and put the following content in it. This will prevent any PHP file inside the “uploads” from executing.

<Files *.php> deny from all </Files>[/vc_column_text][vc_column_text]

5. Regenerate Session IDs

Session Hijacking

[/vc_column_text][vc_column_text]

6. Implement Web Application Firewall

[/vc_column_text][vc_column_text]

7. Activate mod_security module for Apache2

[/vc_column_text][/vc_column][/vc_row]

Best Places to Start Learning HTML 5, CSS 3, Javascript, PHP, MySQL

If you are planning to continue with Web Application programming you need to have knowledge of
1. HTML 5
2. CSS 3
3. Javascript
4. PHP  &  MySQL (My preferred language and database engine)

I am going to continue to write my most of the articles related to web application programming on PHP – MySQL pair as it is my favorite and immensely dynamic.
Lets discuss each entity’s role and importance in web application programming.

1. HTML 5
Now many of you have heard about HTML. It is used for creating web pages etc.. and probably wondering what is HTML 5? Well HTML 5 is nothing but a new and advanced version of HTML. It has got tons of new features, tags etc. If you intend to excel in web application development you must conquer HTML 5 first.
Here are a few important websites that will get you started with HTML 5.
1. Tizag.com/htmlT/
2. W3Schools.com/html5/default.asp
3. html5demos.com (An excellent website for you to checkout new features of HTML 5)
4. W3.org/TR/html5/ (A detailed reference of HTML 5 and its features)

You might also wanna buy these books that contains some quick and awesome HTML 5 hacks.
1. Pro HTML5 Programming: Powerful APIs for Richer Internet Application Development
US/UK/CA                  India
 

2. HTML5 24-Hour Trainer (Wrox Programmer to Programmer)
US/UK/CA                  India
 

2. CSS3
Just as HTML 5 is new version of HTML, CSS3 is the most recent version of CSS (Cascading Style Sheet). CSS is generally used to enhance a website’s look. It has a lot of features like animations, text color, shadows, backgrounds, transitions, custom borders and its colors etc.
Here are a few important websites that will get you started with CSS3.
1.Tizag.com/cssT/
2. W3Schools.com/css3/default.asp
3. CSS3.com

You might also wanna buy these books to know extra stuff about styling your website.
1. Basics of Web Design: HTML5 and CSS3
US/UK/CA                  India
 

3. Javascript
Javascript is a browser sided scripting language. The script is downloaded on client side and executed as the calls are made. Javascript is used to enhance the interactivity and looks of a website. It is used to create drop down menus, perform run-time calculations, update content view dynamically via AJAX(A combination of XML and Javascript to update records/views dynamically at run-time). To reduce the coding work there are many Javascipt libraries like JQuery and MooTools which help you in adding some awesome effects to your website in very less time.
You can visit the following websites to start learning Javascript.
1. Ttizag.com/javascriptT/
2. W3schools.com/js/default.asp
3.Codeacademy.com

Books that can help you learn Javascript even better.
1. Professional JavaScript for Web Developers (Wrox Programmer to Programmer)
US/UK/CA                  India
 

2. JavaScript for Absolute Beginners
US/UK/CA                  India
 

4. PHP and MySQL
PHP-MySQL is the most powerful hence most preferred solution for web based application. PHP provides powerful programming flexibility while MySQL provides us with secure, fast and reliable database management facility. PHP is very easy to understand and one can quickly learn it if he/she has learned the basics of programming by studying C and C++. With ability of numerous inbuilt functions programming a web application becomes even faster. To add to this there are various  frameworks like Code Igniter, Zend, CakePHP which reduce your programming even further since a LOT of functions are available in them too. PHP is both procedural as well as Object Oriented language. The best part is we can use it with HTML. With help of HTML+Javascript+CSS you can create a jaw-dropping look while by using PHP+MySQL with HTML you can create mind blowing PHP application.

Books for PHP and MySQL
1. Beginning PHP and MySQL: From Novice to Professional
US/UK/CA                  India
 

2. PHP Solutions: Dynamic Web Design Made Easy
US/UK/CA                  India
 

3. PHP 6: A Beginner’s Guide
US/UK/CA                  India
 

So this was pretty much I could possibly help you with basics of web application programming. I hope you complete it fast and as you complete, don’t forget to go through intermediate-advanced topics I would be discussing in my upcoming programming posts.
I hope this article helps you. Share it if you like it.
Good Luck and Thank You!!