PHP Include w/If IE Condition Statement?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Alan Horvath
Forum Newbie
Posts: 4
Joined: Sun Oct 18, 2009 12:20 pm

PHP Include w/If IE Condition Statement?

Post by Alan Horvath »

I was having a problem with Internet Exploder (big surprise, right?) displaying my drop down menus improperly. I found a solution that works ... I have a PHP file that I place as an include on all my pages and the drop down menus, on IE, are cool.

But my problem, at present, is this:
I need to have that PHP Include (for IE) loaded, in place of my regular PHP Include (for all the other browsers), when some one is using the IE browser.

Is there a way to write an "If IE" conditional statement for a PHP Include?

Thanks for your help!

Alan
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP Include w/If IE Condition Statement?

Post by califdon »

You can normally determine what browser is being used with the $_SERVER['HTTP_USER_AGENT'] variable. See http://php.about.com/od/learnphp/p/http_user_agent.htm. You will probably have to parse the string to determine the value you need.
Alan Horvath
Forum Newbie
Posts: 4
Joined: Sun Oct 18, 2009 12:20 pm

Re: PHP Include w/If IE Condition Statement?

Post by Alan Horvath »

Thanks, Jack. I might be missing it, but I don't see the solution I'm looking for via the link you gave me.

What I'm looking to do is to display my drop-down menus with a PHP include, like this:
<?php include "MyMenus.php"; ?>

But I want to redirect IE browsers to a different include:
<?php include "MyMenus_IE.php"; ?>

So is there a way I can tell all browsers to read <?php include "MyMenus.php"; ?>, but have IE browsers *not* read that one and be redirected to <?php include "MyMenus_IE.php"; ?> instead?

I've tried:
<!--[if IE]>
<?php include "MyMenus_IE.php"; ?>
<![endif]-->

But that doesn't work.

Thanks for your time.

Alan
Alan Horvath
Forum Newbie
Posts: 4
Joined: Sun Oct 18, 2009 12:20 pm

Re: PHP Include w/If IE Condition Statement?

Post by Alan Horvath »

What about this? Can you explain what he means when he says ... "You will have to make sure that the code is filled under the first line of code on your PHP page." ... ?
-------------------------------------------------------
It is an simple example wich will take the visitators to one page if they are using Internet Explorer, and to another page if the visitator is using another tipe of browser.

Now lets decide what is going to happen if:

1. If the browser is Microsoft Internet Explorer (MSIE), it will automaticaly redirect to: http://www.tutorialize.org/redirect1 ( example )
2. If the browser is not Microsoft Internet Explorer (MSIE), it will automaticaly redirect to: http://www.tutorialize.org/redirect2( example )

The most interesting part of this is that this code is, that it has to be sent out before any output to the HTML page. You will have to make sure that the code is filled under the first line of code on your PHP page.

This is the example php code, it can be modified in many ways to fill your requests.


//if its MSIE then

if
(
$name
= strstr
(
$HTTP_USER_AGENT
, "MSIE"
)
)

{

//it will send to http://www.tutorialize.org/redirect1

Header
(
"Location: http://www.tutorialize.org/redirect1"
)
;
}

else

{

//else will send to http://www.tutorialize.org/redirect2

Header
(
"Location: http://www.yahoo.com/"
)
;
}
-------------------------------------------------------
Alan
Alan Horvath
Forum Newbie
Posts: 4
Joined: Sun Oct 18, 2009 12:20 pm

Re: PHP Include w/If IE Condition Statement?

Post by Alan Horvath »

Ah-haaa! I found one that works!

This goes into "index.php" ... and *nothing* else:
----------------------------------------------------
<?php
if (eregi("MSIE", $_SERVER['HTTP_USER_AGENT']))
{ $location = 'http://somesite.com/index_ie.php';
header ("location: $location");
exit();
} else {
$otherlocation = 'http://somesite.com/index_allothers.php';
header ("location: $otherlocation");
exit();
}
?>
----------------------------------------------------

HalleluYah!

Alan
Post Reply