PHP get random User Agent Function

I was working on an in house project in which I had to test different user agent via cURL request. If you are a PHP beginner then, User agent is used to send user browser and operating system information to the web server (website). Web server in return send different content according to user browser and operating system. You can print user browser agent by calling $_SERVER['HTTP_USER_AGENT'] in PHP.

For more information read: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent

So in this post i am going to share a custom PHP function which I wrote to get random user agent. I collected almost 75 to 76 different user agents of different devices and create an array of them named as $userAgentArray. Then I wrote a function getUserAgent() which return single random user agent. I used array_rand() function to get random key from array.


User Agent Function:

How to use User Agent Function:

Output:

Also read:

 

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *