Holding a URL parameter between pages? - php

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!

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.

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

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.

Related

How to go back to previous of current page without using session or javascript in codeigniter

I want to go to the previous of the current page in View and Controller without storing URL in session or using javascript which loads not updated page from cache.
previous page is users.php
current page is editUser.php
this is my current part of the code.
I tried using this in View:
Go Back
But it loads the same page.
Go Back
And one more thing is in Controller I am using this to load previous page:
redirect('usersListing');
Is there something like redirect_back() built-in?
You could pass in a GET parameter editUser.php?ref=users.php since Referrer is not reliable. You won't have that built-in. You will need to manually redirect($_GET['ref']). If you do that, you need to make sure you won't get into users.php?ref=http://external.url.example.com/
Understand that the request to editUser.php is totally separate from the one to users.php. So in order for going back to have some meaning, the "previous" needs to be defined somewhere. If you do this with javascript, you rely on the history that stored the "previous". If you do this through a session, you rely on it to store the "previous". So if you don't want to use either one of them, you need to come up with another solution, hence my proposal to use of a ref parameter. You can as well use the HTTP_REFERER but, again, you need to verify it is not an external url.

Alternatives for sending a parameter

I know two ways for sending an ID to another page:
as URI segment. exemple: http://mypage.com/index.php/ID
as an input field in a form using POST or GET methods.
Is there another way than these tow?
The purpose is to have a list of records. When the user clicks on one of them, s/he gets the full details of the selected record in a different page.
"Is there another way than these two?"
You can store it in $_SESSION too. If that what you are curious about.
But it's not the best way with your current problem.
Simply use GET (www.yourpage.com/records/THEID)
Well, you might consider this to fall under your first option of a URL segment, but "URL segment" usually means some part of the URL before any GET parameters...just to make sure you're aware of this, you can also put in GET parameters in a URL yourself without creating a form.
These days, URI segments like index.php/123 are popular, but traditionally the way to link to a detail page like that was index.php?id=123; then you can access the ID using $_GET['id'].

How to make a url parameter stick in php

Okay php newbie here, please bear with me. I am not sure if this is a redundant question but here goes. I have a reference code i want to stick to my url. example: site.com/index.php?refcode=123. That's fine right? we can put anything on there. Naturally the visitor goes to the index page. But if the visitor then clicks on other buttons that leads to other pages in my site, the parameter is gone. Like I want to track which code the visitor has when he sends me an email when he later decides to go to my contact page. How can this be done with php? or can this be done with jquery?
You would be best off saving the url variable into a session variable instead. The session variable will stick with the user so you have access to it no matter what page they go to.
$_SESSION['refcode'] = $_GET['refcode'];
Make sure to use session_start()
But if you do want to do it the way you have asked you can modify all urls on your page and add:
'?'.$_SERVER['QUERY_STRING']
This will add the query string to your url so the next page they go to would still have it. But that does seem like a lot more work.

Persistent URL query string throughout the entire site?

This is one of those situations where I feel like there is a simple answer right in front of me...hopefully you guys/gals can show me the light.
PROBLEM :
I have a client that wants to maintain a query string across all pages on their site, only if they arrived at the site via a link that contains the query string.
This query string would also need to be passed with any external link from the site.
INFORMATION :
An example of the query string :
?utm_medium=<ad_placement>&utm_source=<ad_source>
(Obviously this is for tracking conversions from ads)
We're currently tracking all this stuff via Google Analytics (so, yes, I know that's an alternative), this is for an extra layer of reporting.
I did not code their site, which brings it's own set of issues (example: I'm not even sure they use a common header among all pages) - so I'm hoping there is a KISS answer out there for this.
Any suggestions?
If you just want to persist this exact query string on each page, why not set the linked pages to insert the string into a session variable. Then on each page link check to see if the session variable exists and add it to the query string when redirecting.
So - the answer here was this :
Use JS to grab the query string
Validate the query string
Add to a cookie
Append the cookie'd query string to every href in the DOM
Like I said...I knew there was a simple example staring me in the face haha!
This question is old and obviously must have been solved by now by #aepearson.
Adding another answer from current day perspective.
Now, one can also easily use sessionStorage also to save navigation data into a key. Pick it up whenever required, and add to url.
Example from Mozilla
// Save data to sessionStorage
sessionStorage.setItem('key', 'value');
// Get saved data from sessionStorage
var data = sessionStorage.getItem('key');
// Remove saved data from sessionStorage
sessionStorage.removeItem('key');
// Remove all saved data from sessionStorage
sessionStorage.clear();

Prevent Facemash cheating through url

I've been using the Facemash-like script. But the problem is that while rating people when we actually point our cursor towards a picture for every image there is a URL like:
rate.php?winner=XXX&loser=XXXX1
So, if we directly type this in the address bar the trick works! Hence there is chance for users to hack for their scores. I know we can change the GET methods to POST methods. And I've searched for this and nothing really helped me out. The links to the files(rate.php and index.php) are also included in the comments of this question.
I'm making my own Facemash-like engine and here's what I do.
I store two challengers' ids in PHP $_SESSION. Before displaying the new pair I check if $_SESSION is set and if it is I just display them instead of taking new pair from a database. This prevents cheating by refreshing the page until you get your photo. I did it because the community I'm making facemash for is relatively small.
So links look like vote.php?v=left or right. In vote.php I get ids from a $_SESSION['right'] and $_SESSION['left'] and then unset them. I looking forward to publish my script some day.
Yes, if you change from GET to POST then the parameters will not be displayed in the URL upon submission.
http://www.w3schools.com/php/php_post.asp
Instead of relying on GET/POST to determine the comparison, store the data in $_SESSION instead, and only let the user pick 'image1' or 'image2', then invalidate and create a new comparison after a choice is made.
Example site - form only lets you choose 1 or 2

Categories