2

I have a very basic PHP file on a separate server (server 1) that literally contains the following:

<?php $version = "1.0.0"; ?>

In my PHP file on server 2, what I would like to do it simply echo this variable. I have tried using get_file_contents() but I think this is for files on the same server. So I also tried using something similar but also using fopen() and this resulted in Resource id #93.

Looking around on Google, I have found so many different examples, but I basically want to have a short and simple one. Can anyone point me in the right direction?

1
  • not going to happen without security breach. Use GET or Session with generate_id. Commented Mar 7, 2013 at 0:06

3 Answers 3

3

Server 1:

<?php
$version = "1.0.0";
echo $version;

Server 2:

<?php
$version = file_get_contents('http://server1.com/script.php');

(thats assuming server 1 is web accessible from server 2)


Edit: Following on from a comment, should you wish to access more than one variable, you could use this:

Server 1:

<?php
$version = "1.0.0";
$name = 'this is my name';
echo json_encode(compact('version','name'));

Server 2:

<?php
$data= json_decode(file_get_contents('http://server1.com/script.php'),true);
echo 'my version is:'.$data['version'];

That should be fairly self explanatory - any questions via comments..

Sign up to request clarification or add additional context in comments.

3 Comments

Only works if that file is only outputting one static variable and nothing else.
Indeed - but that is what the original poster asked.. If he wanted multiple variables, I would have suggested using serialize or json encoding
thank you, exactly what I was looking for. Did basically the same before but just forgot to use echo $version after I had defined it.
1

file_get_contents only works to read the output of a file, and the example that you are showing doesn't have any kind of output. If you really want to do this you should echo some kind of data so that your can be able to fetch it with get_file_contents.

Another aproach is to include() it, but this is a big security flaw and php.ini disallows this option by default.

Mabe this works for you, instead of the code above you should do something like

<?php echo "1.0.0"; ?>

Then somewhere else you could do this:

<?php
    $path = "http://www.somepage.com/version.php";
    $version = file_get_contents(urlencode($path)); 
?>

But be aware that you need to modify your php.ini if you want file_get_contents to work on remote files. You will need to modify the directive allow_url_fopen to true.

Comments

0

Is the PHP file on the remote server hosted on a webserver like apache?How is it hosted? You can try using curl

$url = "http://<ip_or_domain_of_remote_server>/your_file.php";
$ch = curl_init();  
curl_setopt($ch,CURLOPT_URL, $url);
$result = curl_exec($ch);
echo $result

You should also ensure that your ports are open on the remote server and your file has the appropriate permissions

also, with curl, you can pass variables to the remote php script like this:

 $url = "http://<ip_or_domain_of_remote_server>/your_file.php";
 $data = array("key0"=>"val0","key1"=>"val1");
 $ch = curl_init(); 
 curl_setopt($ch,CURLOPT_URL, $url);
 curl_setopt($ch,CURLOPT_POST, count($data));       
 curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
 $result = curl_exec($ch);
 echo $result

4 Comments

I would rather stay clear from curl, but thanks for the answer
@Lodder well you should have mentioned that before down voting. its a valid answer given your question.
I didn't downvote. but yes, I should have mentioned that in my question
anyone can downvote tucker.. it was likely to be someone else viewing the question/answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.