2016-10-03 96 views
1

我要编码的URL被转移解码为这个变量:
$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'];没有错误。

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

+0

SHA1是散列函数,没有解码函数...所以只有这样才能实现这个功能:创建posibilities表,并将SHA1的结果与输入字符串进行匹配,然后在该表中搜索,找出您的某些记录是否匹配'$ _GET ['url']'如果是这样,然后索引得到原始字符串 –

+0

我不知道你用变量'$ link'做什么,但是如果你使用具有解码功能的函数,那么每个人都可以通过他想要的任何东西,这将是毫无意义的 –

+0

我只是想隐藏链接http://othersite.com/file/123 – asdaqwr14241adadad214fvagsdg

回答

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'); 
+0

我怎样才能做到这一点与多个网址?我有一个很好用的URL列表,像我这样的PHP。 – asdaqwr14241adadad214fvagsdg

+0

将它们添加到'$ sources'数组中:edit –

+0

它们如何通过url访问? – asdaqwr14241adadad214fvagsdg