Jump to content

global not working!!


jaymc

Recommended Posts

I am using a paypal API which I have tested and works fine

 

I am now integreating it into my website and it is no longer!

 

I have debugged as much as I can and what it has come down to is this

 

There is a function which has

 

	function hash_call($methodName,$nvpStr)
{
	//declaring of global variables
	global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature;

 

None of those variables contain any data when echoed inside the function..

 

If I echo them outside the function, it works fine

 

So the conclusion but not the solution is that something somewhere is not allowing me to call a global.

 

Is there anything that would cause this? if so I can then check through all the layers of my code for the problem child.

Link to comment
Share on other sites

<?

$API_UserName="dummy@dummy.com";
$API_Password="12345";
$API_Signature="qwertyqwertyqwety";

// BN Code 	is only applicable for partners
$sBNCode = "PP-ECWizard";

function hash_call($methodName,$nvpStr)
{
	//declaring of global variables
	global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature;


echo $API_UserName; // DOES NOT WORK
}

echo $API_UserName; // DOES WORK

?>

 

Polluting global namespace? Hmm, how may I have done that?

 

Thats what I am looking for really, the reason that may cause global to lose functionality?

Link to comment
Share on other sites

It works for me. You're not calling the function in the code you posted though.

 

Yes it works for me.. I have a working version of the whole thing

 

But when I simply plug it into my website, the globals stop working

 

I havn't touched the code, simply copy and pasted

 

So obviously something else where in my code is causing global to die... What are the possibilities? I don't even know where to begin

Link to comment
Share on other sites

So obviously something else where in my code is causing global to die... What are the possibilities? I don't even know where to begin

 

Which is precisely why it's bad practice. You have no real control over what writes to it when, and it's a pain to debug. I suppose you can always step through the code execution manually until you figure out what resets it.

Link to comment
Share on other sites

So obviously something else where in my code is causing global to die... What are the possibilities? I don't even know where to begin

 

Which is precisely why it's bad practice. You have no real control over what writes to it when, and it's a pain to debug.

 

Its how paypal have written most of there API's

 

In fairness I can echo the variables immediately before the function call and immediately after the function call

 

Just not inside the function... so I don't think anything is overwriting there values?

Link to comment
Share on other sites

I don't know how your script looks, but global will only import things from the global namespace, not from the local namespace where the function was called. So something like this will not work:

 

<?php
function foo()
{
global $bar;
echo $bar;
}

function bar()
{
$bar = 'foo';
foo();
}

bar();

Link to comment
Share on other sites

A file called paypal.php

 

<?
$API_UserName="seller_1256141718_biz_api1.hotmail.com";
$API_Password="1256141861";

function beees() {
	global $API_UserName, $API_Password;

	echo $API_UserName;
	echo $API_Password;
	echo "1234567<br><br>";

}
?>

 

a file called index.php

 


	require_once("funcs/paypalfunctions.php");
	echo "1 $API_UserName<br><br>";
	beees();
	echo "3 $API_UserName<br><br>";

die();

 

 

I get this

 

 

1 seller_1256141718_biz_api1.hotmail.com

 

1234567

 

3 seller_1256141718_biz_api1.hotmail.com

 

Hence, $API_UserName and $API_PassWord are not being echoed inside the function

Link to comment
Share on other sites

Why exactly is it you don't just pass variables by argument?

 

Because I dont want to screw with paypals API or attempt to rewrite it, especially as they update it

 

It should work, its tried and test.

 

It is just a case of something conflicting which i must find.

Link to comment
Share on other sites

Hmm. I have an idea why it may be although no idea why it would

 

in my index file I have this

 

 

$html->body(cpanel_module_loader(file.php));

 

cpanel_module_loader("browser") is a function which will open file.php which  is what contains all that code I gave you...

It will then echo it to screen

 

It seems that because its a case of a function running inside another function.. as obviously if I just call file.php direct it all works fine.

 

Is this a rule of php that global calls do not work if you are inside another function.

 

 

The function cpanel_module_loader uses globals... is this why?

 

function cpanel_module_loader($file) {

 

global $html, $my, $client, $user;

 

include($file); $file includes other files which have functions which do not appear to have working globals!

}

Link to comment
Share on other sites

What does a phpinfo(); statement show for register_globals on both the system where it works and on the system where it does not work?

 

[rant]

Perhaps someone figured out how to disable the global keyword to stop people from writing bad code that takes hours to troubleshoot, instead of a few minutes.

[/rant]

 

Edit: As to your latest post, you do realize that when you don't post your actual code that is exhibiting the symptoms, that NO ONE can help you with what your actual code is doing.

Link to comment
Share on other sites

Edit: As to your latest post, you do realize that when you don't post your actual code that is exhibiting the symptoms, that NO ONE can help you with what your actual code is doing.

 

I was hoping someone could just say .. Are you running this function inside another function?

 

As no one knows any obvious causes I have stripped out my code to try and find the root culpret

 

Is that the reason then? Because I have functions nested in functions.

Link to comment
Share on other sites

I was hoping someone could just say .. Are you running this function inside another function?

 

Isn't that pretty much a paraphrase of this post?

 

Didnt see that post! Cool so now I know

 

What a pain :(

 

What are the ways around it other than using register globals

 

Any other solutions

Link to comment
Share on other sites

Your by far best option is to pass things by argument.

 

Exactly right.  What does:

 

$API_UserName = "seller_1256141718_biz_api1.hotmail.com";
$API_Password = "1256141861";

function beees($userName, $userPass)
{
   echo $userName;
   echo $userPass;
   echo "1234567<br><br>";
}

echo "1 $API_UserName<br><br>";
beees($API_UserName, $API_Password);
echo "3 $API_UserName<br><br>";

die();

 

Give you?

Link to comment
Share on other sites

What are the ways around it other than using register globals

Register globals would not cause your code to work. However, register_globals being on could have accounted for the program variables being overwritten if you had some post/get/cookie/session variables with the same name as your program variables.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.