Secure Your Web Applications with CSRF Protection

Web applications face various security threats, and one of the most prevalent ones is Cross-Site Request Forgery (CSRF). CSRF attacks occur when an attacker tricks a user’s browser into making unauthorized requests on behalf of the user. To mitigate this threat and ensure the integrity of your web application, implementing CSRF protection is essential. In this blog post, we’ll introduce you to a PHP class for CSRF protection and demonstrate how it can help safeguard your application from CSRF attacks.

Understanding CSRF Attacks

Before diving into the solution, let’s briefly understand how CSRF attacks work. Imagine a scenario where a user is logged into a web application that contains a form to transfer money. The form submission triggers a request to transfer funds from the user’s account to another account. An attacker can craft a malicious website with a hidden form that submits the transfer request when an unsuspecting user visits the site. If the user is logged into the target web application, their browser will automatically send the request, executing the unauthorized transfer without their knowledge.

Introducing the CSRFToken Class

To prevent such attacks, we introduce the CSRFToken class—a PHP implementation for generating and verifying CSRF tokens. CSRF tokens act as random, unique identifiers that are embedded in forms and requests. The CSRFToken class enables developers to generate these tokens and verify their authenticity during form submissions or sensitive actions, ensuring that requests are legitimate and not initiated by attackers.

  1. Use cryptographically secure random bytes: Instead of using random_bytes, which is already relatively secure, you can use PHP’s random_int function to generate a random integer and then convert it to a string. This ensures an even more secure generation of random tokens.
  2. Use base64_encode instead of bin2hex: base64_encode produces a shorter representation of binary data compared to bin2hex, making the tokens more efficient in terms of space.
  3. Implement token rotation: To mitigate the risk of token reuse or session fixation, it’s a good idea to rotate the CSRF token after each successful form submission or session refresh. This means that a new token is generated on each request, and the old token becomes invalid.
  4. Add CSRF protection to non-POST requests: CSRF attacks can occur on other HTTP methods (e.g., PUT, DELETE) as well. Consider extending the CSRF protection to these methods as well.
  5. Use SameSite cookies: Set the SameSite attribute of the session cookie to ‘Strict’ or ‘Lax’ to prevent CSRF attacks from other sites.
  6. Log CSRF failures: Implement logging to keep track of CSRF token validation failures. This can help in identifying potential attacks or issues.

class CSRFToken
    private $csrfTokenKey = 'csrf_token';
    private $tokenLength = 32;
    private $tokenExpiration = 600;

    public function generateToken()
        if (!isset($_SESSION)) {

        $token = base64_encode(random_bytes($this->tokenLength));
        $_SESSION[$this->csrfTokenKey] = [
            'token' => $token,
            'time' => time()

        return $token;

    public function verifyToken($token)
        if (!isset($_SESSION)) {

        if (isset($_SESSION[$this->csrfTokenKey]) && $_SESSION[$this->csrfTokenKey]['token'] === $token) {
            // Check if the token has not expired
            if (time() - $_SESSION[$this->csrfTokenKey]['time'] <= $this->tokenExpiration) {
                // Rotate the token after each successful verification
                $_SESSION[$this->csrfTokenKey]['token'] = base64_encode(random_bytes($this->tokenLength));
                return true;

        return false;

Remember that the security of your application depends on various factors, not just the CSRF token implementation. Properly securing your application involves a combination of measures, including input validation, secure session management, and following other best practices. Additionally, stay up to date with the latest security practices and be aware of potential vulnerabilities that could affect your application.

Generating CSRF Tokens

The first step in using the CSRFToken class is to generate a token for each form or sensitive action that requires protection. The class generates a random token using PHP’s cryptographically secure random_bytes function, ensuring that the token is hard to predict or tamper with.

// Instantiate the CSRFToken class
$csrf = new CSRFToken();

// Generate a CSRF token
$token = $csrf->generateToken();

Including the Token in Forms

The generated CSRF token must be included in the forms that require protection. By adding the token as a hidden input field, we ensure it is submitted with the form data and verified during processing.

<form method="post">
    <!-- Your form fields here -->
    <input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
    <button type="submit">Submit</button>

Verifying CSRF Tokens

On form submission, the CSRF token needs to be validated before performing any sensitive actions. The CSRFToken class provides a verifyToken method for this purpose.

// Instantiate the CSRFToken class
$csrf = new CSRFToken();

// Verify the CSRF token
if (isset($_POST['csrf_token']) && $csrf->verifyToken($_POST['csrf_token'])) {
    // Token is valid, proceed with the form submission
    // Handle form data and perform actions here
} else {
    // Invalid token, handle the error (e.g., show an error message or log the incident)
    // Optionally, redirect back to the form page with an error message

Enhancing Security

While the CSRFToken class provides a solid foundation for CSRF protection, you can further enhance the security of your application by considering the following measures:

  1. Token Expiration: Set an expiration time for tokens to prevent attackers from reusing old tokens. Rotate tokens regularly to minimize the risk of session fixation.
  2. SameSite Cookies: Use the SameSite attribute for cookies to restrict when cookies are sent by the browser, mitigating the risk of CSRF attacks.
  3. HTTPS: Always use HTTPS to encrypt communication between clients and the server, preventing attackers from intercepting or modifying requests.
  4. Input Validation: Implement robust input validation and sanitize user inputs to prevent other forms of attacks, such as SQL injection and XSS.


Cross-Site Request Forgery (CSRF) attacks pose a significant threat to web applications. However, with the CSRFToken class, you can fortify your application against such attacks by generating and verifying unique tokens for each form and sensitive action. By integrating this class into your application and combining it with other security best practices, you can ensure a safer and more secure user experience for your website’s visitors. Remember, proactive security measures play a crucial role in safeguarding against potential threats and preserving the trust of your users.

Stay vigilant, stay secure!