473,498 Members | 98 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting cookie before leaving page

Hi all ...
I've been learning and using PHP for a couple months now,
working on a couple of somewhat different sites.

On one site, I have a page with a form,
and that form includes a couple of ACTION=SUBMIT buttons,
so that clicking either button will immediately reload the page
only with an argument tacked on, e.g. "?size=128" or "?size=256".
This causes some images to be resized,
but otherwise the page appears the same.

I would like to save choice made by clicking either of those two buttons
by setting a cookie on the visitor's computer.
(At least, I *think* that's how I should do it.)
I can put some code at the tippy-top of the page
that looks at the incoming $_GET[] arg and call setcookie() accordingly.
This really seems like it ought to do the job.
However, what I'm finding is that the cookie does not get set
unless and until the visitor clicks REFRESH in the browser
(or does anything equivalent);
if they don't think to do that, the cookie remains obsolete.

Are my expectations out of whack?
If not, any likely ideas re what I'm doing wrong?
Oh, fine, here's the code:

<?php
$size_str_fm_get= $_GET["size"];
$time= time()+60*60*24*30;
if ( strcmp( $size_str_fm_get, "small" ) == 0 )
setcookie("size", "small", $time);
elseif ( strcmp( $size_str_fm_get, "large" ) == 0 )
setcookie("size", "large", $time);
?>

(The slightly strange conditional is so that if the value of the $_GET[] arg
is neither of the expected values, the cookie is not set at all.)
(And, yes, this code is at the very top of the file, *before* any HTML code.)

Thanks!
Jan 16 '08 #1
3 2023
On Jan 16, 10:16 am, MangroveRoot <zcukpk...@sneakemail.comwrote:
Are my expectations out of whack?
If not, any likely ideas re what I'm doing wrong?
Are you sure the cookie is only set after a refresh? With what
browser(s) does this happen? And have you tried inspecting the
response headers? (IE with the Live HTTP Headers extension for
Firefox)

-Michael Placentra II | PHP5 ZCE
Jan 17 '08 #2
MangroveRoot wrote:
However, what I'm finding is that the cookie does not get set
unless and until the visitor clicks REFRESH in the browser
(or does anything equivalent);
This is absolutely normal. From the man':
"Cookies will not become visible until the next loading of a page that the
cookie should be visible for."
if they don't think to do that, the cookie remains obsolete.
Well do it for them; after setcookie(), just do a header('Location: ' .
$_SERVER['PHP_SELF']) and, of course, a die().

Basically, this isn't necessary since you've got the date you need in the
$_GET. However, the refreshing method might be a better solution to avoid
the user to be prompted by his browser to resend the post data if he reloads
the page manually, for whatever reason.

Read about the PRG design pattern.
http://en.wikipedia.org/wiki/Post/Redirect/Get
http://www.theserverside.com/pattern...hread_id=20936

-thib´
Jan 17 '08 #3
On Jan 17, 2:06 pm, thib´ <th...@coralsnake-team.comwrote:
This is absolutely normal. From the man':
"Cookies will not become visible until the next loading of a page that the
cookie should be visible for."
Heh...I really over-thought this one. I thought they meant literally
that the cookie is not being set.

Please disregard my post.

-Mike Placentra II | ZCE
Jan 20 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9157
by: bagsmode | last post by:
Hi, I'm trying to set a session cookie and then redirect, however I get the error: Status: 302 Moved Location: /index.cgi I thought I recall getting an error like this when I first tried...
1
1850
by: Rich | last post by:
Hello, I want to create a cookie that will expire in a few mintues after a user leaves my page so that they can return without having to login again if they were only gone for a few minutes. I...
7
14510
by: ehendrikd | last post by:
hi all i need some clarification on how the php session work in relation to cookies. we have a web site where users need to log in. a few of our users were having troubles with their browser...
1
1706
by: Rich | last post by:
Hello, I want to create a cookie that will expire in a few mintues after a user leaves my page so that they can return without having to login again if they were only gone for a few minutes. I...
2
2554
by: junlia | last post by:
Hi All, I am working on a project that acts as a bridge. It does some checking with post xml data, and then redirects the request to an appropriate page. However, we find that depends on the...
2
3717
by: Alan Silver | last post by:
Hello, I have discovered that if I try and add a cookie when one by that already exists, nothing happens. No error, but the cookie is not set to the new value. For example (this is running in...
1
2271
by: AnthonyC | last post by:
I am having a problem tracking down what I believe to be a problem with the way cookies are being used on our website application. When user log onto the application, an in-memory (per-session)...
1
6435
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
7296
by: SevDer | last post by:
Hi, I've done some coding in my web application however right now for an unknown reason my asp.net 2.0 site is not setting asp.net_sessionid cookie and as a result, I am losing the session data...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.