PHP code example of bretto36 / csp-reporting

1. Go to this page and download the library: Download bretto36/csp-reporting library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bretto36 / csp-reporting example snippets


   'route' => [
       'prefix'     => 'csp-reporting', // Alter this to a different prefix
       'middleware' => ['web'], // Change the middleware you want to use
   ],
   

    'report-uri' => 'https://www.yourdomain.com/csp-reporting/report',
    

    use Bretto36\CspReporting\Events\CspViolationReportReceived;
    use Illuminate\Support\Facades\Event;

    Event::listen(CspViolationReportReceived::class, function (CspViolationReportReceived $event) {
        if ($event->violationReport->data->blocked_uri === 'https://example.com') {
            $event->shouldReport = false;
        }
    });
    

    

    namespace App\Listeners;
        
    use Bretto36\CspReporting\Event\CspViolationReportReceived;
        
    class CspViolationReportReceivedListener
    {
        /**
         * Handle the event.
         */
        public function handle(CspViolationReportReceived $event): void
        {
            // Apply logic here
            $event->shouldReport = false;
        }
    }
    

return [
    'enabled' => env('CSP_REPORTING_ENABLED', false),
    'uri' => env('CSP_REPORTING_URI', '/csp-report'),
    'route' => [
        'prefix'     => 'csp-reporting',
        'middleware' => ['web'],
    ],
];
sh
    php artisan vendor:publish --provider="Bretto36\CspReporting\ServiceProvider"
    
sh
    php artisan make:listener CspViolationReportReceivedListener