LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2001, 06:17 PM   #1
mrl
Member
 
Registered: Apr 2001
Posts: 37

Rep: Reputation: 15

Hi Folks,









I'm trying to learn SSI on apache at the moment for fun and would like the browser to display an icon/graphic depending on the users OS









I thought I could use SSI to tell me the browser and OS by using









<!--#echo var="HTTP_USER_AGENT" -->









resulting in..









Mozilla/4.0 (compatible; MSIE 5.0; Linux 2.4.2-2 i686) Opera 5.0 [en]









Instead of telling the user which browser they are using I would like to dispay a seperate image for Linux and windoze users (penguin and devil).









I geuss I would have to character match to find 'Linux' or windows in the HTTP_USER_AGENT variable and then do an If / else thing to select the image.









Could anybody point me in the right direction or suggest how I could do this in bash or perl.






this is what I have come up with so far, it is VERY untidy and does not work but is the product of an evening playing.
I have just used text to experiment with but will use images when / if it does work


<HTML>

<HEAD>

<TITLE>Stupid Page</TITLE>

</HEAD>

<body>



<!--#if expr="${HTTP_USER_AGENT} = /Linux/" -->

<!--#set var="os" value="linux" -->



<!--#elif expr="${HTTP_USER_AGENT} = /linux/" -->

<!--#set var="os" value="linux" -->



<!--#elif expr="${HTTP_USER_AGENT} = /X11/" -->

<!--#set var="os" value="linux" -->



<!--#elif expr="${HTTP_USER_AGENT} = /Konqueror/" -->

<!--#set var="os" value="linux" -->



<!--#elif expr="${HTTP_USER_AGENT} = /Windows 98/" -->

<!--#set var="os" value="win" -->



<!--#elif expr="${HTTP_USER_AGENT} = /Windows 95/" -->

<!--#set var="os" value="win" -->



<!--#else -->

<!--#set var="os" value="unknown" -->

<!--#endif -->







<!--#if expr="$os = /linux/" -->

<B>Good Stuff, Linux is so so kewl!<B>



<!--#elif expr="$os = /netscape/" -->

<B>Prepare to Blue Screen, your using Windoze</B>



<!--#else -->

Hmm, Must be a Mac or something

<!--#endif -->



</body>

</HTML>










Cheers









Mike






[Edited by mrl on 05-29-2001 at 07:09 PM]
 
Old 05-29-2001, 07:21 PM   #2
doodah
Member
 
Registered: Apr 2001
Location: Columbus,OH
Distribution: slackware
Posts: 122

Rep: Reputation: 15
if you know perl...write a block like this for a cgi...

Code:
#!/usr/bin/perl

$browserIs = $ENV{'HTTP_USER_AGENT'}

if($browserIs =~ m/MSIE/gi){
print "LOCATION_OF_DEVIL_PIC";
}
else{
print "LOCATION_OF_PENGUIN_PIC";
}
the print will print the path of the pic, just as if you were hardcoding it in the html file.. ex.

<img src="<!--#exec cgi="PATH_OF_YOUR_CGI" -->">

you will want to change that regex to detect win** or linux or whatever 'HTTP_USER_AGENT' prints for different os's...
 
Old 05-29-2001, 07:32 PM   #3
doodah
Member
 
Registered: Apr 2001
Location: Columbus,OH
Distribution: slackware
Posts: 122

Rep: Reputation: 15
hey you changed that while i was writing! if you want to have different things printed all throughout the page, i would just script the entire page in perl.. perl would be a lot less expensive on the performance end(and alot prettier!).. if you have any questions on perl jsut ask.....
 
Old 05-30-2001, 08:47 PM   #4
mrl
Member
 
Registered: Apr 2001
Posts: 37

Original Poster
Rep: Reputation: 15
Sorry 'bout that!

Hi Doodah



That's just like me to change things in the middle of something



Quote:
. if you have any questions on perl just ask.....


You may regret that!! I have been trying to learn Perl for a while (that is what got me in to Linux) but I am not mathematical or good with computers (I'm a Nurse/Medic IRL, so this is for Kicks) - this character matching has me stumped. I have read a fair bit on it and can 'read' simple scripts to a certain extent but can not write anything yet. The problem I have is I can not translate the character matching code at all.
This has stopped me learning more about perl, I just need to get a handle on it (which is why I started with the SSI thing).


if($browserIs =~ m/MSIE/gi)

i understand that this is looking to match 'MSIE' string. Could you explain what the other characters mean and how I could apply these to extract the OS.



Any help greatly appreciated.



Cheers

Mike
 
Old 05-31-2001, 08:29 PM   #5
doodah
Member
 
Registered: Apr 2001
Location: Columbus,OH
Distribution: slackware
Posts: 122

Rep: Reputation: 15
Code:
if($browserIs =~ m/MSIE/gi)
im assuming you know the if() and that $browserIs is a scalar...

the '=~' is the comparison operator for regexes(regular expressions)... in this case it says

look in $browserIs for-(whatever is on the other side of =~)

the 'm/MSIE/gi' bit says to

MATCH the expression 'MSIE' in whatever is on the other side of '=~'... the 'gi' means global and (case)insensitive... if it finds it, the if() gets a 1 if not, a 0...(well not actually a 1 or 0, but it works for this example)

to get the os you will need to test to see what 'HTTP_USER_AGENT' returns for different os's.. cuz i dont know... but for example if it returned 'linux' just do a block like...

Code:
#!/usr/bin/perl

$browserIs = $ENV{'HTTP_USER_AGENT'}

if($browserIs =~ m/linux/gi){
print "LOCATION_OF_PENGUIN_PIC";
}
else{
print "LOCATION_OF_DEVIL_PIC";

}
this is pretty simple use of regex's, because you know basically what 'HTTP_USER_AGENT' is returning always... but it can get ALOT more complex for other operations...i will answer any questions, no prob!.. but perl is deep, and powerful, i first learned from the 'camel book'(O'Reilly, Programming Perl)... i would recommend that if you are serious about learning Perl... but dont hesitate to ask....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Devil-Linux cd firewall mohapi Linux - Security 6 07-13-2005 07:46 AM
Devil Linux webwolf70 Linux - Distributions 2 05-09-2005 10:24 PM
Devil Linux and webapps momilla Linux - Software 0 10-22-2004 04:30 AM
Devil-Linux OMEGA-DOOM Linux - Newbie 2 09-03-2004 05:51 AM
devil-linux and pentium mmx zioponics Linux - Distributions 0 10-10-2003 06:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration