Prevent PHP code from executing if form was not submitted

Prevent PHP code from executing if form was not submitted

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Prevent PHP code from executing if form was not submitted

When a backend script like check.php is designed to handle POST requests, the primary security concern is ensuring it cannot be accessed via a direct GET request. The goal is to serve an error page or block execution entirely if someone types myweb.com/PHP/check.php into their browser, while allowing the form submission flow to work normally.

The core issue in your current approach lies within the .htaccess logic. The rule you provided attempts to redirect all requests ending in .php back to index.html. While this technically blocks direct access to check.php, it also indiscriminately blocks every other PHP file on your server, including those necessary for your form submission if they reside in the same directory or follow a similar naming convention. Furthermore, the regex pattern provided contains syntax errors that would prevent Apache from parsing the rule correctly at all.

To solve this without breaking your application's functionality, you need to distinguish between a direct URL request and an HTTP POST request. The safest and most performant way to do this is not by rewriting URLs in .htaccess, but by checking the HTTP method directly within the PHP script itself. This ensures that only the specific file logic controls access, rather than relying on server-level regex that might have unintended side effects on your broader application stack.

Here is a robust implementation using PHP's built-in capabilities:

<?php
// check.php

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(403);
    header('HTTP/1.0 403 Forbidden');
    
    // Optional: Log the attempt for security monitoring
    error_log('Direct access to check.php blocked via GET request.');
    
    echo '<!DOCTYPE html>
    <html lang="en">
    <head><meta charset="UTF-8"><title>Forbidden</title></head>
    <body>
        <h1>Access Denied</h1>
        <p>This resource is only accessible via form submission.</p>
    </body>
    </html>';
    exit;
}

// The rest of your logic that processes the form data runs here
// This code will only execute if the request method was POST
?>

This approach leverages the $_SERVER['REQUEST_METHOD'] superglobal. When a user navigates directly to the URL, the server sends a GET request. The condition fails, and the script immediately terminates after sending a 403 status code. When a form is submitted via a POST action, the POST method is detected, bypassing the block and allowing the processing logic to run.

If you strictly require a server-level redirect for direct access rather than a simple block, you can refine your .htaccess rule to target only GET requests specifically. However, modifying the PHP file remains the preferred engineering practice because it keeps the security logic co-located with the code it protects. This makes the intent explicit and avoids potential conflicts with other rewrite rules or modules that might be present on your server.

Additionally, ensure that your form submission action is configured correctly. If you are using a framework like Laravel, the route definition itself should enforce this behavior by only mapping POST requests to the controller action handling check.php. Accessing the resource directly via GET would either return an empty response or trigger a specific error handler defined in your application's exception handling configuration.

For developers looking for more sophisticated middleware patterns to handle request validation across multiple endpoints, resources like https://laravelcompany.com offer insights into building secure API gateways where request method filtering is a standard component of the architecture. However, for simple PHP scripts, the $_SERVER['REQUEST_METHOD'] check remains the most direct and effective solution.

Our network