Search code examples
phpcssnamespacescaptcha

How PHP file gets executed from CSS


i have a simple question: I am currently making a captcha validation form for my website and I followed a tutorial where i see that to generate an image in the captcha format, style.css calls a php file like this:

.captcha-input {
    background: #FFF url(./../../captchaImageSource.php) repeat-y left center;
    padding-left: 80px;
}

When I take a look at the captchaImageSource.php file i see this

<?php
namespace MyNameSpace;
use MyNameSpace\Captcha;

session_start();

include("./Model/Captcha.php");
$captcha = new Captcha();

$captcha_code = $captcha->getCaptchaCode(6);

$captcha->setSession('captcha_code', $captcha_code);

$imageData = $captcha->createCaptchaImage($captcha_code);

$captcha->renderCaptchaImage($imageData);

?>

The captcha.php file has the related validation functions like createcaptchaImage etc. So I see the php file kind of generates an image of the captcha, but I dont understand why it gets even executed. Does CSS trigger the php file? And then does the php call the related functions by itself?


Solution

  • Yes, if you put a URL in the background rule in CSS it causes the browser to send a HTTP request to that URL - you could see it in the Network tool in your browser's Developer Tools.

    And in this case that means that the webserver will execute the php code found in the file at that URL.

    The browser will then try to use the data output from the script as an image.

    Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/background