Custom Cookies?

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
zgray
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Aug 09, 2007 7:55 pm

Custom Cookies?

Post by zgray » Wed Aug 15, 2007 8:40 pm

I'm working on a project using JSP/Tomcat in conjunction with Apache and Joomla!, and I would like Joomla! to write a custom cookie containing the session ID (encrypted with MY algorithms, so I can decipher it in my JSP apps) to the web user's workstation every time they log in.

Then my JSP app retrieves the cookie and pulls the information it needs out of it, using session_id to find the currently logged in user, and forward that info to the login script for an online ordering mechanism. The JSP runs in a Joomla! wrapper, and works great, but I need to add the custom cookie via Joomla.

I looked around and couldn't find anything about cookies, so I'm posting here. I added a setcookie() call in joomla.php, with the other setcookie() commands, but it isn't working. I checked my cookies folders manually in both IE and Firefox, and also ran my java programs, but the cookie definitely isn't being written.

I'm not necessarily asking for code, I just need to know where to edit the Joomla source to get the results I'm looking for.



Thanks

soup a loignon y crouton
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Tue Aug 21, 2007 12:38 am

Re: Custom Cookies?

Post by soup a loignon y crouton » Wed Aug 22, 2007 12:42 am

if (!isset($_COOKIE["name"])) {

  setcookie("name", $_POST["name"]);
  setcookie("visits", 1);

  echo "Hello $_POST[name]. <br />";
  echo "It appears that this is your first visit!";

} else {
  setcookie("visits", ++$_COOKIE["visits"]);

  echo "Welcome back, $_COOKIE[name]. <br />";
  echo "You have visited us $_COOKIE[visits] times!";
}
?>

The two points of interest are $_COOKIE and setcookie.

Cookie information sent by the browser is accessed with $_COOKIE, a super global array containing cookie values keyed by name.

The setcookie function is used to assign a key/value pair to be sent back to the client. The client uses the new values to update its cookies if the information already exists. If the information doesn't exist, new cookies will be created.

Remember that cookie information is exchanged within HTTP headers; cookies must be sent before the script generates any output.
Joomla : so easy , a CEO can do it.

User avatar
bascherz
Joomla! Intern
Joomla! Intern
Posts: 86
Joined: Mon Jan 16, 2006 1:33 am
Location: Vienna, VA
Contact:

Re: Custom Cookies?

Post by bascherz » Wed Aug 22, 2007 12:37 pm

@soup,

That appears to be a general PHP answer to a Joomla-specific question...I think. I believe zgray is wondering where in the Joomla (1.5) core is the code that sets and checks cookies for Joomla. In 1.0.x, I believe it was in joomla.php, but things have changed a bit for 1.5.

@zgray, you should be able to look for $_COOKIE in the 1.5 code if you download the latest RC package. Maybe one of the devs will lend a hand here.

Please post your findings here for the benefit of the rest of us.  :)

Thanks!
__________________
Bruce Scherzinger

User avatar
Chris Davenport
Joomla! Intern
Joomla! Intern
Posts: 95
Joined: Thu Aug 18, 2005 8:57 am
Location: Shrewsbury, Shropshire, United Kingdom

Re: Custom Cookies?

Post by Chris Davenport » Wed Aug 22, 2007 5:42 pm

I'm not too familiar with this aspect of the code, but I think you need to look at JSession: http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:session:jsession/

Regards,
Chris.
Joomla! Core Team Member | Documentation Working Group Coordinator

"Reality is merely an illusion, although a very persistent one" - Albert Einstein
"We are suspended in language such that we don't know what is up and what is down" - Niels Bohr

kerry
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Feb 17, 2006 6:03 am

Re: Custom Cookies?

Post by kerry » Tue Aug 28, 2007 10:03 am

interesting.  i am just doing the same. 

in joomla1.5 the code would go into a  "user" plug-in.  have a look into /plug-in/user/example.php.  i don't know much about php but i do have it working.  my code designed to work with a apache module mod_auth_tkt.  it uses a cookie to set the apache remote_user variable.  thus it log a Jommla user into apache.  use md5 and a secret key to create encrypted check of the data.  i would be nice to encrypt the data somehow that could be unencrypted. i have to look at that next.  i need to get some ldap stuff working first.  i want to allow a user to reset their password that stored in a ldap DB.   

something like setcookie("cookiename","cookiedata", time()+10) 
i used Google and search for "php setcookie" 

firefox has a nice option under tools/options/privacy/show cookies.  also there a firefox add on called "livehttpheaders" which is a trace tool. it displays all the header info to/form a web site.  including the cookie traffic. 
 

i found if you don't set a time value the cook deletes it self straight away.

i going to put the code up somewhere when i finish and find out haw to get it onto one of the extensions sites.
 


Post Reply