Jump to content

Recommended Posts

Posted (edited)

I have a test Perl script for cookies, very simple (and simplified a bit) that doesn't work properly:

 

>use CGI qw(:standard);
use CGI::Cookies;

if(defined param("on")) # on button clicked
{
 $c = new CGI::Cookie(
-name => 'test',
-value => 1,
-domain => '.****',
-path => '/cgi-bin',
 );
}

elsif(defined param("off")) # off button
{
 # set same variable/settings except value is '0'
}

print header(-cookie =>[$a]);

# start html, head, title, body tags

%cookie = fetch CGI::Cookie;

if(defined $cookie{test})
{
 $c = $cookie{test}->value;

 # code for form with on|off submit button
 # the form POSTs back to this script
}

 

Here's how it works

 

# script instance 1 (first page load)

cookie value on PC: 0 (let's pretend)

cookie value from fetch: 0

[i click the 'on' button & submit the form]

 

# script instance 2

cookie value on PC: 1

cookie value from fetch: 0 (this is the problem)

[i click the on button again]

 

# script instance 3

both cookie and fetch are '1'

 

So what's happening is, the cookie gets set and there's a (basically) immediate fetch which doesn't match with what I expect until the second time around. I'm baffled, any help will help. :)

Edited by eggrock
Posted

Basically, the issue is that you're expecting to be able to read a cookie from a user's machine in the same request that you set it in, and cookies just don't work that way. There is one transmission of data from your machine to the server (containing the page request, form data, and any cookies stored for your site), which is then followed by one response from the server to your machine, containing the page to be displayed and any new cookies to be set.

 

'fetch CGI::Cookie;' does not literally fetch a cookie from the user's machine. When you click the submit botton (actually, when you request any page from the web server), the browser automatically sends any cookies it has to the server. The server holds them in memory and provides them to your script when it calls 'fetch CGI::Cookie'.

 

In your second instance, when your script calls 'fetch CGI::Cookie;', it is reading the cookie your browser sent at the time you clicked the submit button - not the cookie you just sent to your machine. This is what's occurring when you run your script:

 

# script instance 1 (first page load)

 

Your machine:

- Browser requests page

- No form parameters sent

- Cookie value '0' sent

 

Server:

- Server sees cookie value '0' sent by browser

- Script sees no form parameter

- Script sends page with form back to browser

 

You then click the 'on' button & submit the form...

 

# script instance 2

 

Your machine:

- Browser requests page

- Form parameter 'on' sent

- Cookie value '0' sent again

 

Server:

- Server sees cookie value '0' sent by browser

- Script sends (sets) cookie value '1' to your machine

- Script reads sent cookie with 'fetch CGI::Cookie' and sees sent cookie value of '0'

- Script sends page with form back to browser

 

You then click the 'on' button & submit the form...

 

# script instance 3

 

Your machine:

- Browser requests page

- Form parameter 'on' sent

- Cookie value '1' sent

 

Server:

- Server sees cookie value '1' sent by browser

- Script sends (sets) cookie value '1' to your machine

- Script reads sent cookie with 'fetch CGI::Cookie' and sees sent cookie value of '1'

- Script sends page with form back to browser

 

In a nutshell, I'd recommend restructuring your script code so that it first reads any cookies first sent by the browser. If no cookie was found, then create one. Then test for your form parameter (on/off). Set the now existing cookie with appropriate value if necessary, and send it to browser (set cookie). Test cookie value, and have script perform whatever actions you want and send page to browser.

 

Hope this helps...

Posted

Great, that's exactly what I needed to know about cookies. In the 'real' script I'm working on I can just set a variable based on which button is clicked and display results that way; next time the page loads it'll read the cookie data.

 

But... Now I'd better examine the rest of the cookies to make sure I understand how they're being interpreted.

 

Thank you!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...