Tuesday, February 21, 2012

Too Many Redirects When Cookies Are Not Enabled

A person (Wouter Verkuijl) was kind enough to post on my blog, not only the problem he encountered when accessing Oracle Learning Library mobile from his iPad, but also the actual problem.  If you access Oracle Learning Library (or any APEX 4.1 application) from a browser which does not accept cookies, you'll see the URL changing in the browser a number of times and then you'll get some nice message similar to:



Of course, the Oracle Learning Library team then asked me "Any ideas on how we can trap the 310 HTTP error code (too many redirects) and provide a user-friendly message (e.g. Enable cookies) on how to resolve the problem?"  As it turns out, it's actually quite easy.

  1. The first step is ensuring that the page that is being accessed has the page attribute 'Authentication' set to 'Page is Public' instead of 'Page Requires Authentication' (thanks Christian).  Even if you are using an application which has an authentication scheme of "No Authentication (using DAD)", the specific page attribute 'Authentication' must still be set to 'Page is Public'.  This will stop APEX from doing a wild number of redirects if cookies are not enabled.
  2. Detecting if Cookies are Enabled in the Browser
    • Our preferred approach (thanks Anthony) is to do this via a dynamic action on the public page with the following attributes:
      • Advanced
      • Name: Cookies not enabled
      • Event: Page Load
      • Condition: JavaScript expression
      • Value: !navigator.cookieEnabled
      • True Action: Alert
      • Text: Cookies are not enabled! 
      • No False Action, leave blank.
    • Alternatively, you could solve this via JavaScript in the page header, with something as simple as the following code in the page attribute "Execute when Page Loads":
if (!(navigator.cookieEnabled)) {alert('Cookies are not enabled');}

By doing this via a dynamic action, you get the benefit of not only having the alert message be translatable, but this is also much easier to find and maintain in your application instead of having JavaScript sprinkled around your application.

I have to point out that you will need Application Express 4.1.1 to achieve this.  Application Express 4.1.1 was released on February 21, 2012.  The Oracle Learning Library team has already updated their application to take advantage of this.

Oracle Application Express 4.1.1 now available

Oracle Application Express 4.1.1 is now released and available for download.  If you wish to download the full release of Application Express 4.1.1, you can get it from the Downloads page on OTN.  If you have Application Express 4.1 already installed, then you need to download the APEX 4.1.1 patch set from My Oracle Support.  Look up patch number 13331096.

As is stated in the patch set note that accompanies the Oracle Application Express 4.1.1 patch set:

  • If you have Oracle Application Express release 4.1 installed, download the Oracle Application Express 4.1.1 patch set from My Oracle Support and apply it.  Remember - patch number 13331096.
  • If you have Oracle Application Express release 4.0 or earlier installed (including Oracle HTML DB release 1.5), download and install the entire Oracle Application Express 4.1.1 release from the Oracle Technology Network (OTN).
  • If you do not have Oracle Application Express installed, download and install the entire Oracle Application Express 4.1.1 release from the Oracle Technology Network (OTN).

There are a large number of issues corrected in the Application Express 4.1.1 patch set.  You can see the full list in the patch set note.  Equally important, the Oracle Application Express 4.1.1 release has incorporated much of the infrastructure necessary for the Oracle Database Cloud service, coming sometime later.

For those of you (like my friend Dietmar Aust) who wait until the first patch set is released, this is your opportunity.