如何获取网址的最后一部分并建立重定向?

时间:2018-11-09 04:30:14

标签: php

提交帖子时,我将重定向到一个网址:

https://example.com/example/create/?usp_success=2&post_id=127065

现在我需要抓住127065并使用它建立重定向:

    $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    // testing this but I don't how to get the last bit of the url
    // $newURL = "$actual_link/?p="
    header('Location: '.$newURL);

我要重定向到的最终URL是:

https://example.com/example/create/?p=127065

更新

如果我愿意(按照评论中的建议)

        $id = $_GET['post_id'];
        $newURL = get_home_url()."/".$id;
        header('Location: '.$newURL);

我得到:

  

警告:无法修改标题信息-..已经发送的标题   在第42行

第42行是:

header('Location: '.$newURL);

3 个答案:

答案 0 :(得分:0)

使用parse_url()parse_str()

$url="https://example.com/example/create/?usp_success=2&post_id=127065";
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['post_id'];

答案 1 :(得分:0)

您需要结合使用以下两个功能:var getValue = value => value === 12 ? { width: 0.08 } : false parse_url()

parse_str()

$actualLink = 'https://example.com/example/create/?usp_success=2&post_id=127065'; $queryArgs = []; parse_str(parse_url($actualLink, PHP_URL_QUERY), $queryArgs); if ($postID = $queryArgs['post_id']) { $newURL = sprintf("%s/?p=%s", $actual_link, $postID); header('Location: ' . $newURL); } 将包含数组:

$queryArgs

您可以使用Array ( [usp_success] => 2 [post_id] => 127065 ) 来获得post_id的值

要纠正此错误:

  

警告:无法修改标头信息-第42行上的..已发送的标头

在调用$queryArgs['post_id']函数之前,您需要确保没有任何输出。

答案 2 :(得分:0)

$id = $_GET['post_id'];
    header('Location: https://example.com/example/create/?p='.$id);
相关问题