justuptime.com - monitor your servers & websites

php cookie

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

They have: 34 posts

Joined: Feb 2002

My error..

Notice: Undefined index: cookie in C:\Inetpub\finalphase\neverplaysober\v4\thankyou1.php on line 3

Warning: Cannot add header information - headers already sent by (output started at C:\Inetpub\finalphase\neverplaysober\v4\thankyou1.php:2) in C:\Inetpub\finalphase\neverplaysober\v4\thankyou1.php on line 4

my code..

<?
$cookie=$HTTP_COOKIE_VARS['cookie'];
setcookie ("cookie", "test", time()+3600);
?>

Mark Hensler's picture

He has: 4,044 posts

Joined: Aug 2000

You can't access $HTTP_COOKIE_VARS['cookie'] because you haven't yet set that cookie variable.

try lowering the error_level:

<?php
error_reporting
(7);
$cookie=$HTTP_COOKIE_VARS['cookie'];
setcookie ("cookie\", \"test\", time()+3600);
?>

Mark Hensler ["Max Albert"] [Email]
If there is no answer on Google, then there is no question.

They have: 34 posts

Joined: Feb 2002

ok, I've set up 2 pages.. one with this code.. The include page has a form that will be sent to a page that mails me the submited info. Then it's forwarded to another page that has a cookie.

Form page
------------------------------------------------------------

<?php
$challenge
= $HTTP_COOKIE_VARS["challenge"];
$challenge1 = $HTTP_COOKIE_VARS["challenge"];
if (
$challenge == "$challenge") {
print (
"Try again in 4hours!");
} else {
include
'challenge1.php';
}
?>

-----------------------------------------------------------
cookie page

<?php
setcookie
("challenge","yes",time()+14400,"/",".neverplaysober.com",0);
?>

Now the form page works fine If I visite the cookie page first. If I visit the form page first I get the following errors..

Notice: Undefined index: challenge in C:\Inetpub\finalphase\neverplaysober\v4\test\challenge.php on line 8

Notice: Undefined index: challenge in C:\Inetpub\finalphase\neverplaysober\v4\test\challenge.php on line 9
I like blue too!

Mark Hensler's picture

He has: 4,044 posts

Joined: Aug 2000

You can't access a cookie before it's set.

$challenge = $HTTP_COOKIE_VARS["challenge"];
This is assigning the value of a cookie named "challenge" to a variable named $challenge. At this point, there is no cookie named "challenged".

setcookie("challenge","yes",time()+14400,"/",".neverplaysober.com",0);
This is creating a cookie named "challenge". Only after you have created this cookie can you access it.

I think what you want is this:

<?php
if (isset($HTTP_COOKIE_VARS["challenge\"])) {
    // user has been here before
    print (\"Try again in 4hours!\");
}
else {
    include 'challenge1.php';
}
?>

Mark Hensler ["Max Albert"] [Email]
If there is no answer on Google, then there is no question.