Flag This Hub

Creating Custom User Agent String Using PHP From Default

By


What is User Agent String

A user agent is the client application used with a particular network protocol; the phrase is most commonly used in reference to those which access the World Wide Web. Other systems, such as Session Initiation Protocol (SIP), use the term user agent to refer to both end points of a phone call, server and client.

A user agent string is the textual representation of the user agent (often a web browser), with which a network resource is accessed (often a web page). Just for example, if a web page is able to trace the user agents of its visitors, it'll show IE version 7 (on Windows XP SP2 OS) as "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)".

 

How To Change The Default User Agent String

The user agent string of the browser can't be changed by default. But we often see, the logs of website visitors say, user agents were "Google Bot". What is this? Is this a special website browser used by Google? No, This is the website crawler engine of Google. The server where the crawler script is hosted sends this string in the header while sending an http request for crawling your web page. So, it's quite clear that if the visitor of a web page is not a browser (ie. a human user), then the user agent string must be sent by the server software (Apache or IIS or whatever you have). Now in this short tutorial I'll show you how to change the default user agent string of your server using PHP.

Add this piece of code at the top the php file which is used for accessing a webpage and save it.

    ini_set("user_agent","Your User Agent String Here");

Now you have a question in mind. How your php file can access a web page? Right? It's easy. Suppose your php script fetches the content of a web page. It reads the content of a web page to extract links on it, or simply, to it. In this case, you can show your user agent string to the web page which is crawled. The complete piece of PHP code for showing your custom user agent string is given below.

<?php
ini_set("user_agent","Your User Agent String");
$data = file_get_contents(http://your-web-page.com",0);
Print strip_tags($data,"");
?>   

Run this piece of code. After that check the visitor log of your-web-page.com and you can see that the user agent string of the visitor is shown "Your User Agent String". This piece of code is really useful for a PHP MySQL Developer, who are working in the field of search engine programming.

Who Need To Change The Default User Agent String

A professional website developer or programmer, who is developing his own web page crawler / spider / scraper using PHP, needs this piece of code. Though it's not important to change the user agent string essentially, while crawling a web page, still it creates a professional impact of your crawler program. Moreover, there are lot of sites, who blocks the access of illegal or unauthorized crawlers / robots to crawl their web pages. By changing the default user agent string, you make yourself sure that, you're not crawling any web page without authorization or violating the robot rules defined for the web page.

Comments

Public Reviews 2 years ago

Oh! That's great. I was in search of this piece of trick to change the user agent string of my server. It worked like a charm.

kevin 19 months ago

very usefull little script

ali 15 months ago

Thank you for this script.

please add " before http:

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working