我想对要传输的网址进行编码并解码到此变量:
$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/