Back to Blog
Security

PHP Security Best Practices for Business Websites

By Ashker Published January 05, 2024 6 min read
PHP security best practices guide for protecting UAE business websites from common attacks

Short Answer

Secure a custom PHP website with prepared statements, password_hash, hardened sessions, CSRF tokens, locked-down uploads and a supported PHP version, checked against the OWASP Top 10:2025.

Custom PHP sites are often safer than bloated plugin stacks, but only when the code follows a few non-negotiable rules. One unparameterised database query or an upload folder that runs scripts can undo everything else. Automated attackers rarely need anything clever or targeted. They look for the same handful of mistakes on thousands of sites at once, day and night.

This guide is for UAE business owners commissioning custom PHP work and the developers building it. It covers database queries, passwords, sessions, forms, uploads and server versions, mapped against the OWASP Top 10:2025. Each section says what good practice looks like and what to ask your developer. Use it as a plain review checklist before launch and after major changes.

How should PHP code handle user input and database queries?

Treat every piece of input as untrusted, including form fields, URL parameters, cookies and headers. Validate it against what you expect, such as an email format or a whole number. Then use prepared statements with bound parameters for every single database query, without exception.

PHP's PDO and MySQLi extensions both support prepared statements, which keep data separate from the SQL command itself. That blocks SQL injection, which OWASP lists under A05:2025 Injection. Escape all output with htmlspecialchars when displaying user data, which prevents stored and reflected cross-site scripting in pages.

Quick Answer

Validate every input, use PDO or MySQLi prepared statements for all database queries, and escape output with htmlspecialchars. This combination blocks the injection attacks that OWASP ranks as A05:2025 Injection.

How should a PHP site store passwords and manage logins?

Store passwords only with PHP's built-in passwordhash function and check them with passwordverify. Never write your own custom hashing, and never use outdated functions such as md5 or sha1 for passwords. The built-in functions handle salting automatically and allow algorithm upgrades later through passwordneedsrehash.

Add rate limiting to login forms, lock or slow repeated failures, and offer multi-factor authentication for admin accounts. OWASP lists these weaknesses as A07:2025 Authentication Failures in its list. Admin panels should also sit behind restricted paths or IP allow-lists where the business workflow allows it.

Quick Answer

Hash stored passwords with PHP's built-in passwordhash and passwordverify functions, never md5 or sha1. Rate-limit login attempts and add multi-factor authentication for every admin and editor account on the website.

How do you secure PHP sessions and forms?

Regenerate the session identifier with sessionregenerateid after login, so an attacker cannot fix a known session identifier in advance. Set session cookies with the Secure, HttpOnly and SameSite attributes. Expire idle admin sessions after a sensible period, such as 30 minutes.

Every form that changes data needs its own CSRF token, a random value stored in the session and checked on submission. SameSite cookies help, but OWASP's cheat sheet still recommends tokens for state-changing requests. Add a honeypot field or rate limit to public contact forms to cut automated spam.

Quick Answer

Secure PHP sessions by regenerating IDs after login and setting Secure, HttpOnly and SameSite cookie flags. Protect every data-changing form with a unique CSRF token checked on the server side.

How should file uploads be locked down?

Allow only the file types the business genuinely needs, and check the real content type on the server rather than trusting the file extension. Rename uploaded files to random server-generated names. Enforce sensible size limits in both PHP settings and application code.

Store uploads outside the public web root where possible, or in a folder where script execution is disabled. An upload folder that runs PHP is one of the most damaging mistakes, because an attacker can then run code. Serve private documents through a script that checks permissions first.

Quick Answer

Lock down PHP uploads by allowing only needed file types, checking real content types, renaming files and limiting size. Store them outside the web root or where scripts cannot execute.

How do server settings and PHP versions affect security?

Run a PHP version that still receives official security fixes. The PHP project supports 8.2 with security fixes only until 31 December 2026, while 8.3, 8.4 and 8.5 continue longer. Unsupported versions receive no patches at all, even for serious, publicly known vulnerabilities.

In production, turn off display_errors and log errors privately instead, since error pages can leak paths and queries. Keep configuration files, database credentials and API keys outside the public web root. Add security headers such as Content-Security-Policy and Strict-Transport-Security, tuned carefully to what the site actually loads.

Quick Answer

Run a supported PHP version, since PHP 8.2 security fixes end on 31 December 2026. Disable display_errors in production, keep credentials outside the web root and add suitable security headers.

Main Takeaways

Secure PHP sites validate all input, use prepared statements for every query and escape all output. Hash passwords with password_hash, rate-limit logins and add multi-factor authentication for every admin account. Regenerate sessions after login, use CSRF tokens and lock uploads away from script execution. Always run a supported PHP version, since PHP 8.2 security fixes end on 31 December 2026.

Reviewing your PHP site's security

PHP security comes down to everyday coding discipline rather than exotic tools or expensive plugins. The same few rules cover most of the OWASP Top 10:2025. A developer who follows them consistently produces custom sites that stay safe for years. One who skips them leaves gaps that no firewall or hosting plan can close.

If you handle customer data, our overview of UAE compliance for business websites explains the legal side alongside the technical controls. Auronix Web Studio can build custom PHP sites to this checklist through its website development service. Ask any developer to walk you through each section before you sign off a launch. For existing sites, our maintenance and support plans include security monitoring, updates and backups.

Sources & References

  • OWASP Foundation: OWASP Top 10:2025
  • OWASP Cheat Sheet Series: Cross-Site Request Forgery Prevention
  • OWASP Cheat Sheet Series: File Upload
  • PHP Manual: password_hash
  • PHP Manual: Prepared statements and stored procedures (PDO)
  • PHP Group: Supported versions
  • FAQs

    Questions readers usually ask next

    These FAQs are written to match the topic of this post and to help readers move from understanding to action.

    Is custom PHP more secure than WordPress?

    Custom PHP has a smaller attack surface without third-party plugins, but it is only as safe as its code. Poorly written custom code can be less secure than maintained WordPress.

    What is the OWASP Top 10:2025?

    It is the Open Worldwide Application Security Project's awareness list of the most critical application risks. Broken Access Control ranks first, followed by Security Misconfiguration and Software Supply Chain Failures.

    Are prepared statements enough to stop SQL injection?

    Prepared statements stop injection through bound values, which covers most queries. Table or column names cannot be bound, so always check those against a fixed allow-list inside your PHP code.

    Should a PHP contact form use CAPTCHA?

    A honeypot field and server-side rate limiting stop most automated spam without annoying visitors. Add a CAPTCHA only if spam still continues, since challenges can reduce genuine enquiries on mobile.

    Which security headers should a PHP website send?

    Start with Strict-Transport-Security, X-Content-Type-Options, Referrer-Policy and a Content-Security-Policy suited to what the site loads. Test the content policy in report-only mode first, because overly strict rules can break legitimate scripts.

    How should API keys be stored in a PHP project?

    Keep keys in environment variables or a configuration file outside the public web root. Never commit secrets to Git repositories, and rotate any key that has ever been exposed publicly.

    What should PHP log for security monitoring?

    Log failed logins, password resets, permission errors and admin changes with timestamps and IP addresses. Avoid logging passwords or full card data, and review the logs regularly for unusual patterns.

    How often should a custom PHP site get a security review?

    Review it before launch, after major feature releases, and at least once every calendar year. Also review whenever PHP moves toward end of support, since upgrades can expose outdated code.

    What is broken access control in a PHP website?

    It means users reach data or actions they should not, such as changing an ID in a URL. Every request must check permissions on the server, not only hide links.

    What should happen if a PHP site is hacked?

    Take the site offline or into maintenance mode, preserve logs and restore from a known clean backup. Then patch the entry point, rotate every password and review access before relaunching.

    Related Resources

    Need help hardening a live PHP site?

    We review forms, login flows, uploads, headers, backups, and maintenance routines so a site is safer without becoming harder to manage.

    Built for business sites that need practical hardening, better recovery planning, and fewer avoidable risks.

    WhatsApp Start project chat