User Agent Targeting with PHP
I’m currently working on a site for a client and they have attached PDFs that open in modal windows, but have included language for users to right-click and download them adjacent to the links. I used the generic language “Save link as …” but my client couldn’t find that option in the right-click contextual dialog. So, I thought to myself, “hmm, how should I fix this for them?”
So, I wrote a quick little PHP function to produce User Agent specific “download this link” language.
function contextual_download_text(){
if(isset($_SERVER['HTTP_USER_AGENT'])){
$agent = $_SERVER['HTTP_USER_AGENT'];
}
if(preg_match('/.*?Firefox/i',$agent)){
// Firefox
$dl_link = "Save Link As ...";
} elseif (preg_match('/.*?MSIE/i',$agent)) {
// IE
$dl_link = "Save Target As ...";
} elseif (preg_match('/.*?Chrome/i',$agent)) {
// Chrome
$dl_link = "Save link As ...";
} elseif (preg_match('/.*?Safari/i',$agent)) {
// Safari
$dl_link = "Download Linked File As...";
} else {
// default to IE, why not?
$dl_link = "Save Target As ...";
}
return $dl_link;
}
Then echo the function in your markup.
< ?php echo contextual_download_text(); ?>
Published on June 29, 2011