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.
Related
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.
I have run into a problem:
I have a page that will be processed many times a week by my customers and each time it will have a different identifier
example
compshop.php?Shopid=2252&sub=s
compshop.php?Shopid=2520&sub=se
compshop.php?Shopid=3152&sub=n
etc....
problem is if possible I don't want all of these to save to the persons history so every time they start typing my url these show as the primary result instead of my home page.
is there a way to stop this
I thought the cache meta tag would deal with this but it appears the cache tag is only for SE indexing
any ideas?
If you add a randomly generated parameter, it's extremely unlikely any page will be visited more than once, but your page can still be accessed by URL alone.
For example,
compshop.php?Shopid=2252&sub=s&t=69396872
compshop.php?Shopid=2520&sub=se&t=17094891
compshop.php?Shopid=3152&sub=n&t=79125863
It's not hiding the pages from the history, but it is mostly ensuring that the page is only entered into the history once, so your homepage should still show up first.
I think you're about out of luck unless you can send the parameters over POST instead of GET. As #PWHite just mentioned you could append a random value to the parameters as well to make sure that even the same Shopid/sub would show as "different" pages in the history, thus putting your home page at the top of the list in terms of # of visits.
I have a PHP page and I want to redirect it first to a page (eg google.com) then to another page (eg bing.com).
To do this I'm using the following:
header('Location: http://google.com');
header('Location: http://bing.com');
The problem is that the script stops at the first 'header'.
Is there a way to make this?
I'm afraid the behavior as you've described it isn't possible. Exactly how would you expect this to work? How can a single browser window go to two pages at the same time? After all, the browser can't interpret your response headers until it receives the response. So there's no step in between the two lines of code you show, the browser receives them both in the same response.
Thinking in terms of the request/response nature of the web, re-consider what you're trying to do in order to meet the need you're addressing. You stated that:
I want to redirect it first to a page (eg google.com) then to another page (eg bing.com)
There's an order of events there:
User requests your page.
Your page responds with a redirect to Page 1.
User requests page 1.
Page 1 responds.
User requests Page 2.
Page 2 responds.
The step where you're looking to interject is between 4 and 5 above. Naturally, you can't modify or in any way control Google's response so you can't have a Page 1 that you don't control respond with a redirect to Page 2. (Indeed, even if they did, it's either content or a redirect... not both.)
Off the top of my head there may be a workaround that might fit your needs. You could use frames to keep the user on your page while showing them the content of these other pages. Within your parent frame, which you would control, you can use JavaScript to set the various timers and other events which would direct the user from Page 1 to Page 2. (Is it instant? If so, why bother with Page 1 at all? Is it after a few moments? What should cause the redirect from Page 1 to Page 2?)
Well, maybe you would like to open 2 page at the same time.
You can control it by javascript.
Something like following code:
var open_link_google = window.open('','_parent');
open_link_google.location="http://www.google.com";
var open_link_yahoo = window.open('','_blank');
open_link_yahoo.location="http://www.yahoo.com";
Something like following code:
var open_link_google = window.open('','_parent');
open_link_google.location="http://www.google.com";
var open_link_yahoo = window.open('','_blank');
open_link_yahoo.location="http://www.yahoo.com";
When a user does a search on my website, and there is only one entry, then I want to redirect the user to the search result. The current way that I am doing this is poor. Here is how I am currently doing this:
If there is only one search result on the search result page, then I render a hidden input with an ID of "redirect" and a value of the link to redirect to. In the javascript, if the hidden input with ID "redirect" exists, then the user is redirected to the value of the element.
It's a poor way to do this because the single search result is loaded first, so the user actually sees that there is one search result. Then, it loads after, say, 3 seconds.
Is there a better way to do this?
You could use the header() PHP function to redirect if there is only 1 result.
header("Location: http://www.example.com/");
You should use PHP to determine if there is only one result, then do a server-side redirect like so:
header( 'Location: http://www.yoursite.com/redirect_page.html' );
exit;
Before printing anything to the page, check to see if there is only one result, and if so, render that script and exit so that nothing else gets processed.
<?php
//Query & other stuff
$num_of_results=mysql_num_rows($result);
if($num_of_results==0){
//no results
}elseif($num_of_results==1){
//only 1 result
//pseudo-code
//get id or controller ect from result set
header('Location: ./page/'.$id);
die();
}else{
//normal display of search results
while($row=.......){
}
}
?>
The issue with your logic is that you're waiting for something in the page to load THEN redirecting. I think a more elegant solution is to change the flow of things to give you a little more flexibility.
First, you're going to want to preprocess your query and check for the pertinent information; if there is one result, use the header(); redirects as mentioned. You may need to add some more information to the result set (database table) to make this possible.
I think taking this a step further, however, would be to redirect certain terms automatically as well. You'll kill two birds with one stone.
Let's say you have a database that is term and url - you could add certain terms to the list that also serve as a redirect. This is great for certain keywords that there are variations of. Use this sparingly though - it is great to use in conjunction with your site statistics. It may help you in instances where 0 records are shown.
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.