1

Hi i want to include an external .php file inside my php file.

this .php file has some variable declaration needed inside my page.

i tried using file_get_contents but it just echoes the lines inside.

tried:

function getter($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

echo getter('www.somehting.com/phpfile.php');

-still not working

also tried:

$code = file_get_contents($url);
eval($code);

is there anyway for me to do this without changing the config.ini?

I have access to .htaccess on the page where the file should be included.

the file is on a different server "rackspace".

Thank You.

magicianiam
  • 1,474
  • 7
  • 33
  • 70
  • @rid what's that supposed to do? – magicianiam Nov 12 '12 at 17:57
  • Does this file belong to you? – Salman A Nov 12 '12 at 18:05
  • www.somehting.com/phpfile.php will get executed before it is returned to your script, either rename phpfile.php to phpfile.txt, or go to the apache conf or .htaccess of www.somehting.com and disable the php execution of phpfile.php, but I'm guessing you do not have access to do these things. Basically, you need it provided to you as the original file's text before it can be eval'ed locally. – gingerCodeNinja Nov 12 '12 at 18:08
  • @SalmanA yes, the file belongs to me, its just that it is needed to be on a different server. – magicianiam Nov 12 '12 at 19:19
  • @Chris i have access to .htaccess. – magicianiam Nov 12 '12 at 19:21
  • @magicianIam if you have access to the .htaccess file, then you probably have access to the php file itself, so just get it in the same way, ie if you're ftp to get to .htaccess, ftp to get to the php file; Alternatively if you still want to remotely load it over http, and you have access to it, then add/create a .htaccess in the directory where the php file is and add a directive to make all php files in that directory return as text like `AddType text/plain .php`, then you can remotely load it and without executing locally; although I think you may be misunderstanding what you're asking for. – gingerCodeNinja Nov 14 '12 at 17:34
  • if you are just wanting to share code between two web servers, even if they are with different providers, there are many other ways, such as using a version control system; if it is data/config that you want to load, and it is always changing, put it in a database somewhere, maybe a third system, and access it from both servers – gingerCodeNinja Nov 14 '12 at 17:36
  • @Chris do you have any documentation on doing this on htaccess? basically what i really want is that i can get some functions in my other php file to work on my other php file. thank you :) – magicianiam Nov 14 '12 at 17:39
  • why don't you just copy the php file the other server and use it there locally? why do you need to grab such a fresh copy of it on every request? does the file change _that_ often? documentation, see Apache Web server Documentation (assuming you are using Apache), and/or http://stackoverflow.com/a/4237000/830171 – gingerCodeNinja Nov 14 '12 at 17:46
  • @Chris thank you, i could do that but this file is used by multiple sites. – magicianiam Nov 14 '12 at 17:49
  • Sharing this way, over http, is by FAR not the best way to share code, for MANY reasons, not limited to speed, security, data integrity. Have you ever used version control software? like CVS, SVN, Git? if not you should look in to them - you can easily deploy code to multiple sites. If not wanting to use one central place for code and deploy it to each, you should put all these sites on the one webhost, then you can access the same file directly - some shared web hosts will let you do this too, just ensure the file permissions are what you want. – gingerCodeNinja Nov 14 '12 at 18:03

2 Answers2

1

You cannot include an external php file, unless the .php file is outputing the php source. Otherwise you will just get the interpreted php file from the external source.

The only thing you can do is output the .phps (source) so that you can access the actually code. Like I said above, you would be accessing the processed / interpreted php file, which would do you no good.

If you have access to the other server, you can try outputing the file.phps instead of file.php

Bot
  • 11,868
  • 11
  • 75
  • 131
0

If you load a file on an external server by web address, Apache will show the page after the php has been finsished.

So it will return

Hello world!

Instead of

<?php
echo 'Hello world!';
?>

You could, however, try to connect to the external server with ftp connection and retreive the file you want. This will require you to have administrator access to the server its hosting.

Gijs P
  • 1,325
  • 12
  • 28