Page 1 of 1

Custom Cookies?

Posted: Wed Aug 15, 2007 8:40 pm
by zgray
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

Re: Custom Cookies?

Posted: Wed Aug 22, 2007 12:42 am
by soup a loignon y crouton
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.

Re: Custom Cookies?

Posted: Wed Aug 22, 2007 12:37 pm
by bascherz
@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!

Re: Custom Cookies?

Posted: Wed Aug 22, 2007 5:42 pm
by Chris Davenport
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.

Re: Custom Cookies?

Posted: Tue Aug 28, 2007 10:03 am
by kerry
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.