Back to Top

Detect the mobile devices in the Magento as Zend_Http_UserAgent_Mobile is deprecated.

Updated 1 May 2023

Hello Friends!!

In today’s blog, I will recommend a solution to detect mobile devices. As after Magento 2.4.4 Zend_Http_UserAgent_Mobile is deprecated.

We will create a helper with all the methods we need. And later we will use this helper in the block file. You can simply use the below code in your helper file.


namespace Webkul\DetectMobile\Helper;

class Mobile
     * @var array
    protected $_haTerms = [

     * User Agent Signatures
     * @var array
    protected $_uaSignatures = [
        'opera mini',
        'opera mobi',
        'windows ce',
        'lg ',
        'lge vx',
        'linux armv',
        'mini 9.5',
        'mot 24',
        'nintendo wii',
        'qt embedded',
        'sony cmd',
        'symbian os',
        'windows phone os 7',
        'wm5 pie',
        'wms pie',

     * first 4 letters of mobile User Agent chains
     * @var array
    protected $_uaBegin = [
        'w3c ',

     * Comparison of the UserAgent chain and User Agent signatures
     * @param  string $userAgent User Agent chain
     * @param  array $server $_SERVER like param
     * @return bool
    public function match($userAgent, $server)
        //  To have a quick identification, try light-weight tests first
        if (isset($server['all_http'])) {
            if (strpos(strtolower(str_replace(' ', '', $server['all_http'])), 'operam') !== false) {
                // Opera Mini or Opera Mobi
                return true;

        if (isset($server['http_x_wap_profile']) || isset($server['http_profile'])) {
            return true;

        if (isset($server['http_accept'])) {
            if ($this->matchAgentAgainstSignatures($server['http_accept'], $this->_haTerms)) {
                return true;

        if ($this->userAgentStart($userAgent)) {
            return true;

        if ($this->matchAgentAgainstSignatures($userAgent, $this->_uaSignatures)) {
            return true;

        return false;

     * Match a user agent string against a list of signatures
     * @param  string $userAgent
     * @param  array $signatures
     * @return bool
    protected function matchAgentAgainstSignatures($userAgent, $signatures)
        $userAgent = strtolower($userAgent);
        foreach ($signatures as $signature) {
            if (!empty($signature)) {
                if (strpos($userAgent, $signature) !== false) {
                    // Browser signature was found in user agent string
                    return true;
        return false;

     * Retrieve beginning clause of user agent
     * @param  string $userAgent
     * @return string
    public function userAgentStart($userAgent)

        $mobile_ua = strtolower(substr($userAgent, 0, 4));

        return (in_array($mobile_ua, $this->_uaBegin));

Now we will create the block file and will use the helper which we created earlier to check if it’s a mobile device or not.

You can simply use the below code in your block file.

Searching for an experienced
Magento Company ?
Find out More

namespace Webkul\DetectMobile\Block;

class DetectMobile extends \Magento\Framework\View\Element\Template
     * @var \Webkul\DetectMobile\Helper\Mobile
    protected $mobile;

     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Webkul\DetectMobile\Helper\Mobile $Mobile
     * @param array $data
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\HTTP\Header $httpHeader,
        \Webkul\DetectMobile\Helper\Mobile $mobile,
        array $data = []
    ) {
        $this->httpHeader = $httpHeader;
        $this->mobile = $mobile;
        parent::__construct($context, $data);

     * Is Mobile
     * @return bool
    public function Ismobile()
        $userAgent = $this->httpHeader->getHttpUserAgent();
        $server = $this->getRequest()->getServer();
        $isMobileDevice = $this->mobile->match($userAgent, $server);
        return $isMobileDevice;

Now, the method Ismobile() will return the desired result.

That’s all about detecting the mobile without Zend_Http_UserAgent Hope this will be helpful.

If you have any questions please comment below, and we will try to respond to you.

Thanks! 🙂

. . .

Leave a Comment

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


  • Gagandeep Singh
    • Shweta Singh (Moderator)
  • Gagandeep
    • Shweta Singh (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home