Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookie

Status
Not open for further replies.

help120

Technical User
Apr 15, 2001
198
US
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 (&quot;cookie&quot;, &quot;test&quot;, time()+3600);
?>
 
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[&quot;challenge&quot;];
$challenge1 = $HTTP_COOKIE_VARS[&quot;challenge&quot;];
if ($challenge == &quot;$challenge&quot;) {
print (&quot;Try again in 4hours!&quot;);
} else {
include 'challenge1.php';
}
?>

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

<?php

setcookie(&quot;challenge&quot;,&quot;yes&quot;,time()+14400,&quot;/&quot;,&quot;.neverplaysober.com&quot;,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!
 
go to the PHP.ini and set error_reporting to:

error_reporting = E_ALL & ~E_NOTICE

that will remove that stupid warnings.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Still does not fix my problem.. my form &quot;challenge1.php&quot; does not show up. If I go to the cookie page first then go to challenge.php then I get &quot;try again in 4hrs&quot;.
 
From what I can tell the cookie is not on my pc so how is the code going to call it? It can't so I get an error. How do I go about fixing this problem?
 
Without knowing the purpose of the scripts, its hard for me to know what it's supposed to be doing. I'd want to know the name of each of the pages involved, the order that the user would encounter them (in a normal flow and in one that encounters error).

What I'm assuming you want is for it to set a cookie if there isn't one already there. If there is one there already, then you read it's value and do a comparison on it. This is the only way around having the script try to read from a cookie that isn't there. There's no test for the cookie in any of the scripts I see, so that part isn't happening.

Also, in the form page PHP, it looks like the comparison is ALWAYS going to evaluate TRUE, therefore it will never include challenge1.php:

&nbsp;&nbsp;&nbsp;&quot;if ($challenge == $challenge)&quot;

...will always be true. That may be a typo though... maybe it's supposed to be &quot;if ($challenge == $challenge1)&quot;? Even still, $challenge and $challenge1 are both read from the same cookie:

&nbsp;&nbsp;&nbsp;$HTTP_COOKIE_VARS[&quot;challenge&quot;];

...so $challenge and $challenge1 are going to be the same, making the comparison the same, and always printing &quot;try again&quot; instead of including challenge1.php.

Also, if you're going to set a cookie, it has to be the FIRST THING that the page outputs to the browser. The reason is that the cookie info has to come through with the HTTP header. If you've output ANYTHING (using any echo commands, or if any scripting errors send error text to the browser) then the HTTP headers have already been sent, and you can no longer set a cookie. This may be your problem: the undefined index notice might be botching the cookie-setting portion of your scripts, as the undefined index notice is causing HTTP headers to be sent before the cookie.

Just some thoughts...
 
It looks like you are sending some other form of headers before it (i.e echoing something). The way I do it is;

<?php

main_set_function();

function main_set_function() {

setcookie(COOKIENAME, $variable);
show_html();

}

function show_html() {

echo &quot;now you can show your HTML here&quot;;

}

?>

That is my understanding of your problem anyway :p

Andy
 
I got it.. thanks guys..

<?php
if (isset($HTTP_COOKIE_VARS[&quot;challenge&quot;])) {
// user has been here before
print (&quot;Try again in 4hours!&quot;);
}
else {
include 'challenge1.php';
}
?>
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top