Reputation: 17
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!
Upvotes: 0
Views: 160
Reputation: 157872
I want to store a variable in the URL while they are browsing.
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.
Upvotes: 0
Reputation:
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 running session_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 the http_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, and http_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
.
Upvotes: 1
Reputation: 17868
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
Upvotes: 1