Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Setting your user_agent in PHP - telling back servers who you are

If your web page pulls information in from other sites, you'll be turning your web server into a client (browser) to those other sites - and just as your web server can identify the type of browser it's answering via the $_SERVER[HTTP_USER_AGENT] variable, so can the remote server.

The default user agent setting that PHP sends out is an empty string - it does not identify itself - and that will sometimes cause the server it's contacting to provide a limited response, or even (as I have see in one case) send back code 403 "Forbidden"s. So it's not a good idea to leave this default if you're contacting a wide range of web sites - for example if you're writing a crawler.

PHP's user_agent parameter can be set outside your application via lines in the httpd.conf file (for the whole of your site), or within a .htaccess file (for all applications in and below a specific web site directory). You can also set it on a "per application" basis within the application - for example:
  ini_set("user_agent","Well House Robot - see http://www.wellho.net");

If you shouldn't use the default, then what should you use? I would suggest that you strongly and proudly announce your robot / site, and give anyone who finds you in their log files a link through which they learn who you are, and why you are on their site. And you should - of course - respect their robots.txt file too [see here].

It's also possible for you to set the user_agent to the same string that would be used by a browser - in other words, it's possible for you to pretend to be Chrome, Firefox or Explorer. If you are writing a script to test a site automatically and with permission of the site owner, then go ahead and do this - but if you pretend to be a browser you're not when visiting lots of other people's site, some of them may get rather upset with you - in effect, you'll be considered to be forging a browser's signature.

There's an example of each type of user agent string in use, and how it appeared in the log files on our server when I ran it, [here].
(written 2010-12-18)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H306 - PHP - Who is your visitor?
  [3946] Moving from a warning system to a control system - PHP, forum spammers - (2012-12-07)
  [2667] Web page to telephone calls / links using an iPhone - (2010-03-08)
  [2380] Object Oriented programming - a practical design example - (2009-08-27)
  [2343] World Flags in your PHP pages - (2009-08-10)
  [2135] What features does this visitors browser support? (PHP) - (2009-04-22)
  [1736] Current visitors from around the world - PHP - (2008-08-04)

H307 - PHP - Web2 and caching
  [4627] Caching results in an object for efficiency - avoiding re-calculation - (2016-01-20)
  [4136] How do I post automatically from a PHP script to my Twitter account? - (2013-07-10)
  [4106] Web server efficiency - saving repetition through caches - (2013-05-30)
  [4075] Further recent PHP examples - (2013-04-28)
  [4055] Using web services to access you data - JSON and RESTful services - (2013-03-29)
  [3999] Handling failures / absences of your backend server nicely - (2013-02-08)
  [3955] Building up from a small PHP setup to an enterprise one - (2012-12-16)
  [3458] On this day ... one PHP script with three uses - (2011-09-26)
  [3186] How to add a customised twitter feed to your site - (2011-02-27)
  [3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
  [2545] Scraping content for your own page via PHP - (2009-12-21)
  [2321] Uploading and Downloading files - changing names (Perl and PHP) - (2009-08-04)
  [2196] New Example - cacheing results in PHP for faster loading - (2009-05-24)
  [1995] Automated server heartbeat and health check - (2009-01-16)
  [1926] Flash (client) to PHP (server) - example - (2008-12-06)
  [1814] Javascript/HTML example, dynamic server monitor - (2008-09-28)
  [1813] Ajax - going Asyncronous and what it means - (2008-09-28)
  [1812] Starting Ajax - easy example of browser calling up server data - (2008-09-27)
  [1733] memcached - overview, installation, example of use in PHP - (2008-08-02)
  [1647] Exchange Rates - PHP with your prices in your users currency - (2008-05-19)
  [1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)


Back to
How many toilet rolls - hotel inventory and useage
Previous and next
or
Horse's mouth home
Forward to
The Christmas Season has arrived
Some other Articles
Learning Object Orientation in Perl through bananas and perhaps Moose
Making Perl class definitions more conventional and shorter
Contrast in pictures
The Christmas Season has arrived
Setting your user_agent in PHP - telling back servers who you are
How many toilet rolls - hotel inventory and useage
wxPython geometry - BoxSizer example
How do regular expressions work / Regular Expression diagrams
Matching to a string - what if it matches in many possible ways?
Python regular expressions - repeating, splitting, lookahead and lookbehind
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3094_Set ... u-are.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat