Important PHP Security Tips You Should Follow

Important PHP Security Tips You Should Follow

What do Wikipedia, Twitter, and Facebook have in common?

All of them use PHP technology. 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 Clfsrpm.net 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. 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.

要查看或添加评论,请登录

Sharad chandra Sharma的更多文章

  • Success & Failure just an Illusion

    Success & Failure just an Illusion

    Success n failure these two adjectives used in everyone life. These words are nothing more than a fake illusion.

  • empty() and isset()

    empty() and isset()

    This is just a quick tutorial regarding the empty() and isset() functions for people that are fairly new to the world…

    1 条评论
  • CDN - Introduction

    CDN - Introduction

    CDN – you keep seeing the acronym. Maybe in URLs, maybe on landing pages, but it never quite clicked – what are Content…

  • Secrets of being good PHP developer (Don't share)

    Secrets of being good PHP developer (Don't share)

    PHP has become the most popular programming language for Web applications. Many popular websites are powered by PHP…

  • Polymorphism Not Complicated anymore in PHP…

    Polymorphism Not Complicated anymore in PHP…

    Polymorphism is a long word for a very simple concept. Polymorphism describes a pattern in object oriented programming…

  • if you want to be a professional PHP developer then you must know

    if you want to be a professional PHP developer then you must know

    There are many beginners or even experienced PHP developers who don’t bother to follow the best practices of the…

    2 条评论
  • Why Use PHP Framework ???

    Why Use PHP Framework ???

    Are you Paying Lots of money to developer for using Wordpress with lots of plugging ? Being a PHP web application…

  • 15 Signs Of Problematic Clients You’ll Meet in IT MARKET (Freelancer / Upwork)

    15 Signs Of Problematic Clients You’ll Meet in IT MARKET (Freelancer / Upwork)

    In freelancing, it’s not the number of clients you have that will make or break your freelance business, but the kind…

    1 条评论
  • Tips For Becoming A Successful Software Developer

    Tips For Becoming A Successful Software Developer

    If you are a great developer, there will always be a market for you. However, landing a job in a global, innovative…

  • Create a Hashtag System with PHP?

    Create a Hashtag System with PHP?

    Hashtags are essentially a filter, allowing people to see the conversation associated with that word or phrase…

社区洞察

其他会员也浏览了