<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DALLASCAO.COM &#187; php</title>
	<atom:link href="http://dallascao.com/en/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://dallascao.com/en</link>
	<description>Site of Dallas Cao, English to Chinese translator</description>
	<lastBuildDate>Mon, 23 Jan 2012 11:37:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Change your php and mysql timezone</title>
		<link>http://dallascao.com/en/change-your-php-and-mysql-timezone/</link>
		<comments>http://dallascao.com/en/change-your-php-and-mysql-timezone/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 16:06:49 +0000</pubDate>
		<dc:creator>Dallas</dc:creator>
				<category><![CDATA[Coding Frenzy]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://dallascao.com/en/?p=789</guid>
		<description><![CDATA[date_default_timezone_set('PRC'); Changes the timezone to China (+8) If you use mysql database, you will also need to use mysql_query("SET time_zone = '+8:00'"); to change the the timezone of mysql database. Related Posts:End a process with Inno setupGoogle Dictionary for Word (GD4Word)VB msgbox always on topUse cookies to remember the language version users visited last time [...]]]></description>
			<content:encoded><![CDATA[<p><code>date_default_timezone_set('PRC'); </code></p>
<p>Changes the timezone to China (+8)</p>
<p>If you use mysql database, you will also need to use</p>
<p><code>mysql_query("SET time_zone = '+8:00'");</code></p>
<p> to change the the timezone of mysql database.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://dallascao.com/en/end-a-process-with-inno-setup/" rel="bookmark" class="crp_title">End a process with Inno setup</a></li><li><a href="http://dallascao.com/en/google-dictionary-for-word-gd4word/" rel="bookmark" class="crp_title">Google Dictionary for Word (GD4Word)</a></li><li><a href="http://dallascao.com/en/vb-msgbox-always-on-top/" rel="bookmark" class="crp_title">VB msgbox always on top</a></li><li><a href="http://dallascao.com/en/use-cookies-to-remember/" rel="bookmark" class="crp_title">Use cookies to remember the language version users visited last time (php code)</a></li><li><a href="http://dallascao.com/en/homezz-com-celebrating-one-year-anniversary-of-service/" rel="bookmark" class="crp_title">Homezz.com: celebrating one-year anniversary of service</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fdallascao.com%2Fen%2Fchange-your-php-and-mysql-timezone%2F&amp;title=Change%20your%20php%20and%20mysql%20timezone"><img src="/en/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://dallascao.com/en/change-your-php-and-mysql-timezone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use regular expressions in .htaccess for redirections</title>
		<link>http://dallascao.com/en/use-regular-expressions-in-htaccess-for-redirections/</link>
		<comments>http://dallascao.com/en/use-regular-expressions-in-htaccess-for-redirections/#comments</comments>
		<pubDate>Fri, 14 May 2010 17:30:04 +0000</pubDate>
		<dc:creator>Dallas</dc:creator>
				<category><![CDATA[Coding Frenzy]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://dallascao.com/en/?p=744</guid>
		<description><![CDATA[Task one: Apache server provides a .htaccess file using which you can use to redirect URLs. For example, if you want to redirect all visitors to http://dallascao.com/cn/ to http://dallascao.com/en/, you can open the .htaccess file at the root, and then input: RewriteRule ^cn\/?$ en\/ Translating the code in English, it means &#8220;Please replace &#8216;^cn\/?$&#8217; with [...]]]></description>
			<content:encoded><![CDATA[<h2>Task one:</h2>
<p>Apache server provides a .htaccess file using which you can use to redirect URLs. For example, if you want to redirect all visitors to http://dallascao.com/cn/ to http://dallascao.com/en/, you can open the .htaccess file at the root, and then input:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">RewriteRule <span style="color: #339933;">^</span>cn\<span style="color: #339933;">/?</span>$ en\<span style="color: #339933;">/</span></pre></div></div>

<p>Translating the code in English, it means &#8220;Please replace &#8216;^cn\/?$&#8217; with &#8216;en/&#8217;&#8221;.   &#8220;^cn\/?$&#8221; is a regular expression, in which:</p>
<p>&#8220;^&#8221; matches the beginning of a string.<br />
&#8220;$&#8221; matches the end.<br />
&#8220;\&#8221; means &#8220;/&#8221; following it stands for &#8220;/&#8221; itself, not part of the grammar.<br />
&#8220;?&#8221; means there can be one &#8220;/&#8221; or no &#8220;/&#8221;</p>
<p>So  &#8220;^cn\/?$&#8221; matches two strings: &#8220;cn/&#8221; or &#8220;cn&#8221;. When visitors visit http://dallascao.com/cn/ (end with a slash) or http://dallascao.com/cn (end with no slash), they are redirected to http://dallascao.com/en/</p>
<h2>Task two:</h2>
<p>If you want to redirect all visitors to the root of http://dallascao.com/ to http://dallascao.com/en/, I can use this:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">RewriteRule <span style="color: #339933;">^</span>$ en\<span style="color: #339933;">/</span></pre></div></div>

<p>&#8220;^$&#8221; matches an empty string, meaning the root.</p>
<h2>Task three:</h2>
<p>Inputting the following code into the .htaccess of http://gt4t.net/, all visitors to http://gt4t.net/anything/ to http://gt4t.net/en/anything/. For example, visitors to http://gt4t.net/downloads/ will be redirected to http://gt4t.net/en/downloads.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">RewriteRule <span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*?</span><span style="color: #009900;">&#41;</span>\<span style="color: #339933;">/?</span>$ \<span style="color: #339933;">/</span>en\<span style="color: #339933;">/</span><span style="color: #0000ff;">$1</span></pre></div></div>

<p>&#8220;^(.*?)\/?$&#8221; means anything that either ends with one &#8220;/&#8221; or no &#8220;/&#8221;. In the second part, &#8220;\/en\/$1&#8243;, &#8220;$1&#8243; stands for the 1st bracketed element in the previous string &#8220;^(.*?)\/?$&#8221; (in this case, the only bracketed element.). And it means replacing &#8220;ANYTHING&#8221; ending with &#8220;/&#8221; or no &#8220;/&#8221; with &#8220;/en/ANYTHING/&#8221;</p>
<p>By the way, if you want your redirection to be hidden, you can add the following option to the beginng .</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">Options <span style="color: #339933;">+</span>FollowSymLinks</pre></div></div>

<p>In the example above, your visitors to http://gt4t.net/downloads is redirected to http://gt4t.net/gt4t_en/downloads/ but http://gt4t.net/downloads is still shown in the address bar.</p>
<p>Finally, here is  is the complete .htaccess file:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">RewriteEngine on
Options <span style="color: #339933;">+</span>FollowSymLinks
&nbsp;
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_HOST<span style="color: #009900;">&#125;</span> <span style="color: #339933;">^</span>gt4t<span style="color: #339933;">.</span>net$ <span style="color: #009900;">&#91;</span>OR<span style="color: #009900;">&#93;</span>
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_HOST<span style="color: #009900;">&#125;</span> <span style="color: #339933;">^</span>www<span style="color: #339933;">.</span>gt4t<span style="color: #339933;">.</span>net$
RewriteRule <span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*?</span><span style="color: #009900;">&#41;</span>\<span style="color: #339933;">/?</span>$ \<span style="color: #339933;">/</span>gt4t_en\<span style="color: #339933;">/</span><span style="color: #0000ff;">$1</span> <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span></pre></div></div>

<p>Confused? <a href="http://dallascao.com/en/contact-dallas/">Contact me</a> if you have such a need. I will be happy to help you.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://dallascao.com/en/use-of-if-fields/" rel="bookmark" class="crp_title">Use of If field in Microsoft Word</a></li><li><a href="http://dallascao.com/en/use-cookies-to-remember/" rel="bookmark" class="crp_title">Use cookies to remember the language version users visited last time (php code)</a></li><li><a href="http://dallascao.com/en/autohotkey-disable-the-close-button-x-of-a-gui-window/" rel="bookmark" class="crp_title">Autohotkey: Disable the close button [X] of a GUI window</a></li><li><a href="http://dallascao.com/en/the-context-sensitive-language-versions-link/" rel="bookmark" class="crp_title">The context sensitive Language Versions link</a></li><li><a href="http://dallascao.com/en/google-dictionary-for-word-gd4word/" rel="bookmark" class="crp_title">Google Dictionary for Word (GD4Word)</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fdallascao.com%2Fen%2Fuse-regular-expressions-in-htaccess-for-redirections%2F&amp;title=Use%20regular%20expressions%20in%20.htaccess%20for%20redirections"><img src="/en/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://dallascao.com/en/use-regular-expressions-in-htaccess-for-redirections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use cookies to remember the language version users visited last time (php code)</title>
		<link>http://dallascao.com/en/use-cookies-to-remember/</link>
		<comments>http://dallascao.com/en/use-cookies-to-remember/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 17:56:59 +0000</pubDate>
		<dc:creator>Dallas</dc:creator>
				<category><![CDATA[Coding Frenzy]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[codes]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://dallascao.com/en/?p=159</guid>
		<description><![CDATA[This site is bilingual. http://dallascao.com/cn/ is the Chinese version and http://dallscao.com/en/ is the English version. When you visit http://dallascao.com/, you are directed to either /cn/ or /en/ according to the default language of your browser or the version you visited last time. The site uses cookies to remember the version you visited last time and [...]]]></description>
			<content:encoded><![CDATA[<p>This site is bilingual. http://dallascao.com/cn/ is the Chinese version and http://dallscao.com/en/ is the English version. When you visit http://dallascao.com/, you are directed to either /cn/ or /en/ according to the default language of your browser or the version you visited last time. <span id="more-159"></span>The site uses cookies to remember the version you visited last time and direct you to that version when you visit again. If it is the first time to visit, you will open the version that matches your browser language.</p>
<p>To do this, you need to edit the header.php of the English version under the /en/ folder and add the following code before the <html><head> tag:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!--set permanent cookie --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lang&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">94608000</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;.dallascao.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This code writes a variable &#8220;lang&#8221; on your computer and assigns a value &#8220;en&#8221;. 94608000 is the expiration time (3 years). &#8220;/&#8221; and &#8220;.dallascao.com&#8221; means it works from the root directory of dallascao.com and its subdomains. Inserting this code into header.php means that when you visit any page of the English version, your visit will be remembered.</p>
<p>Likewise, you need to insert the following code into the header.php of the Chinese version.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!--set permanent cookie --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lang&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;cn&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">94608000</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;.dallascao.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>It means when you visit the Chinese version, the variable &#8220;lang&#8221; will be assigned the value of &#8220;cn&#8221;.</p>
<p>At last, Create an index.php at the root containing the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$lang</span><span style="color: #339933;">=</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;lang&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lang</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'cn'</span><span style="color: #339933;">:</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:  http://dallascao.com/cn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'en'</span><span style="color: #339933;">:</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:  http://dallascao.com/en'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">#Get the default language of the browser if no cookies are found.
</span><span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
	<span style="color: #000088;">$lang</span> <span style="color: #339933;">=</span> getDefaultLanguage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lang</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
       <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'zh-cn'</span> <span style="color: #339933;">:</span> 
               <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:  http://dallascao.com/cn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
               <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span> 
       <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> 
	          <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: http://dallascao.com/en'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
              <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span> 
		<span style="color: #009900;">&#125;</span> 
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">#########################################################
</span><span style="color: #666666; font-style: italic;"># Copyright ? 2008 Darrin Yeager                        #
</span><span style="color: #666666; font-style: italic;"># http://www.dyeager.org/                               #
</span><span style="color: #666666; font-style: italic;"># Licensed under BSD license.                           #
</span><span style="color: #666666; font-style: italic;">#   http://www.dyeager.org/downloads/license-bsd.php    #
</span><span style="color: #666666; font-style: italic;">#########################################################
</span>
<span style="color: #000000; font-weight: bold;">function</span> getDefaultLanguage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_ACCEPT_LANGUAGE&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">return</span> parseDefaultLanguage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_ACCEPT_LANGUAGE&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">else</span>
      <span style="color: #b1b100;">return</span> parseDefaultLanguage<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> parseDefaultLanguage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$http_accept</span><span style="color: #339933;">,</span> <span style="color: #000088;">$deflang</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$http_accept</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$http_accept</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;"># Split possible languages into array
</span>      <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$http_accept</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">#check for q-value and create associative array. No q-value means 1 by rule
</span>         <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/(.*);q=([0-1]{0,1}\.\d{0,4})/i&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$lang</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">else</span>
            <span style="color: #000088;">$lang</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">#return default language (highest q-value)
</span>      <span style="color: #000088;">$qval</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lang</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$qval</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qval</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$deflang</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$deflang</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://dallascao.com/en/use-regular-expressions-in-htaccess-for-redirections/" rel="bookmark" class="crp_title">Use regular expressions in .htaccess for redirections</a></li><li><a href="http://dallascao.com/en/use-of-if-fields/" rel="bookmark" class="crp_title">Use of If field in Microsoft Word</a></li><li><a href="http://dallascao.com/en/the-context-sensitive-language-versions-link/" rel="bookmark" class="crp_title">The context sensitive Language Versions link</a></li><li><a href="http://dallascao.com/en/major-projects/" rel="bookmark" class="crp_title">projects</a></li><li><a href="http://dallascao.com/en/autohotkey-disable-the-close-button-x-of-a-gui-window/" rel="bookmark" class="crp_title">Autohotkey: Disable the close button [X] of a GUI window</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fdallascao.com%2Fen%2Fuse-cookies-to-remember%2F&amp;title=Use%20cookies%20to%20remember%20the%20language%20version%20users%20visited%20last%20time%20%28php%20code%29"><img src="/en/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://dallascao.com/en/use-cookies-to-remember/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The context sensitive Language Versions link</title>
		<link>http://dallascao.com/en/the-context-sensitive-language-versions-link/</link>
		<comments>http://dallascao.com/en/the-context-sensitive-language-versions-link/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 15:39:21 +0000</pubDate>
		<dc:creator>Dallas</dc:creator>
				<category><![CDATA[Coding Frenzy]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[codes]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[string replace]]></category>
		<category><![CDATA[webmastering]]></category>

		<guid isPermaLink="false">http://dallascao.com/en/?p=127</guid>
		<description><![CDATA[Most bilingual sites only have a fixed link to the other language version. Wherever you are on one language version of a site, clicking on the Language Version link always takes you to the home page of the other version of the site. But I want my visitors get the exact page they are viewing written in the other language. I used the javascript replace method to make it work.]]></description>
			<content:encoded><![CDATA[<p>Most bilingual sites only have a fixed link to the other language version. Wherever you are on one language version of a site, clicking on the Language Version link always takes you to the home page of the other version of the site. But I want my visitors get the exact page they are viewing written in the other language. <span id="more-127"></span>For example, if my visitor is visiting</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://dallascao.com/en/2009/07/the-context-sensitive-language-versions-link/</pre></div></div>

<p>by clicking &#8220;中文&#8221; at the bottom, he is taken to</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://dallascao.com/cn/2009/07/the-context-sensitive-language-versions-link/</pre></div></div>

<p>So it involves replacing the &#8220;/en/&#8221; the URL of the current page with &#8220;/cn/&#8221;.</p>
<p>The following php code gives out wrong results:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$url_old</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'PHP_SELF'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$url_new</span><span style="color: #339933;">=</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;cn&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$url_old</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>   
&lt;a href=<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$url_new</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>Chinese&lt;/a&gt;</pre></div></div>

<p>Whichever page you are visiting, $url_old always gives the home URL &#8220;/en/index.php&#8221;. This is because I turned Permalinks on and the URL is rewritten. The URL rewritting takes place <b>after</b> the execution of PHP codes on the server side, and therefore the php string &#8220;$url_old&#8221; cannot get the actual URL as it is rewritten afterwards. The following java script can get the right current URL as java script runs on the client&#8217;s computer:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
var s<span style="color: #339933;">=</span>window.<span style="color: #006633;">location</span>.<span style="color: #006633;">href</span>
r<span style="color: #339933;">=</span>s.<span style="color: #006633;">replace</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">/</span>en<span style="color: #339933;">/</span>, <span style="color: #0000ff;">&quot;cn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
document.<span style="color: #006633;">write</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;li&gt;&lt;a title='English verion of this page' href=&quot;</span><span style="color: #339933;">+</span>r<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;&gt;Chinese&lt;/a&gt;&lt;/li&gt;&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>More information:</p>
<p><a target=_blank href="http://www.w3schools.com/jsref/jsref_replace.asp">java replace () method</a></p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://dallascao.com/en/use-of-if-fields/" rel="bookmark" class="crp_title">Use of If field in Microsoft Word</a></li><li><a href="http://dallascao.com/en/use-cookies-to-remember/" rel="bookmark" class="crp_title">Use cookies to remember the language version users visited last time (php code)</a></li><li><a href="http://dallascao.com/en/use-regular-expressions-in-htaccess-for-redirections/" rel="bookmark" class="crp_title">Use regular expressions in .htaccess for redirections</a></li><li><a href="http://dallascao.com/en/open-a-link-in-a-new-tab/" rel="bookmark" class="crp_title">Shortcut to open a link in a new tab instead of a new window</a></li><li><a href="http://dallascao.com/en/friendfeedtranslate/" rel="bookmark" class="crp_title">friendfeedTranslate: script that translate all languages on a page into one language</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://dallascao.com/en/the-context-sensitive-language-versions-link/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

