alasdairkeyes/redirecttoken

生成用于验证重定向的令牌的包

1.0.0 2018-12-18 10:39 UTC

This package is not auto-updated.

Last update: 2024-09-19 12:38:18 UTC


README

pipeline status coverage report Latest Version on Packagist Total Downloads

redirecttoken

PHP包,用于生成用于验证重定向的令牌

描述

通过链接到处理重定向的本地文件来跟踪您网站上外部链接的点击。

来源

<a href="https://someothersite.com/">Click me</a>
<a href="/redirect?uri=<remoteurl>&token=<validationtoken>">Click me</a>

此包提供了生成验证令牌的方法,以防止不怀好意的机器人将您的重定向用作恶意目的的重定向。

安装

  • 使用以下命令将包添加到composer中
    composer require alasdairkeyes/redirecttoken
    

示例

为要重定向的URL生成令牌

    # Or any other Class that implements Psr\Http\Message\UriInterface
use GuzzleHttp\Psr7\Uri;
use RedirectToken\Generator;

$secretKey = 'fgsdkjghsekugkserg';
$uriDestination = new Uri('https://www.google.co.uk/');

// Instantiate token generator and create token
$generator = new Generator($secretKey);
$token = $generator->generateToken($uriDestination);

$validRedirectUri = 'redirect.php?' . http_build_query([ 'uri' => (string)$uriDestination, 'token' => $token ], '', '&');

// Output
print '<a href="' . $validRedirectUri . '">Valid redirect</a>' . PHP_EOL;

验证令牌

    # Or any other Class that implements Psr\Http\Message\UriInterface
use GuzzleHttp\Psr7\Uri;
use RedirectToken\Validator;

// Parse the URI Query
parse_str($_SERVER["QUERY_STRING"], $q);

$redirectUri = new Uri($q['uri']);
$token = $q['token'];

// Instantiate validator
$validator = new Validator($secretKey);

if ($validator->validateUriToken($redirectUri, $token)) {
    header('Location: ' . $redirectUri, true, 302);
} else {
    print "Invalid request";
}

预构建

如果您已克隆了存储库,可以使用PHP内置的web服务器进行测试

cd example
php -S localhost:8080

在浏览器中转到localhost:8080

变更日志

  • 2018-12-18 :: 1.0.0 :: 移动到稳定版本
  • 2017-10-29 :: 0.1.0 :: 首次发布

网站

https://gitlab.com/alasdairkeyes/redirecttoken

作者

许可协议

在GPL V3下发布 - 请参阅附带的许可文件。