Here Are 8 Very Important PHP Security Tips You Should Follow

Kirjoitettu - Viimeisin muokkaus

What do Baidu, Wikipedia, Twitter, and Facebook have in common? All of them use PHP technology. W3Techs did a survey and revealed that many web applications and websites prefer to use PHP as opposed to ASP.NET or Java. PHP is popular for many reasons. It is easy and cheap to host PHP applications. You can do PHP development using open source software, which means you do not incur additional costs in acquiring additional software. Also, PHP connects with many databases. Even though developers make many media websites, education portals, and e-commerce social networking sites using PHP technology, there are a number of security issues you need to consider. This article highlights the security tips you should follow in PHP usage.

1. Validation of input data

As you outline your application, you need to ensure you protect it against terrible information. It is always advisable to avoid taking client input as dependable. Even though you have planned your application for good clients, you cannot ignore the fact that some clients will try to destroy your work by keying in wrong information. If you happen to approve and disseminate every piece of information you receive, there are chances of fabricating an application under developer’s protection. Ensure you approve all information in your PHP code. If you are using JavaScript to approve the contribution from your client, it is possible that your client may have killed JavaScript in his program. In such a situation, your application cannot approve any information. It is okay to approve in JavaScript, but you need to re-approve the information in PHP to make preparations for any unanticipated issues.

2. Escape query data

Do not stick with any information that takes you in an inquiry, and more importantly ignore SQL inquiries in the application. Make use of a certain type of deliberation such as active record. A slight SQL injection weakness can help a hacker to take over the entire framework. SQL map takes a few minutes to control a given framework. Therefore, if you have a single page with an SQL infusion, this can make the entire site defenseless. If there is a parameter that you have used in an SQL query and you do not escape it, you have a left a door open for hackers to interrupt your system. Therefore, beware of SQL queries. Ensure you escape every id parameter through using suitable functions such as mysqli_real_escape_string() before you put them in your query.

3. Guard against XSS attacks

Cross-website scripting assault (XSS assault) is an attack of infusion of codes in other helpless site pages. Through this, you risk having unsafe information which you have not checked, and you might display it in your program. For instance, if you have a remark shape in your code that allows clients to give information, and once they do they get various remarks, a client could enter a certain remark that has obsolete JavaScript code. Once the application presents the form, it sends the information to the server, and this is stored in the database. A little while later, the remark will emanate from the database and appear in the HTML page, and this makes the JavaScript code run. Any malicious JavaScript can divert a client to a phishing site, or similar. If you need to secure your site from such assaults, run your information through strip_tags to remove any labels. When you feel you want to demonstrate information in your program, use PHP htmlenminities () function.

4. SQL injection

This is a code injection technique that hackers use to attack applications that are data-driven. Hackers insert malicious SQL statements into the user entry fields. They use this information to get secret information from the database, and they can delete or modify any important information. For such kind of hacking to occur, attackers use any input from users and augment it with some parameters to come up with an SQL query that can harm the database. Though obtaining such information is not an easy venture, if your database is open source, hackers will have an easy time. You can prevent this by:

  • Use libraries such as MySQLi Extension (MySQL Improved) or PDO that help you with statements that have bound variables.

  • Delete all idle procedures from your site.

  • Ensure you perform data validation and confirm that every input from users has the anticipated data type.

  • Avoid usage of super user access to connect to your site. Always have customized users, and limit their privileges.

  • Ensure that you strongly type query APIs with parameters, and with some substitution markers.

 5. Cross Site Request Forgery (CSRF) attacks

One-Click attack, session riding, or Cross Site Request Forgery (CSRF) is a type of a web app susceptibility where victims inadvertently run a script in their browser when they are using a certain site. Session riding attacks happen if there is data coming into a website through certain users’ requests. CSRF exploits the trust that a client has for a certain site. The hacker uses a browsing website and special techniques to get access to sensitive data from other users. Such vulnerability is as a result of wrong assumptions and poor coding. To avoid such vulnerability:

  • Use open libraries such as NoCSRF, CSRF Protection, anticsurf, and to protect your website from CSRF susceptibility.

  • Use  Unpredictable Synchronizer Token Pattern. This method helps a website to generate a random token which associates with the current session of a user as a hidden value. Once the site submits the form, your site can verify if the random token has come through a request, and if it is right.

  • Use Completely Automated Public Turing Test to tell Computers and Humans Apart (CAPTCHA) verification in forms to avoid CSRF. CAPTCHA that comes from the side of the client is legitimate since no hacker can guess its pattern. However, this gives the user another burden, and it can destroy the overall web experience of the user.

  • The application must ascertain the referral header. The website should block any request that comes from another domain. You can do away with vulnerability if the site allows requests from a similar domain. If there are HTTPS connections, this method fails since it omits the referrer.

6. Protect session data

In shared hosting, you can get a script from someone that reads the session effectively. The session data has a temporary directory. In this way, ensure you do not expose sensitive data such as credit card numbers and passwords in a session. A perfect way to monitor information is to assess the data stored in the session. This is not fully secure since encoded information is not under protection. Keep your session information in a different database. Use session_set_save_handler() from PHP to hold information in your own manner.

7. User data

Any developer who uses PHP must ascertain that all user-centered data is valid in logic, size, and type. Sanitize the data before you use it in the system. PHP came up with filter_var that makes it easy to validate data. Filter_var performs data sanitization and data validation. In data validation, the function determines if the data is proper, and for data sanitization, it eliminates all illegal characters.

8. Proper handling of errors

You should know all the errors that occur as you develop your application. When end users can access the application, it is important to hide all errors. Any open error can make the site vulnerable to attacks. If you need to avoid this, configure your server differently. Turn off all display_start_up_errors, and display_errors. Ensure that log_errors and error-reporting are on, so you can identify them as you hide other errors from end users.

Understand that web application security is an endless process. All PHP developers should update themselves about all new forms of exploits, and new flaws. Ensure you do not trust user input, and guard your database and file system. Visit CGISecurity.Net, PHP Manual Security Section, OWASP, and The PHP Security Consortium for more guidance about PHP security.

Anything more to add? If you have suggestions about PHP security, share them in the comments section below!

Ilmoitettu 1 elokuuta, 2017

Ruchi Bhargava

Content Writing | Designing | Web Development

Hi! Are you looking for content that gets all the birds (read: customers) in one proverbial stone? Then I'm the person you need to talk to. After millions of words written for my clients, I can guarantee one thing: quality. When you're working with me, you'll get nothing short of perfect, well-researched, and enticing content. But that's not all I'm about. Along with creating custom cont...

Seuraava artikkeli

11 Tips To Help You Sketch Like A Real Architect