How to detect prefetching from the browser in PHP? - php

Some pages can be prefetched by the browser (see this or that). How to detect in PHP that a request is a prefetched request ?
Is there any method is not browser-specific ?
(PS: I use Apache.)

I was looking for a similar answer to stop FF and other browsers throwing out my server side page counts.
Firefox
Chrome white paper
According to the articles above, Firefox sends a header which makes it easy to detect but the Chrome white paper states that Chrome doesn't give any indication to the server. The Chrome white paper notes that external analytic solutions (scripts written in JS) will not count the prefetch hits when they use the page visibility API.
My suggestion is to write your own JS script to detect if the page is ever made visible.

This question is very similar to "How to detect if cookies are enabled?". But you must use client-side redirection, because prefetched page start loading after loading main page.
Directly prefetch request can by detected by header (X-Moz: prefetch for FF) or by passing additional param to a link.
P.S. Without cookies enabled you cannot detect it for client, only for each directory prefetch request.

Related

track request's on page load

i analyzed a page that i'm working on with live http headers addon for FF and im seeing that is making request to the same page 2-3 time during the load of it. i checked for empty img tags that could be the reason but there is none on the html code. is there any addon out there or something that could let me track what resource is firing this request? or any other recommendation of what is should look apart from the empty img tags.
the page have several javascript libraries and mootools with ajax calls, but none of them is causing it as they load the resources and the ajax calls are get request passing several parameters to the page, this unexpected request are clean one, whiteout parameters.
Regards,
Shadow.
SOLUTION
monitoring from the server side the request and commenting code, i realized that was making 3 request's
1) from a popup blocker checker that open an about:blank in theory but it was requesting the same page
2) a class used to check if flash was enabled
3) the yslow plugin for FF was making the last one.
i'm worried about this last one as it screw up a feature in my app, so i will need to have some alternative thing here :)
if you have access to server logs, you can find the answers there.
finally, you can use some sniffer software to see all http request sending by browser

Website with optimal cache control

My goal
I would like to let browsers cache my whole website, but only download the static content when I have changed one or more files.
My situation
After some research I have found a way to do this. That is to add a Far Future Expires Header to my htaccess file and add a querystring to my files using the filemtime() function.
The problem
When I click on the address bar and type in my website address in firefox, then Firebug displays 38.3 KB (36.4 KB from cache)
When I press F5 in firefox, then Firebug displays:241.1 KB (10.9 KB from cache)
Now I have tried to do the same with Google and they are sending HTTP header 304 back. I have read a lot about ETag and the Last Modified header, but I have heard a lot of people saying that they are not really reliable.
My question
What would be the best solution if I would like to send HTTP header 304 back with my static content if the user presses on F5, like Google?
I am asking this question because I am often visiting a website and using F5 to see if there is some new information available. Not to reload the images etcetera.
Update
It seems that Firefox is controlling the way the cache is used and I would like to use the cache also when a user presses F5.
The very purpose of reload is to reload the page. There is no server-side header magic if the browser was witten to ignore caches when the user specifically asks for it.
The solution for Google is that you check if the crawler sent an If-Modified-Since header with:
if ($_SERVER["HTTP_IF_MODIFIED_SINCE"]) {
header("HTTP/1.0 304 Not Modified");
exit();
}
This trick could work for browsers, but not in forced reload modes, like Firefox's SHIFT+RELOAD.
You can also use the newer application cache feature.
I don't know what your target browser is, but most browsers have been supporting it for quite a few versions so far..
This way you can define your statics to be downloaded only once.
For some very good information on the subject you can take a look at this page:
http://diveintohtml5.ep.io/offline.html
I'm not sure I understand the intent of your question, but you can specify the response code in php, with the header function, regardless of whether or not your user presses a button.
http://php.net/manual/en/function.header.php

Get Browser Tab Index/Id

So in most current browsers there is the feature of Tabs, is there a way to get the Tab index?
so Tab 1 has www.google.com opened in it and Tab 2 has www.google.com opened in it, is there a way to identify what the Tab index is?
Pseudo Code:
if($tab == 2) {
alert "Tab 2 is active\n";
}
if($tab == 1) {
alert "Please use Tab 2 as this is Tab 1\n";
}
Funny as everything I search for about tabs is related to the tab index of the webpage itself, sigh...
Strictly speaking. TABS are on the end user's machine. PHP works on the server. PHP can't see what the end user's machine is doing, it can only serve the end user PHP'ed pages.
Google does this with JavaScript and Cookies. For every instance of the page opened, increment a cookie counter. If the counter > 1, use AJAX to display an error message. Also, prohibit the page from functioning if cookies or JavaScript is disabled.
Look into jQuery.
As far as determining the absolute tab index, I know of no way to do it with Javascript. You can identify windows by their names, but not anything else.
In your example of two tabs containing the same web page, you should be able to uniquely identify them by making them aware of each other. You'd need to use cookies for this. Essentially, when a page is loaded, it would check for a cookie that tells it about other instances of the page that are currently loaded, and make decisions accordingly.
In this scenario, your onload handler would check the cookies, and register the loading page. You'd also need an onunload handler to unset the cookie pertaining to the page being unloaded.
See Javascript communication between browser tabs/windows for more information on how to use cookies to communicate between windows with Javascript.
in php: definitely not - it's executed on your server without access to the cleints browser.
maybe there's a solution using javascript (but i've never heard of that, and i'm pretty sure this isn't possible too - at least not as a cross-browser solution).
i think the best chance you'll have (if there even is one) is using other client-side languages like flash, silverlight or a java-plugin as this ones can do a lot more than javascript - but i'm sorry i don't know any of these good enough to give more information or hints.
Don't waste anymore time on this mate. It isn't possible, mainly because any webpage inside browser will not be able to get this kind of information due to security restrictions.
Try looking for an laternative approach as some of the other guys have suggested in their comments.
I am sure there is not a global variable for support that information. But maybe clever browsers such as Firefox or Google Chrome might support something on it. I have made a quick search on net and I came with these.
First, check Mozilla Tab Helper can be work with Mozilla. But be remember, this will never be a cross browser solution. Also, I am thinking there is not a cross browser solution.
Second one is, if you want to use this for your own use then it might bu useful, I don't test it. This is a addon. Here is the Open Tab Count Mozilla Addon
Open Tab Preview

Detecting browser file compatibility

I am looking for a way to detect what files a users web browser can display (natively or via plugin), either server side in PHP or client side in JavaScript. Basically, I want to have a box in which a file is displayed (audio in an <embed>, documents in a <frame>, etc), however if the browser is simply incapable of displaying the file, I want to show the user an error and give them a link to download it.
Thanks for any help.
Your best bet is going to be to determine what browsers are supported and which ones aren't and then use browser detection to determine what response to send to the client.
Quirksmode has a great script for this purpose as well as a demo on this page:
http://www.quirksmode.org/js/detect.html
what happen if user turn off javascript ?
PHP $_SERVER['HTTP_USER_AGENT'] can be faked, even a newbie can do this with User Agent Switcher (a firefox addon)
i think there isn't a absolute solution for this.

how does facebook/digg get all of the iamges for a webpage?

DO they use a php page to analyze the link, and return all of the images as josn?
Is there a way to do this with just javascript, so you dont have to go to the server to analyze the page?
I don't now how they do it. I'd implement a small service for that purpose. Given an URL return some relevant image (or generate a screenshot). This service could also cache results for better performance. But still, the page needs to be accessed in order to grab the <img src=... or to take the photograph.
Facebook calls back to the server. If you use Firebug (or, as I did, the Web Inspector in Safari), you can inspect the ajax calls. Facebook calls back to a script at /ajax/composer/attachment.php - in there is some JavaScript which contains HTML that gets inserted into the page. Here is what it looks if I point the Facebook attach link dialogue to the BBC News homepage in Safari Web Inspector:
Facebook JavaScript response when you attach a link in Safari Web Inspector http://tommorris.org/files/Facebook-20100529-181745.jpg
I put up the full JavaScript response on Gist (it is all one-line and minified originally, so I just flung it through TextMate to wrap it).
I'm not sure if you could do it on the client-side - because of browser protections on cross-site scripting - and even if you could, you probably ought not to because of this potential security problem: imagine if someone puts in a URL that points to a page which only they have access to. You don't necessarily want to put what's on someone else's customised or private page up on your Facebook/Digg type site. Imagine if it was something like Flickr and there were private pics - or worse, a porno site. No, better to proxy it back to your server and then grab the images. Plus, it'll probably be faster. No need to tax your end user's potentially slow connection downloading a page when your server will probably be able to do it quicker...

Categories