Conditional menu display based on browser via PHP

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
pavia
Super Advanced
Super Advanced
Posts: 39
Joined: Fri Aug 16, 2002 2:39 pm

Conditional menu display based on browser via PHP

Post by pavia »

I hope it's okay to start a new topic based on a prior one? The discussion originated with this one
(http://milonic.com/forum/viewtopic.php?t=3692)
but it has, um...evolved.

In a nutshell, I'm seeking to to circumvent some browser-specific problems which crop up in older versions of Netscape. Consequently, I'd like to be able to accomplish the following with the relative-positioned-in-a-table version of the Milonic menu:

If the browser IS NOT Netscape 4.XX, load Version 5 of the Milonic menu

If the browser IS Netscape 4.XX, load Version 3 of the Milonic menu

Haven't had any luck thus far and then Kevin introduced the idea of doing the foregoing server side using PHP and I thought WOW! Great idea!! Why didn't I think of that?!? I bet it can be done...

...just as soon as I learn PHP.

I've been trying to sort this out on my own for several days now but I didn't get very far and was wondering if someone with more experience might be able to lend some assistance.

I know this is lame but the only thing I could find that might be useful dates back to November 2001--an article titled "When Browsers Suck: PHP to the Rescue!" located here: http://www.sitepoint.com/article/619.

It demonstrates server-side browser detection, provides a script for same and then gives an example for implementation that looks promising. Here's the example:

Code: Select all

<?php include(browserdetect.php); ?> 
<?php if ($browser == 'ns6'): ?> 
 <!-- Netscape 6 specific code --> 
<?php else: ?> 
 <!-- Code for other browsers --> 
<?php endif; ?>
At the end of the explanation, the writer states that one could use include files for each browser-dependent segment of the page and I thought perhaps there would be a way then to address the fact that the method for positioning Version 5 of the Milonic menu in a table is quite different from that used in Version 3.

Anyone up for tackling this? It might end up being extremely useful to a lot of Milonic users...and, of course, I would be eternally grateful for the help.
User avatar
Ruth
 Team
 Team
Posts: 8763
Joined: Thu May 15, 2003 5:02 am
Location: Yucaipa, CA
Contact:

Post by Ruth »

I am NOT a code person. But, I am interested in much of this. I had a thought when you mentioned that version 5 and 3 use different methods for placing in a table. Wouldn't you just put both the menu [version5] and the placemenu [version 3] in the same cell because wouldn't the code you use tell it which to show? Well, you're talking about include files, so wouldn't you just put the include call for each since the code you're talking about would tell it which to include depending on which browser?

Ruth
pavia
Super Advanced
Super Advanced
Posts: 39
Joined: Fri Aug 16, 2002 2:39 pm

Post by pavia »

Hi Ruth--

Yes, I think you would put the include in the table to call the code to display either menu but I have spent hours trying to figure out how to accomplish this to no avail.

I'm about to throw in the towel on this whole Milonic thing :-(

What stymies me is that you basically have two pieces of code to contend with...the code to get the menu into the table cell and the code to get the menu to show up in the first place. That's probably not a good way of referring to the latter--what I mean is this:

For Version 3:

Code: Select all

<script language=JavaScript src="menu_array.js" type=text/javascript></script>
<script language=JavaScript src="mmenu.js" type=text/javascript></script>
For Version 5:

Code: Select all

<script language=JavaScript src="milonic_src.js" type=text/javascript></script>	
<script language=JavaScript src=mmenudom.js type=text/javascript></script>
<script language=JavaScript src="menu_data.js" type=text/javascript></script>
Both of these would go right after the body tag, right?

But the other javascript needed to show the menu in the table--which is also dependent upon whether the browser is NS 4.XX--needs to go in a different location within the HTML.

I don't know how to write the PHP code--or maybe SPLIT UP THE PHP CODE(?)--in order to get this to work.
User avatar
Ruth
 Team
 Team
Posts: 8763
Joined: Thu May 15, 2003 5:02 am
Location: Yucaipa, CA
Contact:

Post by Ruth »

Disclaimer:: I AM NOT A CODE PERSON

I don't know anything about writing calls and such, but let's take the code for the menu in the table for version 3 and version 5.

Code: Select all

whatever table you have. <table><tr><td>call for version 3

<script>PlaceMenu("mainmenu")</script>

then call for version 5

<script>with(milonic=new menuname("Main")){
style=menuStyle; 
etc.
position="relative"; 
aI("text= Home;url=whatever/;status=;"); 
} 
drawMenus();</script>
</td></tr></table>
Ok so now you have version 3 and 5 in their own little script tags in the table cell. Neither will do a thing if they don't have the files to tell them to do something, the menu files and that's the code you gave an example of up above. I guess you put it just below the body since that's where version 3 needs to be to be called.

Code: Select all

<body><?php include(browserdetect.php); ?> 
<?php if ($browser == 'ns4'): ?> 
 <!-- Netscape 4 specific code --> [call the version 3]
<?php else: ?> 
 <!-- Code for other browsers --> [call the version 5]
<?php endif; ?>


Now, I have absolutely NO clue if this is a correct assumption since I do not do codes, calls and such and I thought php was some kind of acid content in soaps or something! But, if you have a call that says find the browser and if ns4 use 'this call' or else use 'that call' Then if your php thingees are working correctly when it detects ns4 it will put up the code for the menu_array.js and the mmenu.js and when it detects anything else it will put up the 3 files for version 5. Remember I dont' know how php works. if it's files you have to include then I would take it that the things I have in the script tags for the placemenu and the other one for the version 6 with menu then I would think you put the codes in their little separate files and in the table cell you put the include calls for each file. Since again, even if they 'include' both files none will function unless they have their own triggering files.

And I have one last question, can you see the milonic menu on the homepage here, does your 'browser' crash, if so have you tried a reinstall because there haven't been any other comments that I can find about ns4 and a mac crashing the milonic homesite?

Alternatively, and I can't play with this with any idea of what it will look like since you don't have a page nor have placed at least the table layout with it's sizes and such so we can get a visual on how the page is going to look, but check at this page http://www.citytech.cuny.edu/ first using the netsacpe 4. See if the browser crashes on it. That's calling a 'text' menu for netscape 4. If that crashes also, there has to be something in your browser I would think.

Ruth
pavia
Super Advanced
Super Advanced
Posts: 39
Joined: Fri Aug 16, 2002 2:39 pm

Post by pavia »

Ruth--
I'm gonna have to skip addressing the PHP stuff in your last post because when it comes right down to it, neither of us know what we are doing. And I'm not saying that to be rude so PLEASE don't take it that way. I'm saying it because we know what should be done and how it should be done but we're no closer to getting it done. Or something like that. :-)

Regarding the second part of your post...Netscape 4.79 on the Mac doesn't crash when viewing the Milonic home page. The menus do, however, stop functioning. Can't think of a way to describe it...hmmm...it's like they lock up and refuse to keep working. The only way to keep navigating the page is to click on menu items rather than hover with your mouse. It's like the entire menu turns itself into one of those "open-on'click" type of menus (but not a well-functioning one). The same thing occurs in Netscape 4.8 in Windows.

Tepidarium's menu doesn't do anything "bad" to Netscape on the Mac. I've read his posts and it would seem he has taken this route for exactly the same reasons I am seeking to load Version 3 for older Netscape browsers--Netscape 4.XX just doesn't play nice with Version 5 of the menu.
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

Hi Pavia,

I am not a PHP user. Never tried it even once and I don't have PHP on our server to test. But, if you're willing to try a little more of the blind leading the blind, I'll give it a shot.

I assume that you have the other site's browser detect code in a file called browserdetect.php and that their browser detection actually works. If it's like other server-side stuff, there's probably some sort of "Include Path" in the PHP configuration, so might want to have that file in whatever your include path is. Note that the path may be relative. If you are in doubt, you can probably override the Include Path by specifying the path in the argument to include(). E.g., if browserdetect.php is in your site's root directory, you could probably do this:

Code: Select all

<?php include('/browserdetect.php'); ?>
Or, you could probably also specify the exact location like this

Code: Select all

<?php include('http://www.yoursite.com/php/browserdetect.php'); ?>
which I imagine would look for the file in http://www.yoursite.com/php/ -- of course, this is mostly guess work on my part. You may have to play with line 1 of the following code example to get the include file loaded.

As far as loading the scripts and running functions, you will definitely need to distinguish between ns4 and not-ns4 in loading the menu scripts, as well as in the table where you display your main menu.

(1) You should be able to load the menu code for either version at the top of the <body>. Try this at the top of <body>:

Code: Select all

<?php include('browserdetect.php'); ?>
<?php
if ($browser == 'ns4') {
  echo '<script language="JavaScript" src="menu_array.js" type="text/javascript"></script>';
  echo '<script language="JavaScript" src="mmenu.js" type="text/javascript"></script>';
} else {
  echo '<script language="JavaScript" src="milonic_src.js" type="text/javascript"></script>';
  echo '<script language="JavaScript" src="mmenudom.js" type="text/javascript"></script>';
  echo '<script language="JavaScript" src="menu_data.js" type="text/javascript"></script>';
}
?>
(2) To open the main menu, try this in your table cell:

Code: Select all

<?php
if ($browser == 'ns4') {
  echo 'PlaceMenu("mainmenu");'
} else {
  echo <<<END
    <script language="Javascript">
    with(milonic=new menuname("Main Menu")){
    style=menuStyle;
    alwaysvisible=1;
    orientation="horizontal";
    position="relative";
    aI("text=Home;url=http://milonic.com/;status=Back To Home Page;");
    aI("text=Menu Samples;showmenu=Samples;");
    aI("text=Milonic;showmenu=Milonic;");
    aI("text=Partners;showmenu=Partners;");
    aI("text=Links;showmenu=Links;");
    aI("text=My Milonic;showmenu=My Milonic;");
    }
    drawMenus();
    </script>
END;
}
?>
The echo <<<END construct seems to be a good one for outputting multiple lines to the page. I think you have to make sure that END; appears flush left, on a line by itself, with no extra whitespace. If that doesn't work, you could try echoing each line of code separately, as in the first code example.

Note also that I used "mainmenu" in the PlaceMenu() call... substitute your own v3 main menu name. Also, for the v5 menu, I used the 'Main Menu' code from the v5 table-bound example... substitute your own code.

Don't give up if this doesn't work! Maybe some kind php guru will step in or, if nothing else, we'll muddle through. Let us know how it goes.

Kevin
Last edited by kevin3442 on Wed Mar 24, 2004 4:18 am, edited 1 time in total.
User avatar
Ruth
 Team
 Team
Posts: 8763
Joined: Thu May 15, 2003 5:02 am
Location: Yucaipa, CA
Contact:

Post by Ruth »

Nope, I didn't take offense, after all I put a disclaimer at the beginning of the post :lol: I have NS 4.79, win98se and I have no problem on the main site [except the search, but that's cause it's a static browser] All the rest of the menu works just fine, mouseover and everything.

Ruth
pavia
Super Advanced
Super Advanced
Posts: 39
Joined: Fri Aug 16, 2002 2:39 pm

Post by pavia »

Hiya Kevin...

Thanks for giving this a shot. :-)

Right off the bat, I got this error:

Parse error: parse error, unexpected T_ECHO -- on this line:

Code: Select all

echo '<script language="JavaScript" src="menu/menu_array.js" type="text/javascript"></script>';
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

Hi Pavia,

Is the echo all on one line of code, or split across two lines like the examples we both posted?

Kevin
pavia
Super Advanced
Super Advanced
Posts: 39
Joined: Fri Aug 16, 2002 2:39 pm

Post by pavia »

Here's the code (note--browserdetect.php calls for "nsmac" not "ns4")

Code: Select all

<body bgcolor="#FFFFFF" text="#000000">
<?php include(browserdetect.php); ?> 
<?php 
if ($browser == 'nsmac') { 
  echo '<script language="JavaScript" src="menu/menu_array.js" type="text/javascript"></script>'; 
  echo '<script language="JavaScript" src="menu/mmenu.js" type="text/javascript"></script>'; 
} else { 
  echo '<script language="JavaScript" src="menu/milonic_src.js" type="text/javascript"></script>'; 
  echo '<script language="JavaScript" src="menu/mmenudom.js" type="text/javascript"></script>'; 
  echo '<script language="JavaScript" src="menu/menu_data.js" type="text/javascript"></script>'; 
} 
?>
Hey wait--I didn't answer your question, did I? I mean, does the above display correctly?? I'm confused. 8O
pavia
Super Advanced
Super Advanced
Posts: 39
Joined: Fri Aug 16, 2002 2:39 pm

Post by pavia »

Ruth--

Re viewing the Milonic home page in Netscape on Windows...I have NS 4.8 and here's a screen shot from it. I think this should show the problem I have explained (about the menus locking up) because if you think about it, I don't think you can achieve what this shows under normal circumstances. Like, look at where my mouse cursor is...--waaaaay up to the right...and there's no submenu showing from the "samples" menu item...

Image

This is because the menu stopped working and just sat there staring at me.
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

pavia wrote:...Hey wait--I didn't answer your question, did I? I mean, does the above display correctly?? I'm confused. 8O
I guess that's what I'm getting at. When placed in a code block on this forum, the line in question wraps (at least w/ my res of 1024x768). Are you putting the code in like that, or is the echo all on one line?

echo '<script language="JavaScript" src="menu/menu_array.js" type="text/javascript"></script>';

(The line above displays on one line, no wrapping, at 1024x768, maximized, on IE6).

I only ask because of a gut feeling that having it split across two lines without some sort of concatenation character might cause the error.

If we can't get past this, I have another idea (and it does not involve doing violence to a computer).

Kevin
pavia
Super Advanced
Super Advanced
Posts: 39
Joined: Fri Aug 16, 2002 2:39 pm

Post by pavia »

Ahhhh...the old' maximize-the-window trick. Now why didn't I think of that???

To answer your question...every echo statement is on its own line.

:-)
User avatar
Ruth
 Team
 Team
Posts: 8763
Joined: Thu May 15, 2003 5:02 am
Location: Yucaipa, CA
Contact:

Post by Ruth »

Pavia, just out of curiosity have you tried reinstalling your 4.8? Truly, I get everything, submenus and all on 4.79 so I'm wondering, at least on the windows if there's something corrupted?

Ruth
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

pavia wrote:...To answer your question...every echo statement is on its own line.
Drat!

I wonder if > needs to be /> in the echos? Try this:

Code: Select all

<?php include(browserdetect.php); ?>
<?php
if ($browser == 'nsmac') {
  echo '<script language="JavaScript" src="menu/menu_array.js" type="text/javascript"/></script/>';
  echo '<script language="JavaScript" src="menu/mmenu.js" type="text/javascript"/></script/>';
} else {
  echo '<script language="JavaScript" src="menu/milonic_src.js" type="text/javascript"/></script/>';
  echo '<script language="JavaScript" src="menu/mmenudom.js" type="text/javascript"/></script/>';
  echo '<script language="JavaScript" src="menu/menu_data.js" type="text/javascript"/></script/>'; 
}
?>
Kevin
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

Hey Pavia,

You could try this instead:

Code: Select all

<?php include('browserdetect.php'); ?>
<?php if ($browser == 'nsmac'): ?>
<script language="JavaScript" src="menu_array.js" "type=text/javascript"></script>
<script language="JavaScript" src="mmenu.js" type="text/javascript"></script>
<?php else: ?>
<script language="JavaScript" src="milonic_src.js" type="text/javascript"></script>
<script language="JavaScript" src="mmenudom.js" type="text/javascript"></script>
<script language="JavaScript" src="menu_data.js" type="text/javascript"></script>
<?php endif; ?>
If that doesn't cause a choke, we'll tackle the rest tomorrow.

Kevin
pavia
Super Advanced
Super Advanced
Posts: 39
Joined: Fri Aug 16, 2002 2:39 pm

Post by pavia »

G'morning...

Both pieces of code give this error: Parse error: parse error, unexpected T_ECHO--on this line:

Code: Select all

echo 'PlaceMenu("mainmenu");' 
Hey, we're making it a little farther before it chokes!! :-)

Ruth--Netscape 4.8 on Windows is a new re-install as of about 5 days ago. I get submenus and everything too but they just fizzle out and stop working. I'm pretty sure Tepidarium complained of the same problem.
User avatar
Maz
Milonic God
Milonic God
Posts: 1717
Joined: Fri Jun 06, 2003 11:39 pm
Location: San Francisco
Contact:

Post by Maz »

I don't know backend, none of this makes much sense to me, I can't use includes because of my templating system that won't allow anything through for SSI.

Then I found a forum mod that created the javascript for me to place in any page, and it works beautifully. Now I can use the forum for adding news to my pages.

Maybe something similar to ssi via js would work great with the menu?

maz
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

Hey,
pavia wrote:...Hey, we're making it a little farther before it chokes!! :-)
Progress :!:

OK, since my understanding of PHP echos seems to be flawed seems to be flawed (little joke there), and since I forgot the <script> tags around PlaceMenu() anyway, why don't we go with the second approach. Try the following to get the scripts to load (just a reiteration of an earlier post):

Code: Select all

<?php include('browserdetect.php'); ?> 
<?php if ($browser == 'nsmac'): ?> 
<script language="JavaScript" src="menu_array.js" "type=text/javascript"></script> 
<script language="JavaScript" src="mmenu.js" type="text/javascript"></script> 
<?php else: ?> 
<script language="JavaScript" src="milonic_src.js" type="text/javascript"></script> 
<script language="JavaScript" src="mmenudom.js" type="text/javascript"></script> 
<script language="JavaScript" src="menu_data.js" type="text/javascript"></script> 
<?php endif; ?>
And try the following in the table cell, to get the main menu to appear (with appropriate substitutions for your own menu items and menu names).

Code: Select all

<?php if ($browser == 'nsmac'): ?>
<script language="JavaScript">
PlaceMenu("mainmenu");
</script>
<?php else: ?>
<script language="JavaScript">
with(milonic=new menuname("Main Menu")){
style=menuStyle;
alwaysvisible=1;
orientation="horizontal";
position="relative";
aI("text=Home;url=http://milonic.com/;status=Back To Home Page;");
aI("text=Menu Samples;showmenu=Samples;");
aI("text=Milonic;showmenu=Milonic;");
aI("text=Partners;showmenu=Partners;");
aI("text=Links;showmenu=Links;");
aI("text=My Milonic;showmenu=My Milonic;");
}
drawMenus();
</script>
<?php endif; ?>
Let us know what happens.

Kevin
pavia
Super Advanced
Super Advanced
Posts: 39
Joined: Fri Aug 16, 2002 2:39 pm

Post by pavia »

!@$#^*@!!!

I get the following: Parse error: parse error, unexpected T_STRING in browserdetect.php on line 15:

Code: Select all

1 <?php 
2 
3 /** 
4 *  $browser will contain one of the following values: 
5 *      'iewin' : IE 4+ for Windows 
6 *      'iemac' : IE 4 for Macintosh 
7 *     'ie5mac' : IE 5 Macintosh 
8 *      'nswin' : Netscape 4.x Windows 
9 *     'nsunix' : Netscape 4.x Unix 
10 *      'nsmac' : Netscape 4.x Mac 
11 *        'ns6' : Netscape 6 / Mozilla 
12 */ 
13 
14 function inAgent($agent) { 
15    global $HTTP_USER_AGENT; 
16    $notAgent = strpos($HTTP_USER_AGENT,$agent) === false; 
17    return !$notAgent; 
18 } 
19 
20 if ( inAgent('MSIE 4') or inAgent('MSIE 5') ) { 
21  if ( inAgent('Mac') ) 
22    $browser = inAgent('MSIE 5') ? 'ie5mac' : 'ie4mac'; 
23  elseif ( inAgent('Win') ) $browser = 'iewin'; 
24 } elseif ( !inAgent('MSIE') ) { 
25  if ( inAgent('Mozilla/5') or inAgent('Mozilla/6') ) { 
26    $browser = 'ns6';  
27  } elseif ( inAgent('Mozilla/4') ) { 
28    if ( inAgent('Mac') ) $browser = 'nsmac'; 
29    elseif ( inAgent('Win') ) $browser = 'nswin'; 
30    else $browser = 'nsunix'; 
31  } 
32 } else $browser = "unknown"; 
33 
34 ?>
Well, this sucks, huh? How am I supposed to know if your hard work has paid off when this miserable browser detect script is getting in the way???
Post Reply