User agent detection for redirect

Asked

Viewed 122 times

0

I would like to identify IE and redirect them to another page. I took the example in the PHP manual but gives an error.

<?php 
 if (strstr($_SERVER["HTTP_USER_AGENT"],"MSIE"))
{
 echo"IE";
 } else {echo"Outro";}
?>

The error presented

( ! ) Parse error: syntax error, Unexpected '{' on line 3

  • 4

    Syntax error is not in these lines.

  • It is the first { that this on line three I just modified here to stay together but the code is just that it has nothing else is just a test page

  • there’s nothing wrong...

  • 1

    The interpreter can get lost in syntax errors, it can indicate a line because there it was possible to identify that there is an error but in fact the actual origin of the error may be in another line. I’ll give you a hint: if you try to be careful in the organization of your code it is much easier to understand it and find possible errors. Just because you can write everything together doesn’t mean you should. It even serves to better organize the thinking about the algorithm. The human brain works better that way.

  • But there are no other lines just these, as Avia said I just modified to get everything together here but the line 3 is found the first { I’ve researched several sites all have similar examples but it doesn’t work I don’t know if it can be problem with wamp?

  • 3

    I doubt it is WAMP. This code is correct and does not have 3 lines, so it is impossible to have only these lines. Look at it working: http://ideone.com/FuWm2s. There’s another problem in it, but it’s for another reason.

  • 3

    Post your code exactly how it’s on your machine (I don’t know if it’s the way @Kaduamaral put it).

  • And review the error also if you are now presenting another message. Now it became more difficult to still have error in line 3. Anyway, piled up, the error will be in line 1, could have several, all would be in line 1 as it is in the comment. Separating code into multiple lines is not freshness, it is important to facilitate maintenance.

  • Thank you is just like that!!! really do not know why should give this error I keep searching in several forums and all are similar examples and as everyone here say that there is no error gets more difficult!!!

  • 2

    It’s unlikely he’s exactly like this. In programs any minor detail can make a difference. Without seeing each character we have nothing to do. You will have to call some experienced programmer you know there to help you.

Show 5 more comments

1 answer

1

A good way to start solving your problem is by identifying where PHP is registering the User-Agent variable. User-Agent is an HTTP header and is sent to every request.

Start by making a PHP page with code:

<?php phpinfo(); ?>

This page comes with a lot of information about PHP: which version, whether it is installed on Apache server or any other and... also has the headers and in turn the User-Agent passed by the browser! Search your browser by searching with CTRL+F on the page.

Here in my example, we have the line:

PHP Variables
_SERVER["HTTP_USER_AGENT"]  Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36

Note that on my server, your code works! But it may be that your server is another, and then this variable may be another.

About something related, you can also make a code[1] and see all HTTP headers, including the User-Agent:

<?php
$headers = apache_request_headers();

foreach ($headers as $header => $value) {
  echo "$header: $value <br />\n";
}
?>

But this only works on Apache-like servers.

[1] Source: http://php.net/manual/en/function.apache-request-headers.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.