PHP code example of kingsoft / csp-builder

1. Go to this page and download the library: Download kingsoft/csp-builder 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/ */

    

kingsoft / csp-builder example snippets




use Kingsoft\Csp\{Builder, Directive, Source};

// API order: Source is the CSP directive name (e.g. script-src),
// Directive is the value (e.g. 'self').
$builder = (new Builder())
  ->addCspPolicy(Source::Script, Directive::Self)
  ->addCspPolicyNonce(Source::Script)
  ->addCspPolicyUrl(Source::Script, 'https://cdn.example.com');

$builder->setCspHeader();
$nonce = $builder->getNonce();

// addCspPolicyNonce() adds the nonce token to script-src,
// getNonce() returns the same token for use in your HTML tags.

$header = $builder->getCspHeader();

$builder = new Builder(true);