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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opening a cookie the 2nd time doesnt work?

Status
Not open for further replies.

altctrldel

Technical User
Jul 26, 2003
30
well at the top there exists;

Code:
$cookie_read = explode("|", base64_decode($user));
// define variables to hold cookie values.
$userid = $cookie_read[0];

and then further down (in another function) I want to get the exact same data so I try to use the same codes again but it wont work. I try printing the variable but its empty.
 
Since you're mentioning functions, are you sure that your variable exists within that function? Read up on variable scope in PHP manual.
 
the 1st time it read was inside a function. <<--- it exists in this one. I can print it out.

then the 2nd time i wanted to read it was in another function but it just wont show up.
 
The link I gave you before said:
Any variable used inside a function is by default limited to the local function scope.
So, your variable exists in the first function where it is populated (local function scope) but not in the second one (which would mean no longer local function scope).

Since you apparently decided to ignore the link the first time, I will just tell you that link lists just about every possible solution on how to conquer the problem you're having. The only one it doesn't is the use of constant -- constants are global in scope and can be used everywhere. But that depends on whether your cookie can be defined as a constant (it is constant throughout) or not (it changes, like a variable). I would give you a link to constants, but I hardly see the point.
 
Yes i did go to the link you posted. I did read it, but it was just that I didn't look deep enough. Ok... so far so good I got the correct working example;

Code:
<?php
$a = 1;
$b = 2;

function Sum()
{
   global $a, $b;

   $b = 132;
   echo "orig: $b <br>";
}

Sum();

function Go(){
	global $b;
	echo $b;
}

Go();
?>
the variable $b was given a new value inside a function and called using another function. This worked and it printed "132".

Now the funny this is that I used the same principles with my actual script and it only returns the ORIGINAL value when $b was declared. (in this example: 2). weird.... but i'll still try harder to make it work.

thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top