如何编码和解码URL到$ link = $ _GET ['url'];变量?

时间:2016-10-03 22:03:34

标签: 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'];而不会出现错误

我该怎么做,一步一步走?感谢。

1 个答案:

答案 0 :(得分:0)

简单的方法:

// 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');