简体   繁体   English

在页面之间保存URL参数?

[英]Holding a URL parameter between pages?

I want to store a variable in the URL while they are browsing. 我想在浏览时在URL中存储变量。

For example: A menu, when the user selects ?category=shopping it goes to a map with shopping and they can click on a place and it should go to ?category=shop&id=22. 例如:菜单,当用户选择?category = shopping时,它会转到带购物的地图,他们可以点击一个地方,它应该转到?category = shop&id = 22。

If they return to the menu then the ?category should be removed and if they click on something else eg ?category=cafe. 如果他们返回菜单,则应删除?类别,如果他们点击其他内容,例如?category = cafe。

I've been really puzzled with this and would appreciate any help - thanks! 我一直对此感到困惑,并感谢任何帮助 - 谢谢!

Thats a good use for session variables. 这是一个很好用于会话变量。

$_SESSION["category"]="stuff";

you can then keep it until you dont want it any more, or they terminate their session 你可以保留它直到你不再需要它,或者他们终止他们的会话

If you just need to store state between pages, as your title suggests, then you can store this information inside the $_SESSION superglobal array. 如果您只需要在页面之间存储状态,如标题所示,那么您可以将此信息存储在$_SESSION超全局数组中。 You start a new session by running session_start() as the very first line of any new page, before any output is sent to the browser. 在将任何输出发送到浏览器之前,通过将session_start()作为任何新页面的第一行运行来启动新会话。 Anything you then store inside of $_SESSION will be available when you start the session in the same way on the next page. 当您在下一页以相同方式启动会话时,您在$_SESSION存储的任何内容都将可用。

If you're only interested in building a query string (ie the ?field=value&field2=value2 portion of the URL), as the content of your question indicates, then you might want to take a look at the http_build_query() function. 如果您只对构建查询字符串感兴趣(即URL的?field=value&field2=value2部分),正如您的问题内容所示,那么您可能需要查看http_build_query()函数。

Your question seems a little ambiguous to me as to what your actual goal is for this, so I gave you both approaches. 关于你的实际目标是什么,你的问题对我来说似乎有点模棱两可,所以我给了你两种方法。 Just remember that you should use $_SESSION for state, and http_build_query() for creating dynamic URLs to point to specific content. 请记住,您应该使用$_SESSION表示状态,使用http_build_query()来创建指向特定内容的动态URL。 Also remember that if the data needs to be secure, then you shouldn't put it in the URL or anywhere else the user could modify it, or where others could read it (eg in the browsers address bar). 还要记住,如果数据需要是安全的,那么你不应该把它放在URL或者用户可以修改它的任何地方,或者其他人可以读取它的地方(例如在浏览器地址栏中)。 That sort of information needs to be in $_SESSION . 这类信息需要在$_SESSION

I want to store a variable in the URL while they are browsing. 我想在浏览时在URL中存储变量。

You can't actually "store" anything in the URL. 您实际上无法在URL中“存储”任何内容。
If you want to pass some data from one page to another using query string, you have to add this data to the query string. 如果要使用查询字符串将某些数据从一个页面传递到另一个页面,则必须将此数据添加到查询字符串中。

"A map with shopping" should add category to the every it's link. “购物地图”应该为每个链接添加类别。
That's the way every web application works. 这就是每个Web应用程序的工作方式。

Session is not the way to go, because every page on the site should have it's address , and your category being important part of this address. 会话不是要走的路,因为网站上的每个页面都应该有它的地址 ,并且您的类别是该地址的重要组成部分。 If you store it in the session, no bookmark can be added, no link to be sent to a friend and no search engine will index your goods. 如果您将其存储在会话中,则不能添加任何书签,也不会将任何链接发送给朋友,也不会有任何搜索引擎将您的商品编入索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于在页面之间保存变量的会话 - Session for holding variables between pages 配置文件页面-URL参数 - Profile Pages - URL Parameter 如何在此代码的页面之间传递参数? - How to pass parameter between pages in this code? 在页面之间重定向时,URL末尾有奇怪的#号 - Weird # at end of URL when redirecting between pages htaccess-使用原始URL作为参数重定向无扩展名的页面 - htaccess - Redirecting pages without extensions using the original URL as parameter Symfony:executeNew / executeCreate之间的URL参数丢失 - Symfony : loss of an URL parameter between executeNew / executeCreate 我正在提交一个表单,其中包含一个包含URL字符串的字段,该字符串丢失了表单提交和输入MYSQL数据库之间的格式 - I'm submitting a form with a field holding a URL string which loses formatting between form submission and entry into MYSQL database 如何使用onclick但不使用URl在页面之间传输信息 - How to transfer information between pages using onclick but without using the URl 关于使用URL在多个页面之间传递变量 - About passing variable between multiple pages using URL PHP:通过 URL 参数在两个字符串之间进行 XOR 的 eval() - PHP: eval() of a XOR between two strings via the URL parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM