matc has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to build a feature into my site where people can pick out 5 of some 5000 favourite links to be displayed as the users favourite links across the site. I'm working on a perl script that will hopefully allow for this...

However, I wanted to expand on the usual format of the cookie I have. Ideallly a single cookie will hold the 5 values for each seperate id of the link.

The values would be one for each link and would consist of an id+4 digits which are picked off a query string. There is a .HTaccess file and XSSI code to deal with thr front end.

Link would be fired on the website by either:
<a href="/cgi-bin/setfavourite.pl?<!--#echo var="PAGE_ID" -->&addfavlink">Add <!--#echo var="label" --></a> for adding a one
&
<a href="/cgi-bin/setfavourite.pl?<!--#echo var="PAGE_ID" -->&removefavlink">Remove <!--#echo var="label" --></a> for removing a one.

Here is the PERL code I have so far:
#!/usr/local/bin/perl use CGI; $query = new CGI; my ($number, $fav1, $fav2, $fav3, $fav4, $fav5, $cookie); my $nextpage ='http://www.domain.co.uk/'; if (m/^(\d+)&addfavlink/) { $number = $1; $addfavlink = "id".$number; $cookie = $query -> cookie( -NAME => 'FAV5LINK', -VALUE => '$fav1', '$fav2', '$fav3 +', '$fav4', '$fav5', -EXPIRES => '+1y', -DOMAIN => '.domain', -PATH => '/', -SECURE => '0' ); print $query -> header( -COOKIE => $cookie, -LOCATION => $nextpage."userchoice.sht +ml?id=$number&added" ); } elsif (m/^(\d+)&removefavlink/) { $cookie = $query -> cookie( -name => 'FAV5LINK', -VALUE => '$fav1','$fav2','$f +av3','$fav4','$fav5', -EXPIRES => '-1h', -DOMAIN => '.domain, -PATH => '/', -SECURE => '0' ); print $query -> header( -COOKIE => $cookie, -LOCATION => $nextpage."removed.html" );<br /> } exit 0;

The content should define in the content area of the cookie the values *= omitted *(fav1=)id0008... etc ...etc

Ideally I wanted to allow for updating the cookies values to allow for replacing the 'fav1' with something else or the user adding a 'fav2'. Also there would only be a maxiumum of 5 values for this named cookie.

I got my head around it a little bit but am in much need of help.

many thanks, mat

Replies are listed 'Best First'.
Re: CGI cookies: in much need of help
by EvdB (Deacon) on Feb 12, 2004 at 15:38 UTC
    A few comments:
    • You never read in the value of the cookie you want to edit - so how do you know what other links have been saved.
    • The two cookies you create have different names - is this right?
    • One of your cookies is set to expire immediately (one hour ago).
    • Don't let Hutton get you down :-)

    To acheive what you are after I would suggest:

    get value off cookie do we want to add or remove link to cookie value add: add the link, trim if needed to 5 values remove: remove link save new cookie value && redirect to new page.

    HTH.

    Update: removed comment on html in code.

    --tidiness is the memory loss of environmental mnemonics

      Hi,

      lol - not exactly the most well formed code I have to admit. and as for the Hutton, I have better things to worry about than him... like my old dear for a start ;)

      ...
      names - should be the same for sure.

      The cookie set to -1h should really be '0' or nothing '' to end the session when the all browser instances are closed.

      once the cookie has been set with the first value and redirected to the desire id=....?added it should display the new link in a section of the site with a add remove text link next to any and all.

      The way I was think but need to ask if its right to do this...

      The only reason the user should call this script is to add and delete links. the actual printing of values will be embedded into the section of the page and will read the cookie from there. The reason for this is that some pages are static and some are dynamic, this script hopefully will just serve as the mechanism. To be honest I haven't got to that bit.

      The only thing the script should really do is update the cookie with the users values and like you said trim/limit them to a total of five values. there doesn't really have to be any user msg for when they reach the maxiumum of 5 links, I'll think about that later when I learn a bit more about PERL.

      Hope that anwsers some of the stuff anyway. so the routines should include what you mentioned.

      Add
      1. clock the named cookie and set a new one if need. Add the values(s) and limit to 5.

      Remove
      2. grab the name of the cookie and delete the selected value. and update the number of values.

      Seems you got me thinking now could I stick another value in the cookie that would hold the value count... a ha.. something like count .. that starts at 0 and goes to total 5. That would help the front end code know how many links are there and respond correctly hey!?

      peace, mat
Re: CGI cookies: in much need of help
by zby (Vicar) on Feb 12, 2004 at 15:59 UTC
    I would advise you to use CGI::Session. This way you would be able to store an arbitrary complicated data into the session object and the cookie would only need to contain the session id to restore that information.
      This is a good suggestion - but the front end (which is SSI powered) may not have access to the datastore that would be used. I expect this is the case here.

      This would explain the somewhat arcane format that the OP is getting at with the values in the cookie.

      --tidiness is the memory loss of environmental mnemonics