Cookie Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hammer
Forum Newbie
Posts: 2
Joined: Thu Dec 30, 2004 1:22 am

Cookie Problem

Post by hammer »

Hello, i am expreriencing strange problems with cookies.
For example when i use:
$GLOBALS['something']
it works on my apache2 and php 4.4.0 while $HTTP_COOKIE_VARS['something'] works only sometimes. ... why is that?

also on my other machine apache 1.3.28 and php 4.3.3
$GLOBALS['something'] dont work at all .... but $HTTP_COOKIE_VARS['something'] still works sometimes ...... whyyyy?????

i really need to know some way that will work on every machine i try it ...
please help...
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

read up on "register globals"

i think your problem of $GLOBALS['something'] only working sometimes is because you are depending upon register globals, or do not understand how the $GLOBALS array is structured. to have a look, do
print_r($GLOBALS);

you should also use the new superglobals instead of the old $HTTP_*_VARS
for example, use $_COOKIE

http://www.php.net/en/language.variables.predefined
hammer
Forum Newbie
Posts: 2
Joined: Thu Dec 30, 2004 1:22 am

Post by hammer »

okay i've replaced everything with the $_COOKIE variable but still no result here is some of the code:

Code: Select all

<?php

  function getforums(){
      // if has needed userlevel then can add forums!
      if($GLOBALS['logged'] == 1) {
        if (get_user_lvl_by_username($GLOBALS['user']) > 4) echo"<center><a href='functions.php?action=post_forum&vars='>New Forum</a></center>";
      } else echo "Not Logged!"; 
  }
?>
the function checks if the user is logged and does he have enought level to start a new forum thread .... but its not even close to working on my linux machine php 4.3.3
the same code works on my windows php 4.4.0

here is the code that works on both machines :

Code: Select all

<?php
 if($_COOKIE['logged'] == 1) {
    echo"<a href='$homepage/forum/functions.php?action=logout&vars='>Logout (" . $HTTP_COOKIE_VARS['user'] . ")</a><br>";
  }
  else {
    echo"<a href='$homepage/forum/functions.php?action=login&vars='>Login</a><br>"; 
  }

// if user is loged then give logout option and display username else give login option
?>


the first one works only on my windows php 4.4.0
and the second one works on my windows php 4.4.0 and on my linux 4.3.3
...
any ideas?
tomtomtom
Forum Newbie
Posts: 8
Joined: Tue Dec 28, 2004 12:39 pm

Post by tomtomtom »

Stick with $_COOKIE - it's the method you should always use to access cookie data.

Never rely on globals for GET / POST / COOKIE data - it's a security risk to have and use register_globals, so on many systems (including one of yours), it's turned off.
Post Reply