{"id":27,"date":"2008-08-20T20:22:22","date_gmt":"2008-08-21T00:22:22","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=27"},"modified":"2008-08-20T20:22:22","modified_gmt":"2008-08-21T00:22:22","slug":"changing-the-user-agent-of-php","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2008\/08\/20\/changing-the-user-agent-of-php\/","title":{"rendered":"Changing the user agent of PHP"},"content":{"rendered":"<p>If you ever need to adjust or change the default user agent that PHP accesses pages with (http and ftp wrappers) because either the default PHP user agent was blocked (&#8220;PHP&#8221;) or you wrote a bot with PHP and wish to customize it, you can do so easily with a <a rel=\"nofollow\" href=\"http:\/\/ca3.php.net\/manual\/en\/ini.php#ini.list\">php directive called &#8220;user_agent&#8221;<\/a>. <strong>Note that 3rd party ways (such as cURL) are not affected by this setting.<\/strong><\/p>\n<p>To set it, you can use the following function:<\/p>\n<p><code><span style=\"color: #000000;\"><span style=\"color: #0000bb;\">&lt;?php<\/span><\/span><\/code><\/p>\n<p><span style=\"color: #007700;\">function <\/span><span style=\"color: #0000bb;\">set_php_user_agent<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$useragent <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #dd0000;\">&#8216;PHP&#8217;<\/span><span style=\"color: #007700;\">)\u00a0{<br \/>\n<\/span><span style=\"color: #0000bb;\">$u <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #0000bb;\">strval<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$useragent<\/span><span style=\"color: #007700;\">);<br \/>\nif(<\/span><span style=\"color: #0000bb;\">ini_set<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">&#8216;user_agent&#8217;<\/span><span style=\"color: #007700;\">,<\/span><span style=\"color: #0000bb;\">$u<\/span><span style=\"color: #007700;\">)\u00a0=== <\/span><span style=\"color: #0000bb;\">false<\/span><span style=\"color: #007700;\">)\u00a0{<br \/>\n<\/span><span style=\"color: #0000bb;\">$r <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #0000bb;\">false<\/span><span style=\"color: #007700;\">;<br \/>\n}\u00a0else\u00a0{<br \/>\n<\/span><span style=\"color: #0000bb;\">$r <\/span><span style=\"color: #007700;\">= <\/span><span style=\"color: #0000bb;\">true<\/span><span style=\"color: #007700;\">;<br \/>\n}<br \/>\nreturn <\/span><span style=\"color: #0000bb;\">$r<\/span><span style=\"color: #007700;\">;<br \/>\n}<\/span><\/p>\n<p><span style=\"color: #ff8000;\">\/\/\u00a0Example:<br \/>\n<\/span><span style=\"color: #0000bb;\">set_php_user_agent<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">&#8216;TestAgent\/1.0\u00a0(http:\/\/www.example.com\/testagent\/)&#8217;<\/span><span style=\"color: #007700;\">);<\/span><\/p>\n<p><span style=\"color: #0000bb;\">?&gt;<\/span><\/p>\n<p>To get you started, here are some user agents for you to use or test:<\/p>\n<ul>\n<li><strong>Mozilla\/2.01 (Win16; I)<\/strong> &#8211; Netscape 2 on Windows 3.1<\/li>\n<li><strong>Mozilla\/2.0 (compatible; MSIE 3.0; I; Win95) <\/strong>&#8211; IE 3 on Windows 95<\/li>\n<li><strong>Mozilla\/3.0 (WinNT4; I)<\/strong> &#8211; Netspace 3 on Windows NT4<\/li>\n<li><strong>Mozilla\/4.0 (compatible; MSIE 4.01; Win98)<\/strong> &#8211; MSIE 4.01 on Windows 98<\/li>\n<li> <strong>Opera\/5.11 (compatible; MSIE 5.0; Windows ME) [en]<\/strong> &#8211; Opera 5.11 on Windows ME<\/li>\n<li><strong>Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)<\/strong> &#8211; IE6 on WinXP<\/li>\n<li><strong>Mozilla\/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko\/2008061015 Firefox\/3.0 (ubuntu-hardy) <\/strong>&#8211; Firefox 3.0 on Ubuntu Hardy<\/li>\n<li><strong>Lynx\/2.8.6rel.4 libwww-FM\/2.14 SSL-MM\/1.4.1 GNUTLS\/2.0.4 (Linux i686)<\/strong> &#8211; Lynx 2.8.6 on Linux<\/li>\n<li><strong>Mozilla\/5.0 Galeon\/1.0.2 (X11; Linux i686; U;) Gecko\/20011224<\/strong> &#8211; Galeon 1.0.2 on Linux<\/li>\n<li><strong>Lynx\/2.8.6rel.4 (Windows NT 5.1; I)<\/strong> &#8211; Lynx 2.8.6 on Windows XP<\/li>\n<li><strong>Wget\/1.10.2<\/strong> &#8211; GNU Wget 1.10.2<\/li>\n<\/ul>\n<p>If you found this article interesting or helpful, please help Compdigitec spread the word. Don&#8217;t forget to <a href=\"\/labs\/feed\/\">subscribe<\/a> to Compdigitec Labs for more interesting articles!<\/p>","protected":false},"excerpt":{"rendered":"<p>If you ever need to adjust or change the default user agent that PHP accesses pages with (http and ftp wrappers) because either the default PHP user agent was blocked (&#8220;PHP&#8221;) or you wrote a bot with PHP and wish to customize it, you can do so easily with a php directive called &#8220;user_agent&#8221;. Note [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,5],"tags":[96,97,864,95],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/27"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}