Perl CGI and Javascript Cookies Primer Tutorial

Perl CGI and Javascript Cookies Primer Tutorial

Perl CGI and Javascript Cookies Primer Tutorial

Today we follow up on yesterday’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 CGI and Javascript.

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.

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’t annoy the user, but rather make it easier for them, instead of obstructing the user achieving what they intend to do.

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:

  • the form will use the POST method
  • the GET method can be used to change language codes when multiple languages are of interest

We do redirection again to the Joomla “My Favourites” website at RJM Programming, and enhance its internal three languages of use, to allow Google Translate to encase it in its webpage translation mode of use, as we have discussed during HTML/Javascript Internationalization Primer Tutorial.

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):

  • Live Run redirecting to Joomla (CMS) “My Favourites” website
  • Live Run (ignoring cookies) redirecting to Joomla (CMS) “My Favourites” website
  • Thanks for all the help with relevant links below:

    To find out more about Joomla CMS visit its home page, read about Content Management Systems or try various test websites here.

    Will (almost) leave you with some downloadable programming Perl source code you could call language_cookie.cgi supervised by some HTML code containing the calling form called picklanguage_cookie.html


    Did you know? … or … By the way

    The little “Eat at Joes” bizzo with the tutorial picture today is an example of Javascript embedded into the blog post as featured at Javascript Thumbnails in Your WordPress Post Primer Tutorial, and involved:

    • Giving the image an id (don’t we all crave for this) … made it “pcimage”
    • Embedded into the blog post
      <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>
    • 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 … refer to this link perhaps.

      Another approach to this “look” are animated GIFs, and for this would recommend Gifpal and you can catch up on some tutorials about Gifpal here.


      Yesterday’s relevant Perl CGI Redirect Primer Tutorial is shown below.

      Perl CGI Redirect Primer Tutorial

      Perl CGI Redirect Primer Tutorial

      Today we venture into the world of CGI (Common Gateway Interface) 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.

      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’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 Google Translate page (thanks) that takes the language picked by the user and welcomes you to the tutorial.

      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 Perl and CGI for the World Wide Web by Elizabeth Castro … ta muchly.

      The Perl actually does a <meta … > type of redirect 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’ll type “Applescript” as “AppleScript” then you could have an AppleScript directory that only contains index.html which virtually only contains a <meta … > tag type of redirect to the Applescript directory where the real logic resides).

      Have a feeling that such redirects annoy the search engines, and this may be an overriding consideration for you, regarding this.

      You may ask … can’t all this be done in Javascript? Mostly, yes, but Javascript can’t get you the server time without a lot of trouble.

      So, yes, CGI is that powerful interface to the server … server side logic … so write code to use it when Javascript (in its usual client-side guise) cannot help you.

      Typically CGI is often accessed in the action part of an HTML form tag definition, as for our (live) tutorial today.

      Will leave you with some downloadable programming Perl source code you could call language.cgi supervised by some HTML code containing the calling form called picklanguage.html

      If this was interesting you may be interested in this too.


      If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>