{"id":98,"date":"2011-06-29T13:36:13","date_gmt":"2011-06-29T18:36:13","guid":{"rendered":"http:\/\/www.marcblase.com\/blog\/?p=98"},"modified":"2015-04-24T08:56:50","modified_gmt":"2015-04-24T13:56:50","slug":"user-agent-targeting-with-php","status":"publish","type":"post","link":"https:\/\/ma.rcbla.se\/blog\/2011\/06\/user-agent-targeting-with-php\/","title":{"rendered":"User Agent Targeting with PHP"},"content":{"rendered":"<p>I&#8217;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 &#8220;Save link as &#8230;&#8221; but my client couldn&#8217;t find that option in the right-click contextual dialog. So, I thought to myself, &#8220;hmm, how should I fix this for them?&#8221;<\/p>\n<p>So, I wrote a quick little PHP function to produce User Agent specific &#8220;download this link&#8221; language.<\/p>\n<pre lang=\"php\" line=\"1\">\r\nfunction contextual_download_text(){\r\n\r\n\tif(isset($_SERVER['HTTP_USER_AGENT'])){\r\n\t\t$agent = $_SERVER['HTTP_USER_AGENT'];\r\n\t}\r\n\r\n\tif(preg_match('\/.*?Firefox\/i',$agent)){\r\n\t\t\/\/ Firefox\r\n\t\t$dl_link = \"Save Link As ...\";\r\n\t} elseif (preg_match('\/.*?MSIE\/i',$agent)) {\r\n\t\t\/\/ IE\r\n\t\t$dl_link = \"Save Target As ...\";\r\n\t} elseif (preg_match('\/.*?Chrome\/i',$agent)) {\r\n\t\t\/\/ Chrome\r\n\t\t$dl_link = \"Save link As ...\";\r\n\t} elseif (preg_match('\/.*?Safari\/i',$agent)) {\r\n\t\t\/\/ Safari\r\n\t\t$dl_link = \"Download Linked File As...\";\r\n\t} else {\r\n\t\t\/\/ default to IE, why not?\r\n\t\t$dl_link = \"Save Target As ...\";\r\n\t}\r\n\t\r\n\treturn $dl_link;\r\n\r\n}\r\n<\/pre>\n<p>Then echo the function in your markup.<\/p>\n<pre lang=\"php\">\r\n< ?php echo contextual_download_text(); ?>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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 &#8220;Save link as &#8230;&#8221; but my client couldn&#8217;t find that option in the right-click contextual dialog. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[25,16,24],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-coding","tag-browser","tag-php","tag-useragent"],"_links":{"self":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/98","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":9,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":449,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/98\/revisions\/449"}],"wp:attachment":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}