Cookie Problem

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Cookie Problem

Postby ruturajv » Wed Apr 09, 2003 6:18 am

I'm having a strange problem, I can't write a cookie
I have the following code

<?php

$backcolor = $_POST['backcolor'];
$textcolor = $_POST['textcolor'];

setcookie("backcolor", $backcolor, time()+3600);
setcookie("textcolor", $textcolor, time()+3600);
header("Location: http://localhost/php/cookies.sps");
exit;

?>

I get the the POST data from the 'cookies.sps' where I send my browser back.
And my Register_globals is off, I use PWS

And as in ASP
How can same cookie name can be given a different name=value
like response.cookies("color")("textcolor") = "black"

Is the below code correct for PHP?

<?php
setcookie("color['backcolor']", $backcolor, time()+3600);
setcookie("color['textcolor']", $textcolor, time()+3600);
?>

Please help!

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Cookie Problem

Postby ruturajv » Thu Apr 10, 2003 10:00 am

Seems no one is interested in my question,
Why can't I write the cookie as mentioned in the original post?

Please Help!

sleepingdanny
New php-forum User
New php-forum User
Posts: 13
Joined: Mon Mar 17, 2003 6:38 am

Postby sleepingdanny » Fri Apr 11, 2003 4:54 am

Try this...

<?php
$bc = $backcolor;
$tc = $textcolor;

setcookie("backcolor", $bc, time()+3600) &&
setcookie("textcolor", $tc, time()+3600) &&
header("Location: http://localhost/php/cookies.sps");
exit;
?>

I never tried to work my scripts in a localhost, why don't you try uploading them first. :P
And remember that the code has to be above the <html> tag or it wont work!

By the way... You'r using a form I hope??!!

User avatar
pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Postby pootergeist » Fri Apr 11, 2003 5:03 am

or

$c_array = array(backcolour => $_POST['backcolour'], textcolour => $_POST['textcolour']);
$c_data = serialize($c_array);
setcookie('colour_cookie',$c_data,time()+3600,"/","www.domain.com");

.......

if($_COOKIE['colour_cookie'] !== "")
{
$c_data = unserialize(stripslashes($_COOKIE['colour_cookie']));
echo 'background = '.$c_data['backcolour'];
}

might need if($colour_cookie !== "") though as the name might be globalized. Also depends upon your build version of PHP.


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron