<?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; codes</title>
	<atom:link href="http://dallascao.com/en/tag/codes/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, 09 Apr 2012 09:52:51 +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>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/php-create-random-password/" rel="bookmark" class="crp_title">PHP: create random password</a></li><li><a href="http://dallascao.com/en/major-projects/" rel="bookmark" class="crp_title">projects</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>

