【问题标题】:Get an activation key string from URI从 URI 获取激活密钥字符串
【发布时间】:2021-02-08 10:22:25
【问题描述】:

我有一个这种格式的网址:

https://www.example.com/activate/9ajebaidblahdeblahblah2020

我的设置是 /activate/index.php

如何使用 /activate 文件夹中的 index.php 文件解析 /9ajebaidblahdeblahblah2020?

我试过了……

$current_url = $_SERVER['REQUEST_URI'];
echo $current_url;

//OR

var_dump($_SERVER['REQUEST_URI']);

我想做的是(目的)...

$current_url = explode("/", $_SERVER['REQUEST_URI']);
//echo $current_url[2];

提前感谢您的帮助。

【问题讨论】:

  • 使用parse_url()。希望这对你有用
  • 在我的示例中,与 parse_url 相同,它返回 404。假设我无法设置 URL(在我的数据库中)。
  • 如果你使用的是 Apache,你将不得不添加一些 .htaccess 的 URL 重写(类似 nginx)
  • 是的,尽管如此,但我希望自 7.x 以来可能会有一些新的魔法方法。也许我至少需要使用 /activate/index.php/9ajebaidblahdeblahblah2020,我可以在数据库中进行更改。
  • 我决定保留我的原始 URL,但包含 index.php 因为它除了在电子邮件链接中从未见过,该文件只是一个处理器,然后我可以分解结果并执行比如:$authKey = explode("/", $_SERVER['REQUEST_URI']);回声 $authKey[3]; /activate/index.php/9ajebaidblahdeblahblah2020

标签: php parsing server-side


【解决方案1】:

使用以下代码在您的activate 项目文件夹中添加.htaccess 文件:

RewriteEngine On

RewriteRule ^/?activate/(.+)$ /activate/index.php?val=$1 [NC,L,P]

演示: https://htaccess.madewithlove.be?share=aa64900e-ba78-4f17-9369-326f4384dd47

稍后在您的 index.php 文件中,您可以使用 as:

<?php

echo $_GET['val'];

【讨论】:

  • 嘿,这是一个很棒的工具,感谢您指出。
【解决方案2】:

您的服务器上没有名为 9ajebaidblahdeblahblah2020 的页面。

您可以使用 htaccess (https://httpd.apache.org/docs/current/howto/htaccess.html) 将所有 URL 重定向到 index.php。

在此处查看更多详细信息: Redirect all to index.php using htaccess

【讨论】:

  • 我的初步结论也是如此,因此我知道我至少需要 /index.php/ 。
【解决方案3】:
<php

$url = $_SERVER['REQUEST_URI'];

$route_path = parse_url($url, PHP_URL_PATH);

$segments = array_filter(explode("/", $route_path));

print_r($segments);

你会得到以下结果,

数组( [1] => 激活, [2] => 9ajebaidblahdeblahblah2020 )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多