Trying To Set A Variable From Included File

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
AWayne
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2008 1:53 pm

Trying To Set A Variable From Included File

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trying To Set A Variable From Included File

Post 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.
(#10850)
AWayne
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2008 1:53 pm

Re: Trying To Set A Variable From Included File

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Trying To Set A Variable From Included File

Post 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. ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trying To Set A Variable From Included File

Post by Christopher »

What's the "global keyword"? I've never used that? ;)
(#10850)
Post Reply