View your IP Address: (none)♦MembersHelpBookmarkNewHOT110mbAlexa Ranking

Vietnamese | English

StudyInfoTechSecurityEntertainmentTourismCareerSocialBusinessScience
World Times♦Vietnam:
♦California-USA:

Conditional Expressions of SSI



Products / Services

SSI allows conditional processing and provides a number of elements for this purpose.

The basic flow control elements are:

<!--#if expr="condition" -->
<!--#elif expr="condition" -->
<!--#else -->
<!--#endif -->

The #IF element works like an IF statement in programming. The test condition is evaluated and if the result is true then the text/lines following the IF statement is returned in the output until either on #ELIF, #ELSE or #END element is reached.

The #ELIF and #ELSE elements are used to output text/lines if the original #IF statement was false. If #ELIF is parsed the SSI will continue until either an #ELSE or #END element is reached, and if an #ELSE statement is parsed the SSI will continue until an #END element is reached.

The #END element closes the #IF element and must always be present or an error will likely occur.

The condition from the flow control code above is one of the following:

string1
string1 = string2
string1 != string2
string1 < string2
string1 > string2
string1 >= string2
(condition)
!condition
condition1 && condition2
condition1 || condition2
If string NOT empty result is true
If string1 is equal to string2 result is true
If string1 is NOT equal to string2 result is true
If string1 is less than string2 result is true
If string1 is greater than string2 result is true
If string1 greater than or equal to string2 result is true
If condition TRUE result is true
If condition FALSE result is true
If condition1 AND condition2 are true result is true
If condition1 OR condition2 are true result is true

"=" and "!=" bind more tightly than "&&" and "||".
"!" binds most tightly.

Anything that is not recognisable as a variable or an operator is treated as a string. To ensure your variable is treated as a variable you should use the format ${var_name}

If a string is written as /string/ it will be treated as a regular expression so to test something of the form /home/path you will need to use an escape sequence \"/home/path\".

Using the above information you can create quite complex tests. Here's a simple example to get you started:

<!--#if expr="(${HTTP_USER_AGENT} = /Mozilla\/4/) && 
(${HTTP_USER_AGENT} != /MSIE/)" -->
	Firefox styles
<!--#elif expr="(${HTTP_USER_AGENT} = /Mozilla\/4/) && 
(${HTTP_USER_AGENT} = /MSIE/)" -->
	<!--#if expr="${DOCUMENT_URI}=/flash-site/" -->
	Flash MSIE styles
	<!--#elif expr="${DOCUMENT_URI}=/plain-site/" -->
	Plain MSIE styles
	<!--#endif-->
<!--#else-->
You must be using Opera or Chrome ?
<!--#endif-->

You can also set variables (in Apache 1.2 and later), then conditionally display data, such as printing tags only if the browser is Internet Explorer.

<!--#set var="Agent" value="${HTTP_USER_AGENT}" -->
<!-- Print it -->
<!--#echo var="Agent" -->

Check the variable to see if it contains "MSIE", and if true, print today's date, else print some text that says it's not MSIE.

<!--#if expr="$Agent = /MSIE/" -->
<!--#echo var="DATE_GMT" -->
<!--#elif expr="$Agent != /MSIE/" -->
<!-- You are not using MSIE -->
<!--#endif-->

When testing expressions, the usual escape rules apply. For example, if you were looking to match a string the contains the dollar sign, you would need to escape the dollar sign with a backslash.

<!--#if expr="$a = \$" -->

To disambiguate variables that could be interpreted in more ways than one (because the dollar sign is not required in variables and may be treated as a string), you can use the dollar sign and braces as in:

<!--#set var="UserAgent" value="Your Browser is ${HTTP_USER_AGENT}" -->

The basic flow control for if,else conditions (available in Apache 1.2 and later) is as follows:

<!--#if expr="condition" -->
<!--#elif expr="condition" -->
<!--#else-->
<!--#endif-->

The if condition is evaluated and if true, then any text until between it and the next condition is printed to the web page. The elif or else conditions (both optional) are be used the print text text to the web page if the if was not met. The endif condition terminates the conditional block and is required.

Conditions have the following meanings and can use any of the following operators:

<!--#if expr="$Var" --> True if $Var has any value at all.

<!--#if expr="$Var = hello" -->. True if $Var equal "hello". Keep in mind, that if the string you're trying to match has a space, you must quote and escape the string as in.

<!--#if expr="$Var = \"hello there\"" --> or use single (nested) quotes without escapes. Note that if using single quotes, they must be nested within double quotes.

<!--#if expr="$Var != hello" -->. True if $Var does not equal "hello".

<!--#if expr="$Var = /hello/" -->. True if $Var contains "hello" anywhere within the entire value of the variable. Similar to Perl and Javascript regular expressions.

<!--#if expr="$Var = /hello/ && $Var = /there/" -->. True if $Var contains both "hello" and "there" anywhere within the entire value of the variable.

<!--#if expr="$Var = /hello/ || $Var = /there/" -->. True if $Var contains "hello" or "there" anywhere within the entire value of the variable.

If you're familiar with Unix, Perl or JavaScript programming and regular expressions, feel free to experiment with other operators you're aware of such as parenthesis. There are many similarities in regular expression syntax that apply to these languages.

View more... 
Select Vietnamese to find other information or service from Vietnamese site(s).

Email to your friendEmail to usGet updates by EmailView updates on WebFollow us on TwitterGoogle Safe BrowsingMcAfee SiteAdvisorDiagnostic this pageNoreton SafeWebDrWebTechnorati LinksSave to del.icio.usAdd to del.icio.usDigg This!Share on Facebookoutside.in: geotag this storyDiscuss on NewsvineStumble It!Add to Mixx!

If you would like to submit your website for FREE or advertise your own webpage(s) on our webpages, please enter Terms of Service webpage to send your request. Thank you!

©2004- ESI Portal ™®

Products / Services

(\__/)
(=^.^=)
(")_(")
ESI Portal
HOME
Top News
Online Tools
Online Translator
WYSIWYG Editor
Source Code Editor
Real Time Tool
2D Map Tool
3D Map Tool
Forex News
Online Shopping
Watch Videos
Listen Songs
Find Jobs
For Webmasters
About Us
Privacy Policy
Terms of Service
Contact Us
Live chat with us