Page 1 of 1

Trying To Set A Variable From Included File

Posted: Mon Mar 03, 2008 1:59 pm
by AWayne
Hi,

I'm trying (with no luck) to get a variable from an included file to set a variable in another file.

The code:

Code: Select all

 
include "config_vars.php";
function cbValid($rcpt, $time, $item, $cbpop){
$key="$secret_key";
$xxpop=sha1("$key|$rcpt|$time|$item");
$xxpop=strtoupper(substr($xxpop,0,8));
if ($cbpop==$xxpop){
return 1;
} else {
return 0;
}
}
$rcpt = trim(addslashes($_GET['cbreceipt']));
$time = trim(addslashes($_GET['time']));
$item = trim(addslashes($_GET['item']));
$cbpop = trim(addslashes($_GET['cbpop']));
 
In the code above you'll see...

$key="$secret_key";

$secret_key is a variable found in the included "config_vars.php" file. The problem is that even though
I can echo "$secret_key" and see it on that page, I cannot set the variable $key with the $secret_key.

In other words, $key is empty.

Any help would be greatly appreciated.

Thanks.

Anthony

Re: Trying To Set A Variable From Included File

Posted: Mon Mar 03, 2008 4:07 pm
by Christopher
You need to pass the value in:

Code: Select all

include "config_vars.php";
function cbValid($rcpt, $time, $item, $cbpop, $secret_key){
     $key = $secret_key;
// ...
}
And you don't need to quotes.

Re: Trying To Set A Variable From Included File

Posted: Mon Mar 03, 2008 4:30 pm
by AWayne
Thank you very much.

Still new to PHP as evidenced by the fact that I couldn't figure this out.

Again, thank you for your time.

Anthony

Re: Trying To Set A Variable From Included File

Posted: Mon Mar 03, 2008 6:52 pm
by RobertGonzalez
Quick note for you, what you have going on here is scope resolution. You are trying to access a variable within the scope of a function. That is why it needs to be passed in.

And if anyone tells you to use the global keyword, tell them that the gurus at devnetwork told you not to. ;)

Re: Trying To Set A Variable From Included File

Posted: Mon Mar 03, 2008 7:11 pm
by Christopher
What's the "global keyword"? I've never used that? ;)