How to get the previous pages URI including GET variables - php

I would like to be able to load the entire URI string of the previous page within my PHP, for example:
The URL that my page is reached from: /test.php?test=test
I know that $_SERVER['HTTP_REFERER'] can get the test.php but how to get back the WHOLE url, the page name, as well as the GET values?

You can't do this strictly in PHP, you can do this with Browser back and javascript functionality but this is very easy to be user manipulated. As well as this, the $_SERVER['HTTP_REFERER'] value is only supported now on some systems due to perceived security implications and can also be easily edited by the user, and so is inherently unreliable.
What you can do if you really need a complete page history (even if only of one page) is to load the pages of your site visited, into a $_SESSION variable or a database -- something that is not lost from visits to different pages.
So, in your page header you can set an array of pages, $_SESSION['pages'] and in each page you run, at the top of the page add the page values to the array.
so:
<?php
session_start();
$_SESSION['pages'][] = $_SERVER['REQUEST_URI'];
//structure: shows folder path, filename and query string
//of the URI used to reach the current page.
And to find the previous pages that where accessed, with their query strings (the GET variables), you can do this:
$reverse = array_reverse($_SESSION['pages']);
$returnURL = $reverse[1];
///previous page address: "/page.php?test=test&goat=yes"
unset($reverse);
This is because $reverse[0] will be the page you are currently on so the preceeding value is $reverse[1].
This code above is somewhat hacky but with pure PHP will give you what you want, without using Browser (cache) history or Javascript.

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.

How to pass a query string to another page on the same website?

I am using a plugin that uses unique user ids (uuid) to let visitors access some information. They receive this information by email. The whole system is based on wordpress.
This works great, if the visitor opens the direct page with a url containing the query string, for example:
https://www.example.com/user_site/?uuid=237237
As soon, as the user opens the link and navigates to another page, and then maybe to its user_site, the query string is lost. Its shown in this example:
History of opened urls:
https://www.example.com/user_site/?uuid=237237
https://www.example.com/another_site - bad, the query string is lost
https://www.example.com/user_site - no access, who even is this guy?!
How can I change this and pass (or parse) the query string while the user stays on the site? Or can I even cache it? I can use php, .htaccess javascript or jQuery to achieve this.
How the history should be:
https://www.example.com/user_site/?uuid=237237
https://www.example.com/another_site/?uuid=237237 - good, still know who you are
https://www.example.com/user_site/?uuid=237237 - great, come in
Try this : var sections = window.location.split('=')

forwarding url parameters to the next url

Using PHP (for the first time in my life) and working in a CMS environment without access to the back-end PHP pages or code, I created a form on one page that places four parameters into the url of the next page to which the form send its data.
Here is the form page:
http://CMSDetroit.org/480
Here is the url the form generates:
http://www.chambermusicdetroit.org/422?School=formdata&Grade=formdata&Teacherformdata=&Handle=formdata
I need to collect the parameter information from the url and pass it on to the url of the next page after this one, either by putting a command into every link on the page (422), or through some other more efficient method.
I've tried all sorts of things and keep coming up dry...help!?!
For PHP see $_SERVER and more specifically QUERY_STRING
Then with that I'm not sure how you are invoking the next page? maybe with header?
header('Location: http://www.example.com/?' . $_SERVER['QUERY_STRING');
exit;
Point being it is just one large string. If you don't know already you use $_GET and key to extract the individual values or if you want you can use explode on the QUERY STRING

Creating "came-from" navigation - PHP

I was wondering, how is it possible to get example the last X number of pages a user came from on my site?
I am creating a navigation, so the user easily can see the X number of previuos pages he visited on my site only.
I just don't know how to do this. Is there any function in PHP to obtain this?
Thanks in advance.
I'm assuming you mean the last X pages on your site only, and that you know how to use sessions and arrays.
In this case you could initialize a session for the user, then every time the user visits a page, append the URL or some identified of that page to the array. If there's more than X pages in the array, additionally remove the first one.
Then you could just get the contents of the array, parse them into a bunch of links and show them on your site.
In case you meant the pages before entering your site, you can only get the latest one via the referer header. Some browsers might be configured to not send the referer header, however, so there really isn't a way of accomplishing this properly.
<?php
session_start();
/*
Put here any logic that could result in a redirect, to avoid useless records
...
*/
$_SESSION['history'][] = $_SERVER['REQUEST_URI'];
$_SESSION['history'] is now an indexed array with all of the user's history on your website.

Holding a URL parameter between pages?

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.

Categories