amarques Posted March 7, 2008 Share Posted March 7, 2008 Hi, This is probably a very basic question to many of you, but I'm just starting in PHP. I need for a variable to be used on a remote file that is included and a new variable from this included file to be used on the script. I'll try to demonstrate: On my file1.php I have <?php $c=4; include 'http://www.example.com/file2.php'; $multiply=$sum*2; echo $multiply; ?> On my file2.php: <?php $a = 2; $b= 6; $sum=$a+$b+$c; ?> It is always returning 0 as in multiplication of 2 against and empty $sum variable. What am I doing wrong? What is the best way to do such a thing? I'm sure there is a way to do it, probably altering the scope of the variables, but again, I'm just starting and would really need your advice. Thanks for any help.[pre][pre][/pre][/pre] Quote Link to comment Share on other sites More sharing options...
conker87 Posted March 7, 2008 Share Posted March 7, 2008 You can't include php script files that are on remote servers, at least, you shouldn't. Think of the security flaw! Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted March 7, 2008 Share Posted March 7, 2008 try this: file1.php <?php $c=4; include 'file2.php'; runFunction($c); ?> file2.php <?php function runFunction($c){ $a = 2; $b= 6; $sum=$a+$b+$c; return $sum; } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2008 Share Posted March 7, 2008 When you include a remote file you only get any HTML output produced by that file. So, the question becomes, not what are you doing, buy why are you doing this? There is likely a more proper way of doing what you want, if you tell us what it is you are trying to accomplish. Quote Link to comment Share on other sites More sharing options...
amarques Posted March 7, 2008 Author Share Posted March 7, 2008 Northern Flame: Won't work... Fatal error: Call to undefined function runFunction() PFMaBiSmAd: It's a little hard to explain, but I'll try. I need to have a script on server websites. Depending on some conditions that are inserted into the variables, I need the script to request a query on the database of another site and that other site answering with the result. You see any way of doing this without including files? How would you do it? Quote Link to comment Share on other sites More sharing options...
revraz Posted March 7, 2008 Share Posted March 7, 2008 Sites in same domain or different? Quote Link to comment Share on other sites More sharing options...
bpops Posted March 7, 2008 Share Posted March 7, 2008 The best way would be to generate a GET post to the script you're running (the one you were going to put in include), then print the results. Quote Link to comment Share on other sites More sharing options...
amarques Posted March 7, 2008 Author Share Posted March 7, 2008 Sites in same domain or different? Different... otherwise I would do the query directly... Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2008 Share Posted March 7, 2008 Actually, the question should be is the same database server accessible by all the domains? Different domains under the same hosting will all likely have access to the database server. Quote Link to comment Share on other sites More sharing options...
amarques Posted March 7, 2008 Author Share Posted March 7, 2008 The best way would be to generate a GET post to the script you're running (the one you were going to put in include), then print the results. Can you give a code example? (Yes, I'm that new). Thanks Quote Link to comment Share on other sites More sharing options...
amarques Posted March 7, 2008 Author Share Posted March 7, 2008 Actually, the question should be is the same database server accessible by all the domains? Different domains under the same hosting will all likely have access to the database server. This are external domains not hosted on the same server. So, no access to the database. That is why I need the query to be run from a file on the server with the database. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2008 Share Posted March 7, 2008 If the mysql server allows external/remote access you could do this directly by just putting in the proper host in the mysql_connect() function. If not, there are several existing "remote mysql helper" scripts that include security. Here is a link to one such script - http://www.phpclasses.org/browse/package/4000.html Quote Link to comment Share on other sites More sharing options...
amarques Posted March 7, 2008 Author Share Posted March 7, 2008 If the mysql server allows external/remote access you could do this directly by just putting in the proper host in the mysql_connect() function. If not, there are several existing "remote mysql helper" scripts that include security. Here is a link to one such script - http://www.phpclasses.org/browse/package/4000.html It allows but it won't be possible to use it in this case since it only allows after IP validation I'll check the script you suggested. Thanks a lot. I'll probably be back asking more question on this. Meanwhile, if anyone has any other suggestion, I'll be thankful. Quote Link to comment Share on other sites More sharing options...
amarques Posted March 7, 2008 Author Share Posted March 7, 2008 Would it make more sense to have the information stored in several files on the main site and, depending on the needs, having the info fread and then fwrite on the requesting sites? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.