我想知道页面加载时是否可以将变量 append 到地址栏中的 url。
演示:http://communityimpact.com/discussions/
当前页面已地理定位,如果类别 = Central Austin...希望网址加载为 http://communityimpact.com/discussions/central-austin
<?php echo $my_category; ?> = Central Austin
<?php echo $my_category_slug; ?> = central-austin
有什么建议吗?
最佳答案
实际上,如果您需要将变量 append 到网址,则 header 是最好的选择。
示例:
$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 变量会更有意义。我看不到在不重定向的情况下将变量 append 到 URL 的任何可能性。 Url 是地址,如果更改 Url,则必须更改页面。但是 session 变量会一直存在,直到它们离开您的站点为止。并可以保存该信息希望这会有所帮助,我不确定您的目的是什么。
$_SESSION["category"] = "central-austin";
但是你必须有
session_start();
在页面的开头。
关于PHP 将变量 append 到地址栏中的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340452/