-1

I want to do a simple thing, wich is: if someone is browsing my website in internet explorer, then a div of my website must be shown in a form, if not then its shown on another form.

Im trying something simple to test the conditions but its not working. Here's the code

$IE = 0;
echo "<!--[if lte IE 8]>" ;
$IE = 1;
echo "<![endif]-->";

die("IE is: "  . " " . $IE);

While using chrome, it always shows IE is 1.

Am i doing something wrong?

Charles Burns
  • 10,310
  • 7
  • 64
  • 81
  • What kind of code is that? – Erik Philips Aug 30 '13 at 16:22
  • you are mixing html comments with php, it doesnt work like that. if you want to check browser in php use `$_SERVER['USER_AGENT']`, but note that it can be spoofed. – Patrick Evans Aug 30 '13 at 16:23
  • You're trying to mix client-side and server-side code. That cannot possibly work. Even without that, Chrome does not recognize conditional comments. – SLaks Aug 30 '13 at 16:23
  • I think you're getting confused between PHP and Javascript. The `$IE` code is PHP, which is not affected by which browser the user is running. – Spudley Aug 30 '13 at 16:24
  • Well i know its very short. What i'd like to do is to use the $IE variable to then draw a div with differents ID's (if its IE, the div will have a name that, in css, will be displayed in a specific form, if not, than the name will be other so i can draw it in a different way). – user2703848 Aug 30 '13 at 16:24
  • Ok, i see now the problem. Than what could i do? – user2703848 Aug 30 '13 at 16:25
  • Do a search on the internet for how to detect what browser the user has visited your site with: http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser – Eric Leschinski Aug 30 '13 at 16:30
  • No need guys, alreay did it :) I included in header this: echo ""; – user2703848 Aug 30 '13 at 16:33
  • that is not how you use IE conditional comments – markasoftware Aug 30 '13 at 20:42

2 Answers2

1

One of the basic things in web development is that one needs to distinguish what happens on the server and what in the browser and how the two are related.

PHP runs on the server, conditional IE comments in the browser. Between the two is the HTML file that is sent over the network.

Your script is executed on the server. The result is :

<!--[if lte IE 8]><![endif]-->IE is:   1

As you see, there is nothing between the conditional comment tags.

On the server side you need to rely on the user agent to guess the browser. I say guess, because you cannot be 100 % sure. Use something like this and let you inspire here : http://www.useragentstring.com

$IE = (bool) strpos($_SERVER['USER_AGENT'], 'MSIE') ;
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
0

You have messed up a little bit java script with php code. If you want to return different html to the client dependently on what sever user uses then please check $_SERVER['HTTP_USER_AGENT'].

Use phpinfo() in your php code or simply call echo $_SERVER['HTTP_USER_AGENT'] there, and open your page in various browsers to see how they present themselves to you.

Grzegorz
  • 3,207
  • 3
  • 20
  • 43