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 or ?

Posted by TedH (TedH), 3 February 2009
Hi, Graham, enjoying the snow? We've got had over 8 inches here today.

I've inadvertantly, by accident, made a key work for a bunch of sub routines. If the key is set (any assigned number) the whole set can be used, if not it won't work. If I hack into one sub and do stuff then the second I click it's action button the script stops and goes to my permission denied screen. Fine that's exactly what I want.

Now I need to have a user who has logged in to have that key number assigned to them so they can use the script. I've got it working from a file, with only the number in, which is read and the whole script works fine. Change the number in the file, the script stops - good, it's working right.

I am assuming that I need the user to have a cookie with just that number in. I have never used cookies before and do not know whether this is the correct way to do it. In looking at cookies I'm getting a lot of information that  seems total overkill for what I want to do (if it can be done in the first place). I guess I would have to set the cookie (don't know exactly how to do that - like can a cookie just have a number in?). Retrieve it (much like the file I'm using) and get the number. When the user closes their browser or logs out the cookie is made useless (no idea here).

There are 3 steps here? (set cookie, retrieve it, dump it). From what I've read, if there is no expiry on a cookie it automatically deactivates when the user closes their browser - that much I understand.

I think I need some input on this please.

Cheers - Ted

Posted by KevinAD (KevinAD), 3 February 2009
If you are using the CGI module in your script it supports cookies. See the documentation for details.

It sounds to me like you could use a cookie for what you are doing. And yes a cookie can store a simple number but you might want to have that number encoded somehow if its usage is meant to prevent people from accessing something.



Posted by TedH (TedH), 3 February 2009
Hi Kevin, no I'm not using the CGI module, but I did see in some of the bumpf about cookies where people did use it to set cookies. Seems very complicated - I rarely use the thing because I can't get decent visual styling like I want (maybe my lack of undertanding it though)

I did a test so that when I log in, the test text file get's created and the number written to it. So that would be the equivelant of writing a cookie.

So if I write the cookie with a one liner like this:
Code:
Set-cookie:key=1;

Then I would have code to read the cookie into an array, split it up and extract the number - yes?

If I can get that working then I can do the number encrypted for the final (once I get it working I'm able to understand it better).

Posted by KevinAD (KevinAD), 4 February 2009
If you don't use the CGI module you can use a subclass CGI::Cookie which comes with perl. I think if you read the CGI::Cookie documentation and try a few things you will figure it out, its fairly simple to write a simple cookie like the one you want.  The only real thing you have to be aware of is that the cookie needs to be set before you print a complete http header, otherwise its just content send with the document instead of a cookie file. The documentation explains it. I think you'll get it after a few tries, if not, post back.

Posted by TedH (TedH), 4 February 2009
Hi Kevin, I'll have a look at CGI::Cookie like you suggest and see how I get on.

It's all very new to me and like so many things in perl etc., when you hit the forums and tutorials people assume you know everything about it or complicate it with so much theory you get lost about paragraph two.

Once I get it down, I'll be able to lay around as needed.

Cheers - Ted

Posted by admin (Graham Ellis), 4 February 2009
We've got a couple of examples on the web site that might help (or I might be too late!)

This one uses CGI::Cookie:

http://www.wellho.net/solutions/perl-handling-cookies-through-cgi-pm.html

This one does it from first principles:

http://www.wellho.net/resources/ex.php4?item=p407/whisky.pl

Note that the cookies use a somewhat different date format to standard, and that's why you're recommended to use the module unless you're happy with cookies which last as long as the browser.


Posted by admin (Graham Ellis), 4 February 2009

Were you asking about this stuff?



Ah - we just carry on - no cancelling of anything around here

Posted by TedH (TedH), 4 February 2009
Hi Graham, looks like you had it light. Doesn't take much to shut down London, so this lot had 'em on the hop.



Posted by KevinAD (KevinAD), 4 February 2009
Its been in the 80s (fahrenheit) here in Southern Califonia. I'll try and send you guys some sunshine.

Posted by TedH (TedH), 4 February 2009
I used to live near Oxnard and some years later in Sunland/Tujunga. Spoilt me - I haven't liked cold since.

Where you at?

Posted by KevinAD (KevinAD), 4 February 2009
I live in Riverside Calif. About 50 miles east of Los Angeles.

Posted by TedH (TedH), 4 February 2009
Been through it many years ago don't remember much. Doesn't snow much there. Just burns on occassion.

Posted by TedH (TedH), 4 February 2009
Just noticed your links Graham. I'll check those out too.

I think my brain is frozen.....

Thanks - Ted

Posted by KevinAD (KevinAD), 5 February 2009
on 02/04/09 at 18:22:35, TedH wrote:
Been through it many years ago don't remember much. Doesn't snow much there. Just burns on occassion.


Where I live, there has been snow once or twice in the last 20 years. You guys wouldn't call it snow. It made the ground white for a few minutes, maybe was a quarter of an inch thick. Almost 80 again today, clear skies, light breeze, beautiful day. Rain forecast for tomorrow with cooloing into the upper fifties for the rest of the week. I might even have to put on long pants for a few days.

Posted by TedH (TedH), 7 February 2009
Been playing with your non persistant cookie code Graham and redid it so that I end up with four fields (fourth is the 2nd move and on in that session). I put in a unique name as the first field, sao I end up with something like this.

UniqueName:Brand:1 (first go in session)
UniqueName:Brand:1:2 (from 2nd move and on)

At the bottom of the script are the 6 brands. I increased that to 12 and it works. Am I allowed to have that many for a cookie?

Posted by KevinAD (KevinAD), 7 February 2009
There is no limit. The limit is the amount of data a cookie file can hold. I thinks it 4096 bytes.

Posted by TedH (TedH), 8 February 2009
Thanks Kevin. Yes it's 4k for cookies apparently.

It took a while and I went very basic so that I could understand how the cookie is handled (which was the main problem my side). At the moment I have it working fine like this:
Code:
--------------------
ca.pl -- sets cookie
--------------------
#!/usr/bin/perl

$cookie = "1";
print "Set-Cookie: $cookie\n";
print "Location: cag.pl\n\n";
exit;

--------------------
cag.pl -- gets cookie and forwards user
--------------------
#!/usr/bin/perl

$gotcookie = $ENV{'HTTP_COOKIE'};
@cookies = $gotcookie;
if (@cookies == 1) {print "Location: cafile.pl\n\n";}
exit;

--------------------
cafile.pl -- file forwarded to
--------------------
#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "It's too cold in London for cookies, so we'll have lemon popsicles instead.";


Before I go any further, am I treating the cookie right?
I'm looking at it like I would look at a normal text file (which might be a wrong way to view it).
When I set the cookie, I'm going to want probably 6 entries:
An ID (Probably a date to the tenth of a second thingy - variable from my date code)
The name of the site
User Name
Session Name (graham's whiskey stuff)
The key number (in the case above it's 1 - eventually encrypted somehow, Kevin)
This session kount

Can I write that like this (with appropriate data) or do cookies need to be done differently?

ID:Site:User Name:Session Name:The Key Number:This session count

Whew! Wrapping my brain around this is hard work. Once I get the gist of it I'll be okay.

- Ted

Posted by KevinAD (KevinAD), 8 February 2009
A cookie is just a plain text file. You can view the cookie files on your PC to see how they look using any text editor. Many are encrypted but some aren't.

Think of cookies the same as flat files but just one line of data. Use a delimiter to seperate the different fields in your cookie when you set the cookie. Then when you get the cookie you split the file using the delimiter into whatever data type suits your requirements.

What you posted is perfectly fine:

ID:Site:User Name:Session Name:The Key Number:This session count

Posted by TedH (TedH), 8 February 2009
Good.

Thanks Graham and Kevin. You've both been a great help.

I shall get on with it now - well, after a little break (it's nearly 11pm here).

Cheers - Ted

Posted by KevinAD (KevinAD), 8 February 2009
I urge you to look into CGI::Cookie it comes with perl so nothing to install. It makes reading and writing cookies a snap. You don't have to worry about any of the underlying issues of how cookies work. Just learn the modules interface and you're good to go.

Posted by TedH (TedH), 8 February 2009
Thanks, I will Kevin.

Posted by admin (Graham Ellis), 9 February 2009
on 02/08/09 at 13:45:26, KevinAD wrote:
I urge you to look into CGI::Cookie it comes with perl so nothing to install. It makes reading and writing cookies a snap. You don't have to worry about any of the underlying issues of how cookies work. Just learn the modules interface and you're good to go.


Totally agree with that advise, even though I'm not keen on the rest of the CGI module stuff which seems like bloat to me!



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