PHP Classes

File: examples/example5.php

Recommend this page to a friend!
  Classes of Tom Postma   PHP Content Security Policy generator   examples/example5.php   Download  
File: examples/example5.php
Role: Example script
Content type: text/plain
Description: Example: how to use a nonce to allow inline CSS in page.
Class: PHP Content Security Policy generator
Generate CSP headers to prevent security attacks
Author: By
Last change: Example: how to use a nonce to allow inline CSS in page.
Date: 9 years ago
Size: 969 bytes
 

Contents

Class file image Download
<?php
require_once('../CSPGenerator.php');

// Add a style-src nonce.
CSPGenerator::getInstance()->setStylesrcNonce();


// Set the headers, always call this method before any content output.
CSPGenerator::getInstance()->Parse();
// Start content output.
?><!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>example5 - allow inline css with nonce</title>
        <!-- Because of valid nonce the following css should be applied. -->
        <style type="text/css" nonce="<?php echo CSPGenerator::getInstance()->getStylesrcNonce(); ?>">
.hidetext {
    visibility: hidden;
}
.bigtext {
    font-size: 16pt;
}
        </style>
        <!-- Because of invalid nonce the following css should not be applied. -->
        <style type="text/css" nonce="deliberately_invalid_nonce">
.hidetext {
    visibility: visible !important;
    color: #FF2121;
    font-size: 16pt;
}
        </style>
    </head>
    <body>
        <span class="hidetext">not</span>
        <span class="bigtext">working</span>
    </body>
</html>