Jump to content


Photo

php cookie


  • Please log in to reply
5 replies to this topic

#1 HEki_

HEki_

    Young Padawan

  • Members
  • Pip
  • 167 posts

Posted 24 July 2011 - 08:57 AM

Hi,

i made a simple webpage for a friend of mine with only 3 pages ... its realy simple.
nothing fancy. i added a php hit counter on the page and now everytime i hit refresh,
the counter gets +1. so i decided to use a cookie.

I read that i have to put the cookie on the top of the page, before any html code, but
i still get "Cannot modify header information ..." error.

I dont know what i am doing wrong.

<?php
setcookie ("zadnji_obisk", "obisk", time()+300);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

And here is the counter script.

<?php
$fil = fopen('stevec.txt', r);
$dat = fread($fil, filesize('stevec.txt')); 
if(isset($_COOKIE['zadnji_obisk'])) {
echo $dat;
	
} else {

echo $dat+1;
fclose($fil);
$fil = fopen('stevec.txt', w);
fwrite($fil, $dat+1);
}
?>

Can anyone point out what i should do?

Regards,
HEki

#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 24 July 2011 - 10:39 AM

I'm curious to know why you wanted to add a cookie to a counter script. That doesn't seem like it would be too useful.

But either way, without seeing all of the files it would be hard to say with this is happening. If anything is output before the call to setcookie(), you will get that error. Even if the output is in a different file that is included before the file you are working in.

#3 HEki_

HEki_

    Young Padawan

  • Members
  • Pip
  • 167 posts

Posted 24 July 2011 - 11:56 AM

Oh,

well in counter script i check if the cookie is set. that way i only read the
number from file, if its not set, than i increse the number ...

i am newb :D so there might be another way to increase the number ...

http://www.monter-sivka.com this is the site ... now i dont use setcookie, couse
its giving me that error and the counter keeps going up :)

#4 HEki_

HEki_

    Young Padawan

  • Members
  • Pip
  • 167 posts

Posted 24 July 2011 - 01:56 PM

Hi,

i have found a solution. It seems, when i edit the code in notepad, that it adds something infront of <?php ...
i selected the whole page from that 2nd line, created a new document in dreamviwer, manualy added <?php, than
copied the code and saved it ... Now its working again ...

Realy strainge!

Thanks for help!

here is a picture
Posted Image

Edited by HEki_, 24 July 2011 - 02:00 PM.


#5 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 24 July 2011 - 10:23 PM

Oh, right. Cookie to avoid updating the counter too much. I see. For some reason i thought you went somewhere else with that.

Anyway, that's weird to see notepad messing up a plain text file. It usually doesn't do things like that. Perhaps you saved it with a weird encoding?

#6 HEki_

HEki_

    Young Padawan

  • Members
  • Pip
  • 167 posts

Posted 24 July 2011 - 11:33 PM

i saved it with utf-8 encoding ... than i switched to asci and it was gone ... but along
with that i also lost čšž xD but a side that, its working now :)

thank you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users