Page 1 of 1

Cookies are not setting

Posted: Thu Mar 15, 2007 10:08 pm
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.

Posted: Thu Mar 15, 2007 11:21 pm
by feyd
You have 17 missing double quotes and one missing closing parenthesis.

Posted: Fri Mar 16, 2007 9:41 am
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

Posted: Fri Mar 16, 2007 9:47 am
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().

Posted: Fri Mar 16, 2007 9:49 am
by mentor
Compare your code with the examples at http://www.php.net/setcookie

Posted: Fri Mar 16, 2007 10:30 am
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);
    }
}
?>

Posted: Fri Mar 16, 2007 10:43 am
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

Posted: Fri Mar 16, 2007 10:48 am
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.

Posted: Fri Mar 16, 2007 10:52 am
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

Posted: Fri Mar 16, 2007 10:55 am
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!

Posted: Fri Mar 16, 2007 10:56 am
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.

Posted: Fri Mar 16, 2007 11:05 am
by Jonah Bron
Oh, okay. So I WAS missing something. ME = :crazy: :wink: Thanks

Posted: Fri Mar 16, 2007 11:05 am
by RobertGonzalez