PHP code example of spatie / url-signer

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

    

spatie / url-signer example snippets


$urlSigner = new Sha256UrlSigner('randomkey');

$urlSigner->sign('https://myapp.com', 30);

// => The generated url will be valid for 30 seconds

// returns `true` if valid, `false` if not
$urlSigner->validate('https://myapp.com/?expires=xxxx&signature=xxxx');

use Spatie\UrlSigner\Sha256UrlSigner;

$urlSigner = new Sha256UrlSigner('mysecretkey');

$expirationDate = (new DateTime())->modify('10 days');

$urlSigner->sign('https://myapp.com', $expirationDate);

// => The generated url will be valid for 10 days

$urlSigner->sign('https://myapp.com', 30);

// => The generated URL will be valid for 30 seconds

$urlSigner->validate('https://myapp.com/?expires=1439223344&signature=a479abde194d111022a6831edbda29b14e7bdb760438a8a0be2556cd1a6c23fa');

// => true

$urlSigner->validate('https://myapp.com/?expires=1439223344&signature=a479abde194d111022a6831edbda-INVALID-29b14e7bdb760438a8a0be2556cd1a6c23fa');

// => false