Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

about to get nuts

Status
Not open for further replies.

jamesp0tter

Programmer
Feb 20, 2003
119
PT
well, i'm stuck in a code for hours and just can't figure out what's wrong.. i have this:

Code:
<?
if ((!$_SESSION['membro']) || ( $_SESSION['membro'] == '0' )) { unset($MEMBRO); } else { $MEMBRO = $_SESSION['membro']; }

// now, i'm sure $MEMBRO is set because other scripts use this var and they work fine

// then i have this:

function bla () {
if (!$MEMBRO) { return 'not set'; } else {
  return 'set to '.$MEMBRO; }
}
?>

and, guess what... the function returns &quot;not set&quot; :) now, how is this possible ? o_O

oh, if i erase the if clause and just leave
Code:
  return 'set to '.$MEMBRO;
the functions returns correctly, including the $MEMBRO var that accordingly to php's if clause isn't set.

i just don't see any logic in this.

any help appreciated, thanks :]

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
same thing... with if ($MEMBRO) or if (isset($MEMBRO)) the output is the same :\
jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
yup.. i'm going 2 give u all code:

Code:
<?
session_start();

if ((!isset($_SESSION['membro'])) || ( $_SESSION['membro'] == '0' )) { unset($MEMBRO); } else { $MEMBRO = $_SESSION['membro']; }

// -------------------------

function login () {
if (isset($MEMBRO)) { return 1; } else { return 0; }
}

?>
(i changed it, more easy to understand now)

well, login () returns 0 if $MEMBRO is set or if it isn't.

if u can't understand this (like me), could someone please give me a code that works ? :x i'll try to re-do everything, if i get some positive results i'll post them here.

any help is appreciated ;]

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
ok it works now...

Code:
<?
session_start();


function login () {
    
 if (isset($_SESSION['membro'])) $MEMBRO = $_SESSION['membro'];



 if ((isset($MEMBRO)) && ($MEMBRO != 0)) { return 1; } else { return 0; }
}

function now returns 1 and 0 when it should, lol :x

tks all :]

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
Your problem was cause you were refering to a var outside her scope.

If you set a var outside a function and try to refer to it inside a function the var is unset, unless you define inside the function the line:
global $MEMBRO;

The $_* arrays are global arrays, that's why you don't have problems.

Another thing, why do you convert the $_SESSION[&quot;membro&quot;] to $MEMBRO? You loose a global array, and you loose track from the origin of the var. In my opinion you should refere, in all the code, with $_SESSION[&quot;membro&quot;]. This way you now that the origin of the var is the Session (weel, you can now all the time, but if someone need to read your code he can be lost).

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin@anikin-skywalker.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top