javascript - 如何将 URL 编码和解码为 $link = $_GET ['url' ];多变的?

标签 javascript php sha1

我想对要传输的网址进行编码并解码到此变量:
$link = $_GET['url'];

通常我使用这样的 php 文件:mysite.com/view.php?url= http://othersite.com/file/123

我想要链接 http://othersite.com/file/123 使用某种加密进行编码,然后解码到我的 php 文件 view.php 中,传递给 $link = $_GET['url']; ,无需错误。

我怎样才能一步一步地做到这一点?谢谢。

最佳答案

简单的方法:

// view.php
$sources = [
    'secretString' => 'http://othersite.com/file/123',
    'secretString2' => 'http://othersite.com/file/1234',
    'secretString3' => 'http://othersite.com/file/12345'
    //etc..
];

if(isset($_GET['url']) && isset($sources[$_GET['url']])){
    $link = $sources[$_GET['url']];
}

if(isset($link)){
    // do something
}

网址为:mysite.com/view.php?url=secretString

顺便说一句,如果你有列表,那么它可以首先按照你想要的方式完成,如下所示:

// view.php
$sources = [
    'http://othersite.com/file/123',
    'http://othersite.com/file/1234',
    'http://othersite.com/file/12345'
    //etc..
];

if(isset($_GET['url'])){
    foreach($sources as $source){
        if(sha1($source) == $_GET['url']){
            $link = $source;
            break;
        }
    }
}

if(isset($link)){
    // do something
}

//...

echo '<iframe src="mysite.com/view.php?url='.sha1('http://othersite.com/file/123').'"></iframe>';

外部文件示例:

txt 文件:

http://othersite.com/file/123
http://othersite.com/file/1234
http://othersite.com/file/12345

阅读:

$sources = explode("\n", file_get_contents('path/to/file.txt'));

或者通过 php:

// sources.php for example
return [
    'secretString' => 'http://othersite.com/file/123',
    'secretString2' => 'http://othersite.com/file/1234',
    'secretString3' => 'http://othersite.com/file/12345'
    //etc..
];

阅读:

$sources = include('sources.php');

或者只是:

// sources.php for example
$sources = [
    'secretString' => 'http://othersite.com/file/123',
    'secretString2' => 'http://othersite.com/file/1234',
    'secretString3' => 'http://othersite.com/file/12345'
    //etc..
];

// in view.php
require_once('sources.php');

关于javascript - 如何将 URL 编码和解码为 $link = $_GET ['url' ];多变的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39841119/

相关文章:

javascript - 搜索 document.innerHTML

php - 如何将 css 与 php 一起使用?

php - Magento 1 : Removing spaces from column data in collection

javascript - 地理定位 - 检查某个位置是否属于某个区域

java - 如何检查字符串是否为有效的 md5 或 sha1 校验和字符串

javascript - JS动画从零到当前量

javascript - Node.js 和 SQL 检查数据库的结果

javascript - 有没有办法通过office.js获取包含选定内容的文档

c++ - 对字节字符串进行 URL 编码?

C++哈希算法