在页面之间保存 URL 参数?
我想在他们浏览时在 URL 中存储一个变量。
例如: 一个菜单,当用户选择 ?category=shopping 时,它会转到包含购物的地图,他们可以单击一个地点,它应该转到 ?category=shop&id=22。
如果他们返回菜单,则应删除“类别”,如果他们单击其他内容,例如“类别=咖啡馆”。
我对此感到非常困惑,希望得到任何帮助 - 谢谢!
I want to store a variable in the URL while they are browsing.
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.
If they return to the menu then the ?category should be removed and if they click on something else e.g ?category=cafe.
I've been really puzzled with this and would appreciate any help - thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只需要存储页面之间的状态,如标题所示,那么您可以将此信息存储在
$_SESSION
超全局数组中。在任何输出发送到浏览器之前,您可以通过运行session_start()
作为任何新页面的第一行来启动新会话。当您在下一页以相同方式启动会话时,您存储在$_SESSION
中的任何内容都将可用。如果您只对构建查询字符串(即 URL 的
?field=value&field2=value2
部分)感兴趣,如问题内容所示,那么您可能需要查看http_build_query()
函数。你的问题对我来说似乎有点模糊,你的实际目标是什么,所以我给了你两种方法。请记住,您应该使用
$_SESSION
来表示状态,并使用http_build_query()
来创建指向特定内容的动态 URL。另请记住,如果数据需要安全,则不应将其放在 URL 或用户可以修改它的任何其他位置,或者其他人可以读取它的位置(例如,在浏览器地址栏中)。此类信息需要位于$_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. You start a new session by runningsession_start()
as the very first line of any new page, before any output is sent to the browser. Anything you then store inside of$_SESSION
will be available when you start the session in the same way on the next page.If you're only interested in building a query string (i.e. 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 thehttp_build_query()
function.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, andhttp_build_query()
for creating dynamic URLs to point to specific content. 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 (e.g. in the browsers address bar). That sort of information needs to be in$_SESSION
.这是会话变量的一个很好的用途。
然后您可以保留它,直到您不再需要为止,或者他们终止会话
Thats a good use for session variables.
you can then keep it until you dont want it any more, or they terminate their session
您实际上无法在 URL 中“存储”任何内容。
如果您想使用查询字符串将一些数据从一个页面传递到另一个页面,则必须将此数据添加到查询字符串中。
“带有购物的地图”应该为每个链接添加类别。
这就是每个网络应用程序的工作方式。
会话不是正确的方法,因为网站上的每个页面都应该有它的地址,而您的类别是该地址的重要组成部分。如果您将其存储在会话中,则无法添加书签,无法将链接发送给朋友,并且搜索引擎不会索引您的商品。
You can't actually "store" anything in the 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.
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.