{"id":7422,"date":"2014-04-25T05:07:11","date_gmt":"2014-04-24T19:07:11","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=7422"},"modified":"2014-04-25T05:07:11","modified_gmt":"2014-04-24T19:07:11","slug":"perl-cgi-and-javascript-cookies-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/perl-cgi-and-javascript-cookies-primer-tutorial\/","title":{"rendered":"Perl CGI and Javascript Cookies Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Perl_CGI_Joomla_Redirect.jpg\"><img decoding=\"async\" id='pcimage' style=\"float:left;border: 15px solid pink;\" alt=\"Perl CGI and Javascript Cookies Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Perl_CGI_Joomla_Redirect.jpg\" title=\"Perl CGI and Javascript Cookies Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Perl CGI and Javascript Cookies Primer Tutorial<\/p><\/div>\n<p>Today we follow up on yesterday&#8217;s relevant <a target=_blank title='Perl CGI Redirect Primer Tutorial' href='#pcrpt'>Perl CGI Redirect Primer Tutorial<\/a> to venture further into the world of CGI (<a target=_blank title='CGI information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Common_Gateway_Interface'>Common Gateway Interface<\/a>) on our Apache\/PHP\/MySql web server.  In our <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Perl_CGI_Joomla_Redirect.jpg\">tutorial<\/a> today we consider the use of cookies with Perl CGI and Javascript.<\/p>\n<p><script type='text\/javascript'>var pcimageo = null; setInterval(eatAtPJoes, 3000);  function eatAtPJoes() { if (pcimageo == null) pcimageo = document.getElementById('pcimage'); pcimageo.src = pcimageo.src.replace('.jpg', '~png').replace('.png', '.jpg').replace('~','.'); } <\/script><\/p>\n<p>Cookies are a means to individualize the user interaction at a website.  Often an eCommerce website will deploy cookie-based logic to keep a track on the likes (and dislikes) of their customers, hoping that some personalization will help attract users back to their website.<\/p>\n<p>Cookies can be controlled by the user, and they can clear cookies, or refuse to allow them, and there is not much the website can nor should do about this, but a well-designed cookie usage shouldn&#8217;t annoy the user, but rather make it easier for them, instead of obstructing the user achieving what they intend to do.<\/p>\n<p>So today we try to get the user to enter their preferred language, and cater for this the next time they access that same URL.   In order to make this happen with the Perl CGI and Javascript we make use of different modes of use (often, in Perl, determined by the $ENV{} values) by saying:<\/p>\n<ul>\n<li>the form will use the POST method<\/li>\n<li>the GET method can be used to change language codes when multiple languages are of interest<\/li>\n<\/ul>\n<p>We do redirection again to the Joomla &#8220;My Favourites&#8221; website at RJM Programming, and enhance its internal three languages of use, to allow <a target=_blank title='Google Translate ... thanks' href='http:\/\/translate.google.com'>Google Translate<\/a> to encase it in its webpage translation mode of use, as we have discussed during <a target=_blank title='HTML\/Javascript Internationalization Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=3954'>HTML\/Javascript Internationalization Primer Tutorial<\/a>.<\/p>\n<p>Here are the links to test this for yourself (where the word yourself is apt, because another user may not see things the way you do):<\/p>\n<ul>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/picklanguage_cookie.html\">Live Run redirecting to Joomla (CMS) &#8220;My Favourites&#8221; website<\/a><\/li>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/picklanguage_cookie.html?nocookies=y\">Live Run (ignoring cookies) redirecting to Joomla (CMS) &#8220;My Favourites&#8221; website<\/a><\/li>\n<\/li>\n<p><\/p>\n<p>Thanks for all the help with relevant links below:<\/p>\n<ul>\n<li><a target=_blank title='How to Obtain SERVER_NAME  Perl Server Side CGI Scripting forum at WebmasterWorld' href='http:\/\/www.webmasterworld.com\/forum13\/2308.htm'>How to Obtain SERVER_NAME  Perl Server Side CGI Scripting forum at WebmasterWorld<\/a><\/li>\n<li><a target=_blank title='Logic for \"one year from now\" to set the expiry date of cookie' href='http:\/\/www.iro.umontreal.ca\/~lapalme\/ift3225\/Dynamique\/showXML.cgi?preferences\/preferences.cgi'>Logic for &#8220;one year from now&#8221; to set the expiry date of cookie<\/a><\/li>\n<li><a target=_blank title='query string - How to obtaining the querystring from the current URL with JavaScript? - Stack Overflow' href='http:\/\/stackoverflow.com\/questions\/9870512\/how-to-obtaining-the-querystring-from-the-current-url-with-javascript'>query string &#8211; How to obtaining the querystring from the current URL with JavaScript? &#8211; Stack Overflow<\/a><\/li>\n<li><a target=_blank title='Working with CGI.pm In-Depth - How to set a cookie' href='http:\/\/perl.about.com\/od\/cgiweb\/a\/set_cookie.htm'>Working with CGI.pm In-Depth &#8211; How to set a cookie<\/a><\/li>\n<\/ul>\n<p><\/p>\n<p>To find out more about <a target=_blank title='Joomla' href='http:\/\/joomla.org'>Joomla<\/a> CMS visit its home page, read about <a target=_blank title='CMS information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Content_management_system\u200e'>Content Management Systems<\/a> or try various test websites <a target=_blank title='Try eCommerce, CMS, Blog, Forum, CRM, Ticketing, Education' href='http:\/\/www.rjmprogramming.com.au\/About_Us.html'>here<\/a>.<\/p>\n<p><\/p>\n<p>Will (almost) leave you with some downloadable programming Perl source code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/language_cookie.cgi_GETME\" title='language_cookie.cgi'>language_cookie.cgi<\/a> supervised by some HTML code containing the calling form called <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/picklanguage_cookie.html_GETME\" title='picklanguage_cookie.html'>picklanguage_cookie.html<\/a><\/p>\n<p><span><\/p>\n<h4><b>Did you know?<\/b> &#8230; or &#8230; <b>By the way<\/b><\/h4>\n<p><\/span><\/p>\n<p>The little &#8220;Eat at Joes&#8221; bizzo with the tutorial picture today is an example of Javascript embedded into the blog post as featured at  <a target=_blank title='Javascript Thumbnails in Your WordPress Post Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=7306'>Javascript Thumbnails in Your WordPress Post Primer Tutorial<\/a>, and involved:<\/p>\n<ul>\n<li>Giving the image an id (don&#8217;t we all crave for this) &#8230; made it &#8220;pcimage&#8221;<\/li>\n<li>Embedded into the blog post <br \/><i>&lt;p&gt;&lt;script type=&#8217;text\/javascript&#8217;&gt;var pcimageo = null; setInterval(eatAtPJoes, 3000); function eatAtPJoes() { if (pcimageo == null) pcimageo = document.getElementById(\u2018pcimage\u2019); pcimageo.src = pcimageo.src.replace(\u201c.jpg\u2033, \u201c~png\u2033).replace(\u201c.png\u2033, \u201c.jpg\u2033).replace(\u201c~\u201d,\u201d.\u201d); } &lt;\/script&gt;&lt;\/p&gt;<br \/>\n<\/i><\/li>\n<p><\/p>\n<p>If this embedding feels odd to you, that is a natural enough reaction, but bear in mind that (Javascript) Bookmarklets all work in this similar vein &#8230; refer to <a target=_blank title='Bookmarklet tutorials here' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Bookmarklet'>this link<\/a> perhaps.<\/p>\n<p>Another approach to this &#8220;look&#8221; are animated GIFs, and for this would recommend <a target=_blank title='Gifpal' href='http:\/\/gifpal.com'>Gifpal<\/a> and you can catch up on some tutorials about Gifpal <a target=_blank title='Bookmarklet tutorials here' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Gifpal'>here<\/a>.<\/p>\n<hr \/>\n<p id='pcrpt'>Yesterday&#8217;s relevant <a target=_blank title='Perl CGI Redirect Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=7405'>Perl CGI Redirect Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/picklanguage.html\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"Perl CGI Redirect Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Perl_CGI_Redirect.jpg\" title=\"Perl CGI Redirect Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Perl CGI Redirect Primer Tutorial<\/p><\/div>\n<p>Today we venture into the world of CGI (<a target=_blank title='CGI information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Common_Gateway_Interface'>Common Gateway Interface<\/a>) on our Apache\/PHP\/MySql web server.   So what language is default for CGI work on such a web server?   It could be Perl or Python, or maybe something else like Peas, Poodles or Pomegranates, though the last three should be likely, but, alas, are not probable.   On our CentOS web server the default CGI is Perl.<\/p>\n<p>Care is needed with CGI work, as you are opening up a program that can get to your Operating System to access from public Internet places.   So in the code you&#8217;ll see below there is a check for where the user came from, as to whether the access should be allowed or not to the real thrust of what the Perl CGI does, and that is to redirect you to a <a target=_blank title='Google Translate ... thanks' href='http:\/\/translate.google.com'>Google Translate<\/a> page (thanks) that takes the language picked by the user and welcomes you to the tutorial.<\/p>\n<p>Perl CGI (or any CGI) should be given the proper stringent permissions, and any advice given by your hoster may need to be adhered to.   With this, as with all Perl things, like <i>Perl and CGI for the World Wide Web<\/i> by Elizabeth Castro &#8230; ta muchly.<\/p>\n<p>The Perl actually does a &lt;meta &#8230; &gt; type of <a target=_blank title='Meta tag redirect advice ... thanks' href='http:\/\/stackoverflow.com\/questions\/12743038\/redirecting-to-another-page-meta-tag'>redirect<\/a> available to all (ie. mere mortal plus CGI-less-(or-is-that-more?)-mere mortal) HTML up in its header.   This technique can be good to handle the same functionality at multiple places on a Unix\/Linux web server (where uppercase\/lowercase matters) regarding guesses by the user as to URLs (eg. if you expect users to try typing in the URLs themselves and there is just as much likelihood they&#8217;ll type &#8220;Applescript&#8221; as &#8220;AppleScript&#8221; then you could have an AppleScript directory that only contains index.html which virtually only contains a &lt;meta &#8230; &gt; tag type of redirect to the Applescript directory where the real logic resides).<\/p>\n<p>Have a feeling that such redirects annoy the search engines, and this may be an overriding consideration for you, regarding this.<\/p>\n<p>You may ask &#8230; can&#8217;t all this be done in Javascript?   Mostly, yes, but Javascript can&#8217;t get you the server time without a lot of trouble.<\/p>\n<p>So, yes, CGI is that powerful interface to the server &#8230; server side logic &#8230; so write code to use it when Javascript (in its usual client-side guise) cannot help you.<\/p>\n<p>Typically CGI is often accessed in the action part of an HTML form tag definition, as for our (<a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/picklanguage.html\" title='Click picture.'>live<\/a>) <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Perl_CGI_Redirect.jpg\" title='Tutorial picture.'>tutorial<\/a> today.<\/p>\n<p>Will leave you with some downloadable programming Perl source code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/language.cgi_GETME\" title='language.cgi'>language.cgi<\/a> supervised by some HTML code containing the calling form called <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/picklanguage.html_GETME\" title='picklanguage.html'>picklanguage.html<\/a><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d7405' onclick='var dv=document.getElementById(\"d7405\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Perl\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d7405' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d7422' onclick='var dv=document.getElementById(\"d7422\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=cookies\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d7422' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today we follow up on yesterday&#8217;s relevant Perl CGI Redirect Primer Tutorial to venture further into the world of CGI (Common Gateway Interface) on our Apache\/PHP\/MySql web server. In our tutorial today we consider the use of cookies with Perl &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/perl-cgi-and-javascript-cookies-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[196,264,327,375,513,532,576,620,652,660,916,997,1319],"class_list":["post-7422","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-cgi","tag-cookies","tag-did-you-know","tag-ecommerce","tag-google","tag-google-translate","tag-html","tag-internationalization","tag-javascript","tag-joomla","tag-perl","tag-programming","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/7422"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=7422"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/7422\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=7422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=7422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=7422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}