minte9
LearnRemember




Simpl Web App

Symfony is a reusable set of PHP components. You can use Simfony ready to use framework, or you can create your own framework. Let's start with the simplest web application we can think of in PHP.
 
/*
    app/01.php

    cd github/php-pages/main/framework/app/
    php -S localhost:8000

    http://localhost:8000/01.php?name=John
        Hello John
    http://localhost:8000/01.php
        Warning: Undefined array key "name" 
*/

ini_set("display_errors", 1);

$name = $_GET['name'];
printf("Hello %s", $name);

Warning Fixing

Fixing the undefined php warning.
 
/*
    app/02.php - Fix Warning
    
    http://localhost:8000/02.php
        Output: Hello World
*/

ini_set("display_errors", 1);

$name = $_GET['name'] ?? "Wolrd"; // Fix Warning
printf("Hello %s", $name);

XSS Vulnerability

Fixing XSS vulnerability - Cross Site Scripting
 
/*
    app/03.php - Fix XSS

    http://localhost:8000/02.php?name=<script>alert('Hack!');</script>
        XSS attempt - working
    http://localhost:8000/03.php?name=<script>alert('Hack!');</script>
        XSS attempt - fixed
*/

ini_set("display_errors", 1);
header('Content-Type: text/html; charset=utf-8');

$name = $_GET['name'] ?? "World";
printf("Hello %s", htmlspecialchars($name, ENT_QUOTES, 'UTF-8'));

Unit Test

This simple code is not that simple anymore and is hard to test.
T 
/*
    test.php - Unit Tests

    cd ../test/framework/app/
    composer require --dev phpunit/phpunit

    vendor/bin/phpunit test.php
        OK (1 test, 1 assertion)

*/

require __DIR__ . "/vendor/autoload.php";

use PHPUnit\Framework\TestCase;

final class Test extends TestCase
{
    public function test_Hello()
    {
        ob_start(); // not naturaly and ugly

        $_GET['name'] = "Fabian";

        include "../../../main/framework/app/01.php";

        $content = ob_get_clean();
        $this->assertEquals("Hello Fabian", $content);
    }
}



  Last update: 128 days ago