Re: [PHP] Not counting my own page visits

[email protected] (Ashley Sheridan)
Newsgroups php.general
Message-ID <[email protected]>
On Tue, 2013-03-05 at 10:19 +1030, David Robley wrote:

> Angela Barone wrote:
> 
> > On Mar 4, 2013, at 11:33 AM, Ashley Sheridan wrote:
> >> You can manually write a cookie on your machine, or use a special script
> >> that only you visit that contains a setcookie() call (it only need be set
> >> once). From there on, you can check the $_COOKIES super global for the
> >> presence of your cookie.
> > 
> > I don't know why, but I can't get cookies to work.  Here's a script I'm
> > calling from my browser:
> > 
> > <?php
> >     $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ?
> >     $_SERVER['HTTP_HOST'] : false;
> > $cookie = setcookie('test2', '123' , time()+60*60*24*30, '/', $domain);
> > ?>
> > 
> > <!DOCTYPE html>
> > <html lang="en">
> > <head>
> > <meta charset="utf-8" />
> > <title>Test Page</title>
> > </head>
> > <body>
> > <?php echo 'Cookie is: '.$_COOKIE[$cookie]."<br>"; ?>
> > <?php echo 'Domain is: '.$domain."<br>"; ?>
> > </body>
> > </html>
> > 
> > The domain is being displayed but the cookie is not.  There's no cookie in
> > the browser prefs, either.  What am I doing wrong?
> > 
> > Angela
> 
> Misunderstanding what $cookie contains? It is a boolean, i.e. it will be 
> true or false depending on whether the cookie was set or not. To echo the 
> contents of a cookie, you need to use the cookie name, viz
> 
> <?php echo 'Cookie is: '.$_COOKIE['test2']."<br>"; ?>
> 
> -- 
> Cheers
> David Robley
> 
> Oxymoron: Sisterly Love.
> 
> 


Not just that, but if you set a cookie, you won't be able to retrieve it
in the same script I believe. It's only available in the $_COOKIES array
once you refresh the page, as that's when the super global is populated
from the cookie data that the browser sends.

Thanks,
Ash
http://www.ashleysheridan.co.uk
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.