PHP code example of kronos / csp
1. Go to this page and download the library: Download kronos/csp 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/ */
kronos / csp example snippets
$factory = new \Kronos\ContentSecurityPolicy\Factory();
$builder = $factory->createContentSecurityPolicyBuilder();
$builder->setDefaultSrc(\Kronos\ContentSecurityPolicy\Policy::FETCH_NONE);
$policy = $builder->getContentSecurityPolicy();
header($policy->getHeader());
use Kronos\ContentSecurityPolicy\Factory;
use Kronos\ContentSecurityPolicy\Policy;
use Kronos\ContentSecurityPolicy\PolicyDirectorInterface;
class Director implements PolicyDirectorInterface
{
/** @var Factory */
protected $factory;
...
/**
* @param Factory $factory
* ...
*/
public function __construct(Factory $factory, ...)
{
$this->factory = $factory;
...
}
public function buildContentSecurityPolicy()
{
$builder = $this->factory->createContentSecurityPolicyBuilder();
$builder->setDefaultSrc([Policy::FETCH_NONE]);
$builder->setObjectSrc([Policy::FETCH_NONE]);
$builder->setStyleSrc([
'mysite.example/css',
'fonts.googleapis.com',
'fonts.gstatic.com',
Policy::FETCH_UNSAFE_INLINE
]);
$builder->setFontSrc([
'mysite.example/fonts',
'fonts.googleapis.com',
Policy::FETCH_UNSAFE_INLINE
]);
$builder->setFrameAncestors([Policy::FETCH_NONE]);
$builder->setReportUri('mysite.example/api/csp-report/');
$builder->setReportOnly();
return $builder->getContentSecurityPolicy();
}
}
$factory = new \Kronos\ContentSecurityPolicy\Factory();
$director = new Director($factory);
$policy = $director->buildContentSecurityPolicy();
header($policy->getHeader());