1. Go to this page and download the library: Download snicco/signed-url 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/ */
$secret = /* */
$hmac = new Snicco\Component\SignedUrl\HMAC($secret, 'sha256')
// This is a simple interface.
// Use one of the inbuilt storages in the #storages section or provide your own.
$storage = /* */
$signer = new \Snicco\Component\SignedUrl\UrlSigner($storage, $hmac);
// The maximum lifetime in seconds that this link should be valid for.
$lifetime_in_sec = 60;
// The maximum amount that this link should be valid for.
// After each successfully validation this amount will be decreased by 1.
$usage_limit = 1;
// optional: adding request context that must be the same in order to
// successfully validate a signed-url.
$context = ($_SERVER['REMOTE_ADDR'] ?? '') . ($_SERVER['HTTP_USER_AGENT'] ?? '');
$signed_url = $signer->sign('https://example.com/unsubscribe?user_id=12' , $lifetime_in_sec, $usage_limit, $context);
$mailer = /* */
$href = $signed_url->asString();
// $href will be something like transformers:
// https://example.com/unsubscribe?user_id=12expires=1639783661&signature=Del1cGmLB1wVET6PJieCrQ==|1MTBBGIpEGPVuGaKDjjrHDBusMNoWB15Ng5lKBSSLQY=
$mailer->send('[email protected]', "Click <a href='{{$href}}'> here <a/> to unsubscribe.")
$storage = /* */
$hmac = /* */
// Clean expired links periodically.
try {
// 0-100
$percentage = 2;
\Snicco\Component\SignedUrl\GarbageCollector::clean($storage, $percentage);
} catch (UnavailableStorage $e) {
// gc did not work for some reason. Log and continue.
error_log($e->getMessage());
}
$validator = new \Snicco\Component\SignedUrl\SignedUrlValidator($storage, $hmac);
$target = $_SERVER['REQUEST_URI'].'?'.$_SERVER['QUERY_STRING'];
try {
// optional context, has to be the same scheme used at creation.
$context = ($_SERVER['REMOTE_ADDR'] ?? '') . ($_SERVER['HTTP_USER_AGENT'] ?? '');
$validator->validate( $target, $context);
} catch (\Snicco\Component\SignedUrl\Exception\InvalidSignature $e ) {
error_log("invalid signature.");
echo "This link has expired. Please request a new one."
} catch (\Snicco\Component\SignedUrl\Exception\SignedUrlExpired $e ) {
error_log("signed url expired.");
echo "This link has expired. Please request a new one."
} catch (\Snicco\Component\SignedUrl\Exception\SignedUrlUsageExceeded $e ) {
error_log("signed url usage exceeded.");
echo "This link has expired. Please request a new one."
}
// Everything is valid.
// If the link can be used multiple times the usage is decremented automatically by 1.
echo "You have been unsubscribed."
// using an array.
$storage = new \Snicco\Component\SignedUrl\Storage\SessionStorage($_SESSION);
// using an object implementing ArrayAccess
$arr = new MyArrayAccess();
$storage = new \Snicco\Component\SignedUrl\Storage\SessionStorage($arr);
$storage = new \Snicco\Component\SignedUrl\Storage\InMemoryStorage()
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.