You are here: setting cookie before leaving page « PHP Programming Language « IT news, forums, messages
setting cookie before leaving page

Posted by MangroveRoot on 10/02/88 12:00

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!

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация