Cookies are not setting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Cookies are not setting

Post by Jonah Bron »

Hi,

This is my first post on this forum. I did a search, but I didn't find anyone else that had this problem. I have just learned PHP, and still know very little (what I know, I learned in 2 days). Using my small amount of knowledge of PHP, I wrote up a page that is supposed to set certain cookies under certain conditions, using if...else, and setcookie(). Here is what I came up with:

Code: Select all

<?php

if ($_REQUEST["item1"]=="yes")
setcookie(item1cookie, yes, date() + 3600;

if ($_REQUEST["item2"]=="yes")
setcookie(item2cookie, yes, date() + 3600);

if ($_REQUEST["item3"]=="yes")
setcookie(item3cookie, yes, date() + 3600);

if ($_REQUEST["item4"]=="yes)
setcookie(item4cookie, yes, date() + 3600);

?>
This is the error message I get, after uploading it, and going to the page:

Parse error: parse error, unexpected T_STRING in /home/content/g/e/i/geinternet/html/nucleussystems/PHP/testform.php on line 20

What I find really confusing, is that there isn't even a 20th line in the code :crazy: . I would really appreciate any help on why this is not working. Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have 17 missing double quotes and one missing closing parenthesis.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Hi,

Thanks for your help. I fixed the parentheses, but I'm not quite sure where the missing double quotes are :oops: . Are they on the name, value, etc. of the cookies?

Thanks again
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Look at our syntax highlighting. It's supposed to have string in red, syntax in green, functions in blue and constants in black.

Code: Select all

<?php

if ($_REQUEST["item1"]=="yes")
setcookie("item1cookie", "yes", date() + 3600);

if ($_REQUEST["item2"]=="yes")
setcookie("item2cookie", "yes", date() + 3600);

if ($_REQUEST["item3"]=="yes")
setcookie("item3cookie", "yes", date() + 3600);

if ($_REQUEST["item4"]=="yes")
setcookie("item4cookie", "yes", date() + 3600);

?>
EDIT | As per poster below, date() should be time().
Last edited by Chris Corbyn on Fri Mar 16, 2007 9:55 am, edited 1 time in total.
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

Compare your code with the examples at http://www.php.net/setcookie
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Another thing you might want to consider is using the $_GET and $_POST superglobals instead of using $_REQUEST. It is a little more secure and less prone to confusion as to where your data is coming from. Also, you really should run them through isset() to prevent undefined variable/index notices.

Code: Select all

<?php
// Set a cookie time var so your cookies are all consistent and calculated once
$cookie_time = time() + 3600;

// Now handle the conditionals
if (isset($_POST['item1']) || isset($_GET['item1']))
{
    if ($_POST['item1'] == 'yes' || $_GET['item1'] == 'yes')
    {
        setcookie('item1cookie', 'yes', $cookie_time);
    }
}

if (isset($_POST['item2']) || isset($_GET['item2']))
{
    if ($_POST['item2'] == 'yes' || $_GET['item2'] == 'yes')
    {
        setcookie('item2cookie', 'yes', $cookie_time);
    }
}

if (isset($_POST['item3']) || isset($_GET['item3']))
{
    if ($_POST['item3'] == 'yes' || $_GET['item3'] == 'yes')
    {
        setcookie('item3cookie', 'yes', $cookie_time);
    }
}

if (isset($_POST['item4']) || isset($_GET['item4']))
{
    if ($_POST['item4'] == 'yes' || $_GET['item4'] == 'yes')
    {
        setcookie('item4cookie', 'yes', $cookie_time);
    }
}
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Hi,

Thanks for your hlep. I made some changes to my code, and it still didn't work. Then, I copied you text, d11wtq, (completely identical, as far as I could see) and it worked. BUT, this came up, when I tried to set the conditions for the cookie setting, this is what I got:


Warning: Wrong parameter count for date() in /home/content/g/e/i/geinternet/html/nucleussystems/PHP/testform.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at /home/content/g/e/i/geinternet/html/nucleussystems/PHP/testform.php:4) in /home/content/g/e/i/geinternet/html/nucleussystems/PHP/testform.php on line 4

Warning: Wrong parameter count for date() in /home/content/g/e/i/geinternet/html/nucleussystems/PHP/testform.php on line 7

Warning: Cannot modify header information - headers already sent by (output started at /home/content/g/e/i/geinternet/html/nucleussystems/PHP/testform.php:4) in /home/content/g/e/i/geinternet/html/nucleussystems/PHP/testform.php on line 7

List of items


Item 1 (checkbox here)

Item 2 (checkbox here)

Item 3 (checkbox here)

Item 4 (checkbox here)

(submit button here)

(form goes to itself)



anything in brackets, I added in. ex. (checkbox here). First of all, I checked the first two, and there errrors are on lines 4, and 7, which both happen to be the code for setting the cookie for that condition (those items checked). Thanks for your help. :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Read his post entirely... the date function is not used properly there, it should be time(). Look at my post, it shows it that way.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

I am really confused. At one place where I see how to set cookies, they have them without quotations at all. Here, someone says that there is supposed to be double quotations, and then someone else's code has single quotation marks. :drunk: I am clearly missing something :wink:

Thanks for the good tips and code, Everah. Will put that in, maybe do a little tweaking, to see what its all about, and give it a go round. Thanks!

Thanks again,
PHPyoungster
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Wow! Answers just keep coming so fast, they are even dropping in while I'm answering another! Ha Ha! :P I'll fix that time/date problem. Thanks!
Last edited by Jonah Bron on Fri Mar 16, 2007 11:01 am, edited 1 time in total.
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

You can use single or double quotes. setcookie() accepts cookie name and value in string. In some examples you may have seen variables.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Oh, okay. So I WAS missing something. ME = :crazy: :wink: Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Post Reply