WTF ... IS WTF!?
We are a collective of people who believe in freedom of speech, the rights of individuals, and free pancakes! We share our lives, struggles, frustrations, successes, joys, and prescribe to our own special brand of humor and insanity. If you are looking for a great place to hang out, make new friends, find new nemeses, and just be yourself, WTF.com is your new home.

cant get cookie info to display, why?

Blaze Daily

<b>Banned - What an Asshat!</b>
146
0
0
#1
I sure hope Junglism or Jamesp is reading this, I need a lil help.


if (isset($_COOKIE[date])) {
$last_visit = $_COOKIE[date] }
else { setcookie('date', etc...)
$lastvisit = $_cookie[date];
}

This, I assume, ought to work, but it does not work!

The basic idea is to record the last visit of a user to my website.

I did a check on what exactly is happening on the statement - basically isset($_cookie[date]) never returns true and therefore the cookie is set, even when there already is a cookie set???

It kinda works - the code sets the 'current' visit, not the last. Therefore, whenever a user visits my website, the cookie gets updated to present time.

This is not what I wanted, but I do understand one problem being how often to update the cookie when it IS set...but also another problem is actually displaying the cookie data on the first page load...is this possible? I've seen it happen on some VBulliten Forums, where the cookie data is retrieved from the client's machine first page load and shows the last visit. However, vBulletin does not seem to update their cookies very often? I dunno...this bugs me.

Why is my code crap? :D

Surely, the cookie is sent to the server when it hits my domain and path ( / ) then converted to a global variable, probably, therefore isset($_HTTP_COOKIE_VARS['date']) SHOULD RETURN TRUE!? why doesn't it?
 

Blaze Daily

<b>Banned - What an Asshat!</b>
146
0
0
#2
btw...I know I've already asked for help concerning file uploads, but I actually work on a website called robbiedave.com - it's nothing special, just a medium for me to learn website design. But these problems have persisted over a month now without solution (Thanks, Jung, btw for the fread code and image array location on the image data)

I do read the internet LOTS on trying to figure out these problems. I'm only posting the code problems that really bug the hell out of me for weeks on end.

Also, I intend on making a code resource with all my 'working' code for others to search and use, basically a 'how-to' make a simple website forum. There's already guides on making guest books, I want to create a guide on forum programming when I reach a conclusion to my programming efforts.

I really do hope some of you can help me, and I'll happily quote your name in the guide if you provide source and request to be included.

peace out
 

Awkward Silence

Perplexed Bemusement
99
0
0
#3
http://www.php.net/manual/en/features.cookies.php

try this:

<?php
if (!isset($_COOKIE['date'])) {
setcookie('date', etc...);
}
?>

that needs to be at the very top of the document, even before the <head> etc. and needs to have at least one blank line after it.

However - if you are trying to display the last visit time, this wont work, as the cookie will only be set the first time the user visits the page.
So, you need to make a conditional statement which updates the cookie. You need to update the cookie AFTER you've displayed its contents (see below), otherwise the cookie data will display the current time/date. To update the cookie, you will either need to use output buffering, or, place something like $cookie = $_POST['date']; before the setcookie function.

Cookies work on the basis that they automatically send the data to the page via POST each time the user visits that page. So, to display the cookie data;

<?php
$cookie = $_POST['date'];
echo $cookie;
?>

I think thats correct anyway, I'm sure jung will correct me if i'm wrong.
 

Jung

???
Premium
13,979
1,397
487
#4
No offense man, but you need to either buy a book or learn how to use php.net. That's how I learned. :thumbsup:


I don't have time to look at it right now, but I'll take a look later if nobody else has solved it.
 

Blaze Daily

<b>Banned - What an Asshat!</b>
146
0
0
#7
Junglsim: I read the cookie section, from php.net and the man pages on my Linux drive (I learned PHP from man pages in Linux) and the cookie wont display?

<?php
$cookie = $_POST['date'];
echo $cookie;
?>

Tried this at the top of my script, no joy?
...

Okies...I seem to be making progress. I created a cookie.php script dedicated to retrieving to cookie info. $_POST[date] doesn't get the cookie info, but $HTTP_COOKIE_VARS does, as does $_COOKIE

I have no idea what the problem was, or even if it's fixed, but now it actually gets the cookie info. I hardly changed the code!

if (isset($HTTP_COOKIE_VARS[date])) {
$lastvisit = $HTTP_COOKIE_VARS[date];
$lastvisit = $lastvisit . "True 1";
} else {
$dated = date("F j, Y");
$lastvisit = $dated;
setcookie("date", $dated,time()+604800);
$lastvisit = $lastvisit . "True 2";
}

This basically works now. I get firstly TRUE 1 if a cookie is set, or True 2 if there is no cookie set. I dunno why it wasn't working before - but now it does...I'm completely baffled as to why this did not work in the first place.

Btw...I intended to expire the cookie in a week or so, so basically when the cookie expires, the last visit will 'update' itself, because since the cookie expired no cookie will be found right? I hope this actually works now :)

Personally, I thought Junglism or Jamesp would give me a getcookie(); function, but it's not needed now. Thanks you guys for confirming what I already knew to be the truth and give me the strength to carry on.
 

jamesp

In Memory...
1,714
1
0
#8
Are you using sessions on any of the previous pages?

wait, nevermind.
 

Blaze Daily

<b>Banned - What an Asshat!</b>
146
0
0
#9
jamesp,

Actually, yes for a while I was using sessions - would this conflict with setcookie?
 

jamesp

In Memory...
1,714
1
0
#10
I suppose it could if you initialize the cookie in a page with sessions and try to recall on a page without sessions....but then again maybe not. PHP is funny like that.
 

wootness

Banned - What an Asshat!
232
0
0
#11
blazedaily is to stoned to even know whats going on.