From PHP to Perl


Cookies

Posted in Programming by gosukiwi on November 21, 2006

Hello!

Today i will explain the differences between PHP and Perl with Cookies.

PHP is way better than Perl for cookies (maybe mod_perl could do a better job but PHP is still better) anyways, its not a big deal to get or set a cookie in Perl.

In Perl cookies can have maximum 6 attributes, in PHP they can have 7, most of them are optional, but they ones we are going to explain here are 3 attributes, which are in both languages, name, value and expire, the most basic attributes ;3

Before we start, i want to write the possible attributes, just for you to know them

In PHP:

Name
Value
Expire
Path
Domain
Secure
Httponly

In Perl:

Name
Value
Expire
Domain
Path
Secure Flag

Okay, now we can start explaining the 3 basic attributes, value, name and expire

In PHP:

set_cookie(‘name’,’value’,’expire’);

In Perl:

use CGI::Cookie;
$cookie = Cookie(-name=>’name’, -vaue=>’value’, -expires=>’expire’);
print header(-cookie=>$cookie);

In Perl, cookies must be sent before the html header, else, they wont stick, and you will probably see something like Set-Cookie: mycookie, etc

Okay, lets set a simple cookie, with the name user, the value fedekiller and the expire date 1 month

In PHP:

set_cookie(‘user’,’fedekiller’,time()+60*60*24*30);

In Perl:

use CGI::Cookie;
$cookie = Cookie(-name=>’user’, -vaue=>’fedekiller’, -expires=>’+1M’);
print header(-cookie=>$cookie);

In PHP we use the time function and multiply till we add one month (60 seconds * 60 minutes * 24 hours * 30 days)
In Perl, we just set +1M to make it last 1 month, if we want to delete a cookie, we have to use a negative value in expires, for example, in PHP would me time()-60*60… and in Perl it would be -1M

Now, lets read a cookie, with PHP is very easy

In PHP:

$_COOKIE[‘user’];

That will read the cookie value (in this case, fedekiller)

In Perl:

my %cookies = fetch CGI::Cookie;
$cookies{‘user’}->value

We set all cookies to the cookies hash, and then we just request the value.

I made a little function to get cookies in Perl so its a little bit easier

sub get_cookie
{
my %cookies = fetch CGI::Cookie;
if($cookies{$_[0]} ne ”)
{
$c = escapeHTML($cookies{$_[0]}->value);
}
}

We check if the cookie is not empty, because, if it is it will display a fatal error when we try to get the value.

Okay, thats all for now 🙂 i hope you all find it useful.

Bye!

Advertisements

7 Responses to 'Cookies'

Subscribe to comments with RSS or TrackBack to 'Cookies'.

  1. Perl said,

    use CGI qw( header cookie );
    my @foo = cookie(‘foo’);
    my $bar = cookie(‘bar);

    CGI.pm does have a httponly flag: http://search.cpan.org/dist/CGI.pm/CGI/Cookie.pm

    print header(-cookie => cookie(-name=>’cook’, -value => 1, -httponly => 1));

  2. Ersalan said,

    I wanna delete all cookies related to the domain xyz.com like (abc.xyz.com and def.xyz.com) via a PERL script running on xyz.com. Do you have any pointers on that?

  3. assasiner said,

    It has been a lot since i last programmed in perl, i should read some books again… i dont remember much… but the general idea would be something like this

    use CGI::Cookie;
    my @array;
    my %cookies = fetch CGI::Cookie;
    while (($key, $value) = each(%cookies))
    {
    $cookie = Cookie(-name=>$key, -vaue=>$value, -expires=>’-1M’);
    push(@array, $cookie);
    }

    foreach(@array)
    {
    print header(-cookie=>$_);
    }

    Im not sure about the last foreach, because i dont know how many times header can be printed and im sure there must be an easier way, but well thats the most i can do with what i remember… I shall re-read and post later.

  4. Nazdar said,

    “In Perl, cookies must be sent before the html header, else, they wont stick”

    Thank you! Very important point that hung me up for a couple of hours wondering why the cookie wouldn’t set.

  5. silmaril said,

    “In Perl, cookies must be sent before the html header, else, they wont stick”

    Well that’s not perl specific but inherent to the HTTP procotol. Only difference
    between PH and Perl is that the first does some buffering of the output, allowing
    to use set_cookie() a bit later.

    However even with PHP, if for some reason the header had been sent before the use of set_cookie, you won’t be able to set/delete cookies

  6. Vinod said,

    I am facing the following problem.. Can you pls help me in solving this.
    A cookie has been set in php which is encrypted.
    I want to read information from that in perl..Can you tell me how to do this.?

    Thanks for the help

    Regards
    Vinod


  7. It’s awesome to go to see this web site and reading the views of all colleagues about this paragraph, while I am also keen of getting know-how.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: