PHP code example of dsentker / url-signature

1. Go to this page and download the library: Download dsentker/url-signature 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/ */

    

dsentker / url-signature example snippets



UrlSignature\Builder;
use UrlSignature\HashConfiguration;

// Secret is loaded from a configuration outside of the library
$configuration = new HashConfiguration($_ENV['SECRET']);
$builder = new Builder($configuration);
$url = $builder->signUrl('https://example.com/foo?bar=42'); // http://example.com?foo?bar=42&_signature=90b7ac1...


use UrlSignature\Validator;
$validator = new Validator($configuration); // Use the same $configuration here
var_dump($validator->isValid('https://example.com/foo?bar=42&_signature=90b7ac1...')); // returns true or false, depending on the signature

// If you want to catch Exceptions to determine the cause of an invalid URL, use Validator::verify() instead
$validator->verify('http://example.com?foo=this+is+a+test&_signature=90b7ac1...'); // Returns true or a \UrlSignature\Exception\ValidationException.


$builder = new Builder($configuration);
$url = $builder->signUrl('http://example.com/foo?bar=42', '+10 minutes'); // https://example.com?foo=bar&_expires=1521661473&_signature=009e2d70...


$querySignatureName = '_hash';
$queryExpiresName = 'ttl';
$configuration = new \UrlSignature\HashConfiguration('my-secret-key', $querySignatureName, $queryExpiresName);
$hashedUrl = (new Builder($configuration))->signUrl('https://example.com/?id=1234', new \DateTime('MONDAY NEXT WEEK'));
var_dump($hashedUrl); // https://example.com/?id=1234&ttl=123456789&_hash=009e2d70...


use UrlSignature\HashConfiguration;
$config = new HashConfiguration('secret');
// Complete example: Use *ALL* parts of the URL for hashing
$config->setHashMask(
            HashConfiguration::FLAG_HASH_SCHEME
            | HashConfiguration::FLAG_HASH_HOST
            | HashConfiguration::FLAG_HASH_PORT
            | HashConfiguration::FLAG_HASH_PATH
            | HashConfiguration::FLAG_HASH_QUERY
            | HashConfiguration::FLAG_HASH_FRAGMENT
        );