【问题标题】:How to encode and decode URL to $link = $_GET['url']; variable?如何将 URL 编码和解码为 $link = $_GET['url'];多变的?
【发布时间】:2016-10-03 22:03:34
【问题描述】:

我想对要传输的 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'];

我该如何一步一步地做到这一点?谢谢。

【问题讨论】:

  • SHA1 是散列函数,没有解码函数......所以你可以实现这一点的唯一方法是创建可能性表并将 SHA1 的结果与输入字符串匹配,然后在该表中搜索是否有一些你的记录匹配$_GET['url'] 如果是这样,那么通过索引获取原始字符串
  • 我不知道你对变量 $link 做了什么,但如果你使用具有解码功能的函数,那么每个人都可以传递他想要的任何东西,这将毫无意义
  • 我只想隐藏链接othersite.com/file/123
  • 如果你使用 encryptiondecryption 和只知道 mysite.comothersite.com 的密钥,那么它可以按你的意愿工作
  • @MarekJanoud 教程?抱歉给您带来不便,我是从php开始的。

标签: javascript php sha1


【解决方案1】:

简单的方法:

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

【讨论】:

  • 如何使用多个网址进行此操作?我有一个很棒的 url 列表,可以与我的 php 一起使用,比如这个。
  • 如何通过 url 访问它们?
  • 另一个问题,我是否需要删除 $link = $_GET['url'];来自我的 php?
  • 是的值在我发布的代码中设置为$link 变量
  • 你真是个疯子!太疯狂了!它对我有用,非常感谢我的灵魂兄弟!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多