【问题标题】:PHP append variable to url in address barPHP将变量附加到地址栏中的url
【发布时间】:2015-11-27 05:05:13
【问题描述】:

我想知道是否可以在页面加载时将变量附加到地址栏中的 url。

演示:http://communityimpact.com/discussions/

当前页面已定位,如果类别 = 奥斯汀市中心...希望 URL 加载为 http://communityimpact.com/discussions/central-austin

<?php echo $my_category; ?> = Central Austin
<?php echo $my_category_slug; ?> = central-austin

有什么建议吗?

【问题讨论】:

    标签: php variables url append


    【解决方案1】:

    如果您需要将变量附加到 url,实际上标题是您最好的选择。

    例子:

    $my_category = "Central Austin";
    $my_category_grub = "central-austin";
    
    
        header("location: community-impact/discussions/$my_category_grub");
    //or append a var like this:
    header(" location: community-impact/discussions/?location=$my_category_grub");
    //then you could handle the request on the new page
    

    但是这两种方法都会重定向你。

    如果你想使用 $_SESSION 变量会更有意义。我看不到在不重定向的情况下将变量附加到 URL 的任何可能性。 Url 是地址,如果您更改 Url,那么您必须更改页面。但是会话变量会一直存在,直到它们离开您的站点。并且可以保存该信息,希望对您有所帮助,我不确定您的目的是什么。

    $_SESSION["category"] = "central-austin";
    

    但你必须有

    session_start();
    

    在页面的开头。

    【讨论】:

    • 需要在这 2 个变量的值周围加上引号 :)
    • 是的,哈哈。对不起,错别字。修复。
    【解决方案2】:

    尝试使用带有headers 的重定向,例如

    header("Location: http://communityimpact.com/discussions/central-austin");
    

    【讨论】:

    • 这并没有真正附加 URL - 它强制浏览器到不同的页面。
    • @HPierce ...包含值,因此效果很好^^
    【解决方案3】:

    您可能应该使用 JavaScript,这里有两个选项:

    • 带有重定向

      // similar behavior as an HTTP redirect
      window.location.replace("http://communityimpact.com/discussions/central-austin");
      
      // similar behavior as clicking on a link
      window.location.href = "http://communityimpact.com/discussions/central-austin";
      
    • Without redirection

    【讨论】:

      猜你喜欢
      • 2014-02-25
      • 2018-06-27
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2016-07-14
      • 2019-09-08
      • 1970-01-01
      • 2013-08-24
      相关资源
      最近更新 更多