FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Setting cookies won't work





Arno v. Lumig
Hello everyone,

I am rather new to PHP, but I do have some (well, more then some) C++ experience.

On my site I wanted to tell the user how many new articles/blog posts/3d artworks/links there are since his/her last visit. I thought this would be handy to do with cookies.

The code I am using is this:
Code:
<?php setcookie("tutorialseen", 7, time()+3600*500);?>
<?php include("header.php"); ?>


Explanation: I am setting a cookie named "tutorialseen", the value should be 7 (current amount of tutorials), and the time is 500 hours (thought that would be long enough...).

Then I am including header.php.
header.php contains the banner, the menu and (not working though...) the amount of new items on the webpage.
These items are stored in a simple text file, so it's easier to modify.
The source of header.php is:

Code:
   <?php
      $visited=true;
      $file = fopen("items.txt","r");
      $currentblog = fgets($file);
      $currenttutorial = fgets($file);
      $current3d = fgets($file);
      $currentlink = fgets($file);

      if (isset($_COOKIE["3dseen"]))
         $artseen = $_COOKIE["3dseen"];
      else
         $visited=false;
         echo "<marquee> There are " . $currenttutorial . " new tutorials, " . $current3d . " 3D artworks and " . $currentlink . " links waiting for you! </marquee>";
         
      if (isset($_COOKIE["tutorialseen"]))
         $tutorialseen = $_COOKIE["tutorial"];
      if (isset($_COOKIE["linkseen"]))
         $linkseen = $_COOKIE["linkseen"];
      if($visited==true)
         {
         echo "<marquee> There are " . 
         $currenttutorial-$tutorialseen . " new tutorials/articles, " . $current3d-$artseen .
         " new 3D artworks and " . $currentlink-$linkseen . " new links! </marquee>";
         }
   ?>


This is a rather simple piece of code I guess. It outputs the amount of items on the page if the cookie is not found, else it will show how many new items there are.

Now, that's all code that matters. The problem is that I'm getting this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/arnovlum/domains/arno.frih.net/public_html/tutorials.php:Cool in /home/arnovlum/domains/arno.frih.net/public_html/tutorials.php on line 8

Line 8 is:
Code:
<?php setcookie("tutorialseen", 7, time()+3600*500);?>


That is the first time on that webpage where I use php. It's the first time anything gets done with cookies.

After that line there is the include header.php, which works, except that it think it's the first time I visited it (that's not really surprising, as there is an error in setting the cookie).


I hope you can help,
Arno
DanielXP
do u have

Code:
<?php ob_start(); ?>


at the very very top?
Arno v. Lumig
No, I do not :S

Thanks for your reply, unfortunately I can't test it at the moment because I have to go take a shower now Laughing.

I'll test it tomorrow,


Yours,
Arno
hexkid
After you solve your header problem (with ob_start() or by making sure there is no output before any header), I think you will bump into the problem of the cookie not being set anyway :)

You cannot set a cookie and use it on the same page request. The cookie will only be available on the requests following the one where it was set.

1) Client: requests page (without cookies)
2) Server: sends response and sets cookies for the client
3) Client: requests page (with cookies)
4) Server: sends response (and maybe updates cookies)

On step 2) you have
Arno v. Lumig wrote:
Code:
<?php setcookie("tutorialseen", 7, time()+3600*500);?>
<?php include("header.php"); ?>
and (in header.php)
Arno v. Lumig wrote:
Code:
      if (isset($_COOKIE["tutorialseen"]))
         $tutorialseen = $_COOKIE["tutorial"];

This if() will not be true on step 2); it will only be true on step 4) (and all the following steps).
Arno v. Lumig
The problem is solved, thank you Smile


Greetz, Arno
Related topics
Introduction To Cookies :
PHP: Sessions and Cookies
Setting up POP email to work with our subdomains
Make me a riddle-for 50 Frih$ (Closed - winners announced!)
th 2038 bug and google
Problem with one of my computers....
PHP and cookies. Simple problem.
USELESS bill to provide support to those who won't work.
User Tracking
Acces another site which needs login automatically via php..
Using a computer in the sun, under a lot of heat.
Trying to master night-time pics....
Setting up a cookiless domain or subdomain?
Discrimination against Christian - a real case :-)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.