Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Cookies 101 with a twist

Posted by dabbler (leah), 7 October 2003
I've spent a couple of hours reading through various cookie pages, and I understand cookies generally. But, I am trying to translate perl script to php, and I'm getting a little lost. I'm not sure if I need to translate actually.

What I want to do is take an open source php/mysql recipe database and use yabb membership for user management and permissions, stripping out it's own login code. Testing/learning with a blank page, I can see that the yabb cookie is set, and print it's contents. So far I'm confident that I can display a page or display an authorization error based on this. However, I also need to display a login and be able to set this same cookie from this non yabb page, so as not to force someone to login at the forum and then return to the cookbook area.

I did some research at yabbforum, and found others who were doing this with cgi, but not with php. I want this to be independent of yabb scripts, other than checking user.dat files to set user permissions.

So the first question, how can this section of perl script be translated to php?

Code:
foreach (split(/; /,$ENV{'HTTP_COOKIE'})) {
     $_ =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     ($cookie,$value) = split(/=/);
     $yyCookies{$cookie} = $value;
  }
 if($yyCookies{$cookiepassword}) {
     $password = $yyCookies{$cookiepassword};
     $username = $yyCookies{$cookieusername} || 'Guest';
  } else {
     $password = '';
     $username = 'Guest';
  }


It's the first part that's got me stumped.  

Posted by admin (Graham Ellis), 8 October 2003
In Perl, you have to translate the cookie string in the environment variable into something more friendly (that's what the first section of the code is doing), but PHP does it for you automatically, and provides you with an array of cookies called $_COOKIE.

Simply leave out the first setcion of the code    and then replace your references to $yyCookies with references to $_COOKIE, and your  { and } used to reference hash elements in perl with [ and ] to reference array elements.

Posted by dabbler (leah), 13 October 2003
Thanks Graham, I appeared to continue to try to make it more complicated than it actually was, then resorting to finding another php login/cookie setting script to see working code. I've now got a basic script which is checking the login password and comparing it to the yabb user data, and setting the cookies properly.

However, I spent the weekend learning how to encrypt the passwords before comparing and setting the password encrypted as yabb does, tracking down the salt that was in front of my face the whole time.  And that was the easy part, lol. I still have to make it do a lot more, checking banlist and other data.

Thanks for you help.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho