ERROR WITH COOKIES ON A WEBPAGES

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
bxm_3
Forum Newbie
Posts: 13
Joined: Sun Nov 20, 2005 6:23 pm

ERROR WITH COOKIES ON A WEBPAGES

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

All cookie setting has to be done before any content is passed to the browser.

Please use PHP tags when posting.
bxm_3
Forum Newbie
Posts: 13
Joined: Sun Nov 20, 2005 6:23 pm

Post 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:
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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>
bxm_3
Forum Newbie
Posts: 13
Joined: Sun Nov 20, 2005 6:23 pm

2nd error

Post 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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Please start using [syntax=php][/syntax] tags, and read the link in my signature titled Posting Code in the Forums
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Re: 2nd error

Post 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) 
}
Last edited by trukfixer on Mon Nov 21, 2005 9:00 am, edited 1 time in total.
bxm_3
Forum Newbie
Posts: 13
Joined: Sun Nov 20, 2005 6:23 pm

browser redirection not working

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
bxm_3
Forum Newbie
Posts: 13
Joined: Sun Nov 20, 2005 6:23 pm

browser redirection error

Post 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()
?>
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

*remove all blank-lines/spaces before the <?php tag*
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Make sure you're not in unicode mode either. The BOM can mess things up.
Post Reply