APEX-AT-WORK no image

APEX simple cookie example

Von Tobias Arnhold 5.09.2012
This is an example for the use of a simple cookie which gets created after submit when click on a button. Afterwards a region inside my page shouldn't be shown anymore. The cookie itself is valid for 30 days and will save the value "1".

Page process:
begin
 owa_util.mime_header('text/html', FALSE);
 owa_cookie.send(
     name=>'APEX_AT_WORK_COOKIE',
     value=>'1',
     expires => sysdate + 30);

 apex_application.g_unrecoverable_error := true;
 owa_util.redirect_url('f?p=&APP_ID.:12:&SESSION.');
 exception when others then null;
end;
Update code from Patrick (for more information check the comments):
begin
owa_util.mime_header('text/html', FALSE);
owa_cookie.send(
name => 'APEX_AT_WORK_COOKIE',
value => '1',
expires => sysdate + 30 );

apex_util.redirect_url (
p_url => 'f?p=&APP_ID.:1:' || :SESSION,
p_reset_htp_buffer => false );
end;
The condition checks for the cookie and reads the value. If it is NULL then the region will be displayed.
Region Condition:
-- Type: PL/SQL Function Body Returning a Boolean
DECLARE
 cookie  OWA_COOKIE.cookie;  
BEGIN

   cookie := OWA_COOKIE.get('APEX_AT_WORK_COOKIE');

   if cookie.vals.First IS NULL then 
    return true;
   else 
    return false; 
   end if;

END; 
Documentation:
http://docs.oracle.com/cd/B14099_19/web.1012/b15896/pscook.htm

APEX login example for more then one application:
http://www.oracle.com/webfolder/technetwork/de/community/apex/tipps/apex-mini-sso/index.html Update 19.01.2015
I case you use the cookie technique with the safari browser. Be aware that the first character is not allowed to be a sepecial character like space or comma. Otherwise the cookie stays emtpy.

Post Tags:

5 Comments " APEX simple cookie example "

Patrick Wolf 11 May, 2012 09:40

Hi Tobias,

the undocumented

apex_application.g_unrecoverable_error := true;

shouldn't be used anymore. Use

apex_application.stop_apex_engine;

instead which raises a "stop" exception. For the redirect you can also use

apex_util.redirect_url (
p_url in varchar2,
p_reset_htp_buffer in boolean default true )

which automatically calls stop_apex_engine internally.

Regards
Patrick

Tobias Arnhold 15 May, 2012 08:47

Hi Patrick,

I have just checked your description. Somehow the function to create the cookie acts strange (owa_cookie.send). Your solution returns correctly to the page but the cookie doesn't get created. When I add apex_application.g_unrecoverable_error := true; before apex_util.redirect_url then the cookie gets created. Any idea?

Tobias Arnhold 15 May, 2012 13:17

I have to correct myself. Only the described way inside my blog post let the cookie be created. Why is the cookie process acting so strange?

Patrick Wolf 16 May, 2012 09:10

Hi Tobias,

the following code put into a "Before Header" process is working fine for me.

begin
owa_util.mime_header('text/html', FALSE);
owa_cookie.send(
name => 'APEX_AT_WORK_COOKIE',
value => '1',
expires => sysdate + 30 );

apex_util.redirect_url (
p_url => 'f?p=&APP_ID.:1:' || :SESSION,
p_reset_htp_buffer => false );
end;

Note: I have replaced the &SESSION. substitution with a bind variable to avoid hard parses of the PL/SQL block for each new session.

Regards
Patrick

Ricardo Capuz 20 August, 2013 15:33

Hi, Patrick, i need your help, you could please check my post on OTN Community? ... https://forums.oracle.com/thread/2571464