in reply to cookie retrieval problem

It works for me. Honestly.

I just copy/pasted your script into a new page, and uploaded it to my webserver. The first time it loaded, it said "Can't find the cookie!". When I reloaded, it said "Nikolas". It'll keep doing that until I close my browser, I'm sure.

Update: So it does.

Anyway, don't listen to other people saying you can't read and set a cookie in one script. You can, and apparently, as in your script, even in reversed order.

Replies are listed 'Best First'.
Re: Re: cookie retrieval problem
by chromatic (Archbishop) on Mar 01, 2004 at 02:27 UTC
    don't listen to other people saying you can't read and set a cookie in one script

    Who said that? I said you can't set and read a cookie in the same request. As far as I can tell, that remains true.

Re: Re: cookie retrieval problem
by Nik (Initiate) on Feb 29, 2004 at 22:22 UTC
    yes actually now that i have tried it again it worked for me too! :) But this cookie will se to each visitor right? how can it identify me from others?
      Ah, that problem again. <grin>

      Well, I can think of basically two approaches. Either you provide a special, different cookie value to each user, either semi-randomly assigned or via a login page, and you can then track any visitor to your site.

      Or, you don't set cookies for everybody, but only for yourself. You can have a special page, which you use to enter the site, but which nobody else knows of. All it has to do is set a cookie, and redirect you to the normal entrance. Every other page only has to read the cookie value. The following code forms the entrance page for the latter approach:

      #!/usr/bin/perl -w use CGI qw(:standard); my $cookie_out = cookie(-name=>'test', -value=>'Nikolas was here!'); print redirect(-cookie=>$cookie_out, -uri => "index.html");
      I'm assuming "index.html" is the normal index page to your site's directory.

      Now if you change the other script so it only reads the cookie (so drop the assignment to $cookie_out), you can visit the above script first, then go to the other script, possibly even by typing in the URL manually, and you'll see the cookie value as set here.

        Thanks guys but i have decided that the best way for my script to identify me is even simpler that using cookies.... here it is....

        if ($host =~ /thes530-.*?\.otenet\.gr|millennium-.*?\.ccf\.auth\.gr/)
        Although my ip from the two providers i use is dynamic this solution works just fine for me :)

        The cookies solution can alsobe done but it wouldn't be as quick and short as this one :)
          A reply falls below the community's threshold of quality. You may see it by logging in.

      To identify 'you from others' you will need to store a bit of info. Traditionally you will generate a unique session ID for a user that allows you to map that unique session ID to any data you want. You usually just store the session ID in the cookie and store the data on the server in some sort of persistent store - you have MySQL so that is as good as anything. This is one way to maintain state across the stateless HTTP protocol. Super search for 'session management' or similar to see lots of examples.

      CGI::Session may be what you are looking for.

      cheers

      tachyon