Page 1 of 1

ERROR WITH COOKIES ON A WEBPAGES

Posted: Sun Nov 20, 2005 6:34 pm
by bxm_3
twigletmac | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

I get the following output for set_cookie.php below.

Code: Select all

tt 

weee


Warning: Cannot modify header information - headers already sent by (output started at /content/StartupHostPlus/o/l/olol.org.uk/web/set_cookie.php:7) in /content/StartupHostPlus/o/l/olol.org.uk/web/set_cookie.php on line 14

Warning: Cannot modify header information - headers already sent by (output started at /content/StartupHostPlus/o/l/olol.org.uk/web/set_cookie.php:7) in /content/StartupHostPlus/o/l/olol.org.uk/web/set_cookie.php on line 16

SEE COOKIE 
Usernames: 
Usernames
Here is the code, below

Code: Select all

jto
<br>
tt
<br>
<br>

<?php


echo "weee<br><br>";

$value = 'something from somewhere';
$values = 'something from somewhere';
setcookie('usernames',$value,time()+120);

setcookie("TestCookie", $value, time()+3600); 

?>

<br>

<a href="get_cookie.php">SEE COOKIE</A>
<br>
Usernames:<b><?php $_COOKIE['usernames'] ?></b>
<br>

Usernames:<b><?php $_COOKIE['TestCookie'] ?></b>
<br>
Here is the output for get_cookie.php

Code: Select all

jto 
tt 

weee

finished the cookie


Usernames:
Here is the code for get_cookie.php

Code: Select all

jto
<br>
tt
<br>
<br>

<?php


echo "weee<br><br>";

if($_COOKIE['usernames']=="")
{
 echo "finished the cookie<br><br>";
}


?>

<br>

Usernames:<b><?php $_COOKIE['usernames'] ?></b>
What is the error?

I am puzzled. :o

Posted: Sun Nov 20, 2005 6:44 pm
by Ambush Commander
All cookie setting has to be done before any content is passed to the browser.

Please use PHP tags when posting.

Posted: Sun Nov 20, 2005 7:11 pm
by bxm_3
Ambush Commander wrote:All cookie setting has to be done before any content is passed to the browser.

Please use PHP tags when posting.
I did what you said, the warnings have vanished but I cannot get the values for the cookies but I it does time out after a minute.

I get the following output for set_cookie.php:

**************************

jto
tt


SEE COOKIE
Usernames:
Usernames:

**************************



I get the following output for get_cookie.php after a minute

jto
tt

weee

finished the cookie

*************************************************
I get the following output for the cookie when it has existed for less than 1 minute.
******************************


Usernames:

jto
tt

weee


Usernames:

Posted: Sun Nov 20, 2005 7:14 pm
by Ambush Commander
You still aren't using php tags... :(

You're not echoing the cookie's contents:

Code: Select all

Usernames:<b><?php echo $_COOKIE['usernames'] ?></b>

2nd error

Posted: Sun Nov 20, 2005 8:01 pm
by bxm_3
Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I got the code to work but after 60 seconds the cookie is disabled but I want the browser to redirect.

Here is the code for get_cookie.php.

Code: Select all

<?php


echo "weee<br><br>";

if($_COOKIE['TestCookie']=="")
{
 echo "finished the cookie<br><br>";
 header("Location:feedback.php");
 ##exit;
}


?>

<br>

<!--Usernames:<b><?php echo $_COOKIE['usernames'] ?></b>-->
<br>

<br>
jto
<br>
tt
<br>
<br>

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sun Nov 20, 2005 10:15 pm
by John Cartwright
Please start using [syntax=php][/syntax] tags, and read the link in my signature titled Posting Code in the Forums

Re: 2nd error

Posted: Sun Nov 20, 2005 10:56 pm
by trukfixer
bxm_3 wrote:Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I got the code to work but after 60 seconds the cookie is disabled but I want the browser to redirect.

Here is the code for get_cookie.php.

Code: Select all

<?php


echo "weee<br><br>";

if($_COOKIE['TestCookie']=="")
{
 echo "finished the cookie<br><br>";
 header("Location:feedback.php");
 ##exit;
}


?>

<br>

<!--Usernames:<b><?php echo $_COOKIE['usernames'] ?></b>-->
<br>

<br>
jto
<br>
tt
<br>
<br>

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/quote]

Umm same advice as for setting cookie- do not send *any* output (html , error messages, etc,, even a single whitespace or newline) to the broswer before sending header information (cookies, header, session, etc  Otherwise you get an error..

Code: Select all

//WRONG !!!
if($_COOKIE['TestCookie']=="")
{
 echo "finished the cookie<br><br>";
 header("Location:feedback.php");
 ##exit;
}

//BETTER
if($_COOKIE['TestCookie']=="")
{
 //cookie expired, redirect
//echo "finished the cookie<br><br>"; - commented out - do *NOT* send any output to browser before header information
 header("Location:feedback.php");
 exit;//no need to comment- this is correct usage.. keeps this script from continuing execution (it could, but no point to it due to the header redirect) 
}

browser redirection not working

Posted: Mon Nov 21, 2005 7:20 am
by bxm_3

Code: Select all

<?php




if($_COOKIE['TestCookie']=="")
{
  
 ##cookie expired, redirect 
 ##echo "finished the cookie<br><br>"; - commented out - do *NOT* send any output to browser 
## before header information 
 header("Location:feedback.php"); 
 exit;
 ##no need to comment- this is correct usage.. keeps this script from continuing execution (it could, but no point to it due to the header redirect) 
 
}


?>

<br>

<!--Usernames:<b><?php echo $_COOKIE['usernames'] ?></b>-->
<br>

<br>
jto
<br>
tt
<br>
<br>
Here is the code above for get_cookie.php.

After 30 seconds I get the message on the browser

Warning: Cannot modify header information - headers already sent by (output started at /content/StartupHostPlus/o/l/olol.org.uk/web/get_cookie.php:3) in /content/StartupHostPlus/o/l/olol.org.uk/web/get_cookie.php on line 13

Could it be the exit() function(exit command)?

I think it is unlikely. :o

Posted: Mon Nov 21, 2005 7:27 am
by twigletmac
There is some output on line three (what the error message is telling you) - is there any whitespace above the opening <?php tag?

Mac

browser redirection error

Posted: Mon Nov 21, 2005 7:43 am
by bxm_3
I get 2 lines of white space before the warning:

Warning: Cannot modify header information - headers already sent by (output started at /content/StartupHostPlus/o/l/olol.org.uk/web/get_cookie.php:3) in /content/StartupHostPlus/o/l/olol.org.uk/web/get_cookie.php on line 13


Would it be better to call a function with the header redirection such as




Code: Select all

<? php
function go()
{
 header("Location:feedback.php"); 
 exit;
}
?>

Code: Select all

<? php
go()
?>

Posted: Mon Nov 21, 2005 7:46 am
by n00b Saibot
*remove all blank-lines/spaces before the <?php tag*

Posted: Mon Nov 21, 2005 8:11 pm
by Ambush Commander
Make sure you're not in unicode mode either. The BOM can mess things up.