homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

PHP Cookies

 7:20 pm on May 22, 2012 (gmt 0)

Here's something I've just noticed with the system I've created.

I've used Print_r ($_COOKIE); to display what cookies are set, here's what I get


Array ( [userid] => 3 [username] => becky.flintstone [access_level] => 1 [company] => 1 )


Array ( [activatedmode] => mechanical [g_mode] => 1 [userid] => 3 [username] => becky.flintstone [access_level] => 1 [company] => 1 )

This is using exactly the same files. So In apache virtualhosts both names point to the same directory.

How come there are some extra cookies when accessing remotely?

Also I've noticed that this doesn't work when accessing remotely but does locally:

<?php if ($_COOKIE['access_level'] == 1) { ?>
javascript code
<?php } ?>



 5:24 pm on May 23, 2012 (gmt 0)

Are you using analytics or any other third party apps that set a cookie? Or maybe it's your server software, are you logged in to some control panel while you're looking?

PHP doesn't read "PHP cookies" (Or better said, it's not just PHP that sets and gets cookies.) It reads all cookies for the given domain set in the browser. So obviously something else on your remote server is setting cookies.


 7:56 pm on May 23, 2012 (gmt 0)

Here's what I use to set the cookies:

setcookie("userid",$users['users_id'], mktime(23, 59, 59, date("m"), date("d"), date("y")));
setcookie("access_level",$users['users_access_level'], mktime(23, 59, 59, date("m"), date("d"), date("y")));
setcookie("company",$_POST['company'], mktime(23, 59, 59, date("m"), date("d"), date("y")));

The same code is used whether access the site localling using [intranet...] or remotely using [remote.domain.com...]

I am not using any third party apps within my software. There's no control panel, just my own written code.

Here's a few lines from my Apache httpd-vhosts file:

<VirtualHost *:80>
ServerName [intranet...]
DocumentRoot c:/www/webroot/intranet

<VirtualHost *:80>
ServerName [remote.domain.com...]
DocumentRoot c:/www/webroot/intranet


 4:33 pm on May 24, 2012 (gmt 0)

You're setting username too, aren't you?

What about analytics? (g_mode, hmmmm . . . .)

Something is setting the g_mode and activatedmode cookies on your domain. Look at where it's hosted, how the domain is set up, clear cookies for your domain, not all. (FireFox Tools -> Options -> Privacy tab) After clearing cookies, visit the site and look again, you will probably find cookies have been reset again.

Check it in other browsers too. It could even be some plugin/add-on in your browser of choice.

A lot of things can set cookies, and when you read $_COOKIE it will show all of them, whether set by your program or not. Analytics code and/or other Javascript, AVG software, server software . . . it's coming from somewhere and not from your program. :-)


 10:06 pm on May 24, 2012 (gmt 0)

I was setting usernames, I removed them as I realised I wasn't actually using them at all.

I don't use any analytics on my intranet system.

After clearing the cookies from my domain they have disappeared. I can't get them to reproduce either.

Do you have any idea why my IF statement doesn't work remotely?

<?php if ($_COOKIE['access_level'] == 1) { ?>
do something in here
<?php } ?>


 4:59 pm on May 29, 2012 (gmt 0)

If you execute that immediately after setting it (as in, within the same script or something), that is why. You can't set and get a cookie in the same page load. The cookie will only be available to read on the page **after** it's been set.


 5:25 pm on May 29, 2012 (gmt 0)

The cookies are set when the user logs in and then throughout various pages within the site I use the code above. I'll do some more testing and see what I can come up with.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved