While you can include some lightweight client-side validation to improve user experience (e.g., ensuring required fields are filled), all critical checks and anti-spam measures should happen on the server. Attackers can bypass JavaScript entirely by sending requests directly to your server.
What to do:
Validate all inputs server-side.
Sanitize and escape user input to prevent injection attacks (e.g., SQL injection, XSS).
Reject suspicious or malformed input.
2. Implement CAPTCHA or Invisible reCAPTCHA
CAPTCHAs are one of the most effective tools to prevent spam bots from submitting forms.
Options:
Google reCAPTCHA:
Use reCAPTCHA v3 (invisible) or v2 (checkbox-based) to detect and block bots.
Custom CAPTCHAs:
Create simple challenges like:
Math problems (e.g., "What is 3 + 4?")
Selecting specific images (e.g., "Click all cars").
A hidden honeypot (described below).
3. Use Honeypot Fields
A honeypot field is an invisible form field that real users won't fill out, but bots typically will (because they try to fill every field). If the honeypot field is filled, you can safely assume it's a bot.
How to Implement:
Add a hidden field in your form (CSS display:none or a visually-hidden class).
Name it something generic like email_confirm or user_age.
On the server, check if the field is filled. If it is, reject the submission.
4. Rate-Limiting and Throttling
Spammers often bombard forms with automated requests. Rate-limiting prevents this by restricting how many requests can be made from a single IP address or session within a given timeframe.
What to do:
Use tools like Nginx rate-limiting or libraries like express-rate-limit (for Node.js).
Allow a limited number of form submissions per IP (e.g., 3 submissions per minute).
Block IPs temporarily if they exceed the threshold.
5. CSRF Protection
Bots often attempt Cross-Site Request Forgery (CSRF) attacks by submitting forms on behalf of logged-in users. Implement CSRF tokens to ensure that only legitimate users can submit forms.
How it works:
Generate a unique token on the server when the form is rendered.
Include the token in the form as a hidden field.
On form submission, validate the token server-side.
6. Content Filtering and Validation
Analyze form submissions for spammy patterns or suspicious input, such as:
Excessive links or certain keywords.
HTML tags (often used in spam messages).
Extremely large input sizes.
What to do:
Set a character limit for each form field.
Block common spam patterns (e.g., repeated use of the same link).
Use libraries like Akismet (popular in WordPress) to filter spammy content.
7. IP Blacklisting
Block known spammer IP addresses using a blacklist. You can use services like Project Honey Pot or create your own blacklist.
What to do:
Log suspicious activity (e.g., high submission rates from the same IP).
Factory: https://www.factory.ai/ - Factory helps organizations automate and optimize their software development lifecycle with an enterprise platform built for AI-human collaboration.
All Hands: https://www.all-hands.dev/ - Use AI to tackle the toil in your backlog, so you can focus on what matters: hard problems, creative challenges, and over-engineering your dotfiles
Dosu: https://dosu.dev/ - Dosu lets engineers focus on value-add work by answering questions, triaging issues, and maintaining documentation for them.
Lindy: https://lindy.ai/ - A personal AI assistant for managing schedules, tasks, and more.
11x: https://www.11x.ai/ - Digital workers,
Human results. For Sales, RevOps, and Go-to-Market Teams.
LangGraph: https://langchain-ai.github.io/langgraph/ - library for building stateful, multi-actor applications with LLMs, used to create agent and multi-agent workflows
LiveKit Agents: https://github.com/livekit/agents - The Agents framework enables you to build AI-driven server programs that can see, hear, and speak in realtime. It offers a fully open-source platform for creating realtime, agentic applications.
Arize: https://arize.com/ - AI observability platform for monitoring and improving model performance.
Weave: https://www.weave.ai/ - Agentic AI-Powered Decision Management
The only end-to-end AI and SaaS solution that can automate risk management, due diligence, and compliance
LangFuse: https://langfuse.com/ - Open Source
LLM Engineering Platform
Traces, evals, prompt management and metrics to debug and improve your LLM application.
AgentOps.ai: https://www.agentops.ai/ - Every Agent
Needs AgentOps.
Industry leading developer platform to test and debug AI agents.
We built the tools so you don't have to.
Braintrust: https://www.braintrust.dev/ - Ship LLM products that work. Braintrust is the end-to-end platform for building world-class AI apps.
This structure organizes the application into logical, modular components, separating configuration, core application logic, public assets, and views. Here’s a breakdown of the key elements we’ve set up and how they fit into a standard OOP architecture for PHP:
1. Public Directory as Web Root
Purpose: Keeps public assets (e.g., CSS, JS, images) and the main entry point (public/index.php) separate from sensitive files.
Security: Only the public/ directory is accessible from the web, so sensitive files in src/, config/, and vendor/ are shielded from direct access.
Front Controller: The root index.php serves as a simple front controller that forwards all requests to public/index.php.
2. Configuration and Constants (config/config.php)
Purpose: Centralizes settings, constants, and environment configurations (e.g., database credentials, base URL, paths).
Usage: Keeps configurations separate from application logic, allowing for easy changes without modifying core files.
3. Autoloader (src/Autoload.php)
Purpose: Automatically loads classes as they’re needed, following the PSR-4 standard.
Benefit: Eliminates the need to manually include files for each class. This is essential for OOP in PHP, as it dynamically loads only what’s needed.
Namespace Mapping: Maps namespaces to directories, making it easy to organize and locate classes.
4. Controllers (src/Controllers)
Purpose: Manage the logic for different application actions, such as handling authentication in AuthController.
Role in OOP: Controllers act as intermediaries between the models (data) and views (UI), following the MVC (Model-View-Controller) pattern.
Example: AuthController checks user login status and redirects to the appropriate view.
5. Models (src/Models)
Purpose: Models represent and manage data, such as interacting with the database for user information.
Benefit in OOP: Keeps data-related logic separate from other components, making the application more modular and testable.
Example: A User model would handle user-related data operations, such as fetching user information from the database.
6. Views (src/Views)
Purpose: Contains templates and presentation logic for different pages (e.g., sign-in form, protected page).
OOP MVC Role: Views separate the presentation layer from application logic, allowing controllers to load views as needed.
Example: signin.html is a template loaded by AuthController for the login page.
7. Utilities (e.g., Path.php in src/App)
Purpose: Helper classes like Path.php provide reusable methods to handle paths and URLs across the application.
Modularity: Utility classes can be used by any part of the application, supporting code reusability and maintaining cleaner logic.
8. Namespaces and PSR-4 Autoloading
Purpose: Organizes classes into namespaces that correspond to the directory structure, such as App for utilities and Controllers for application logic.
Best Practice: Following PSR-4 standards makes the project compatible with Composer and modern PHP frameworks, and it improves readability.