twigHow to get the user agent in PHP Twig?
The user agent can be obtained in PHP Twig using the app.request.server.get('HTTP_USER_AGENT')
method.
Example code
{{ app.request.server.get('HTTP_USER_AGENT') }}
Output example
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
Code explanation
app.request
: This is an object that contains information about the current request.server
: This is a property of theapp.request
object that contains information about the server environment.get('HTTP_USER_AGENT')
: This is a method of theserver
object that returns the user agent string.
Helpful links
More of Twig
- How to check if a string contains a substring in PHP Twig?
- How to use the 'foreach' loop with PHP and Twig?
- How to integrate Twig with Yii2?
- How to parse XLSX in Twig with PHP?
- How to handle whitespace in Twig with PHP 7.4?
- How to write PHP code in Twig?
- How to use PHP variables in Twig?
- How to trim a string in PHP Twig?
- How to get a substring in PHP Twig?
- How to prevent Server-Side Template Injection (SSTI) in PHP Twig?
See more codes...