tags:

views:

60

answers:

6

I'm not a PHP developer, but I'm currently hacking on an internal tool so my team can take advantage of its goodness. There's an index file that looks like so:

require( ($loader_path = "../../loaderapi/") . "loader.php" );

Used like this, $loader_path will retain its value within the loader.php file.

However, we want to access this API from our team's server like so:

require( ($loader_path = "http://remoteservername/loaderapi/") . "loader.php" );

In this case the $loader_path variable doesn't retain its value. I'm guessing it has something to do with it being a full blown URL, but I might be wrong. Any idea on how I can make this work, or why I can't do it this way?

A: 

Have you tried splitting it into two lines:

$loader_path = "http://remoteservername/loaderapi/";
require( $loader_path . "loader.php" );

It's easier to read this way as well.

Doug Hays
This may be cleaner to look at, but makes no difference in the behavior I'm experiencing.
Fostah
Following your question this resolves the problem stated... the $loader_path is retained
Nicky De Maeyer
Are you certain that `loader.php` does not contain code that alters $loader_path?
Doug Hays
Putting the echo right at the top of loader.
Fostah
+3  A: 

If your accessing a PHP script over HTTP, only the output of that script is returned. So your script will try to interpret the output of that remote PHP script and not its source.

If there is a connection over the file system, you may want to try file://remoteservername/loaderapi/loader.php instead.

Gumbo
I redact my earlier comment, you are exactly correct; in my haste, I misread the 'include' function. The salient point just a page down is: 'Remote file may be processed at the remote server (depending on the file extension and the fact if the remote server runs PHP or not) but it still has to produce a valid PHP script because it will be processed at the local server ... special care should be taken to secure the remote script to produce a valid and desired code.'
Matt Poush
This won't work for my needs as there is no file system connection.
Fostah
A: 

Simplify the code reading by simply putting everything on 3 lines:

$loader_path = "http://remoteservername/loaderapi/";
$page = "loader.php";
require($loader_path . $page );

Much clearer and it works.

Daok
This may be cleaner to look at, but makes no difference in the behavior I'm experiencing.
Fostah
Downvoting a bunch of question won't help you to have the perfect answer. Your title of your answer is not good. You should ask about remote problem instead of maintenance.
Daok
A: 

why not just put it above the require statement? would make it easier to read too.

<?php
  $loader_path = "../../folderName/"
  require($loader_path . "filename")
 ?>
GSto
The relative path was not the problem.
Fostah
A: 

There shouldn't be any real difference between the two; what you're doing is defining $loader_path, concatenating the loader.php, and passing that to require.

HOWEVER: you're defining the variable within the scope of a require, which will halt processing of the script of require fails.

Try replacing 'require' with 'include' and see if it retains the variable.

Also, note that if you are running your PHP server on a windows machine, and the php version is less than 4.3.0, neither 'require' nor 'include' can handle remote files : http://us.php.net/manual/en/function.include.php

Also, as noted before, if the .php lives on a remote server that parses php, you will not get code, but the result of the remote server processing the code. You'll either have to serve it up as a .txt file, or write php that, when processed, outputs valid php.

Matt Poush
I don't know the cut off version, but **PHP by default disables remote file inclusion**.
dcousineau
Changing to include didn't help. And, I'm running PHP 5.2.10 with Apache 2.2.
Fostah
@dcousineau I have allow_url_fopen and allow_url_include both enabled in my php.ini file. And I don't have a problem including the file as I can echo the value of the $loader_path variable from the loader.php file no matter what I set $loader_path to. It just doesn't retain its value when its a URL.
Fostah
A: 

NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO!

Remote file inclusion is a BAD idea, probably one of the biggest security flaws you can open up. Even for an internal tool this is not acceptable even if only purely for contributing bad habits.

PHP by default disables this behavior, and there is a broad push to have the ability to perform an include on a URL completely stripped from PHP (as there is no compelling reason to have this ability).

If you want to load shared resources, go through a shared file system drive (as in, don't use http, ftp, anything but file://) or better yet distribute copies of loader.php through a version control system. Loading from a single file resource opens you up to problems in the future of say a new dev overwriting loader.php and breaking everyone else's code.

dcousineau
I don't agree and I'm not asking if it's good practice or not. Maybe it is a security flaw, but it's not an issue on a private server and it makes no sense to mass blast a bunch of teams to sync every time the master API gets tweaked.
Fostah
Then shared drive is your best solution. I would HIGHLY suggest trusting the wisdom of the core PHP team, the community at large, and the entire software security industry and find a real, long term stable solution rather than a horribly dirty hack. But it's your funeral.
dcousineau
Still, your off topic. It's like me taking my car to the mechanic and him telling me my problem is that I'm not driving an American car. Give me a solution that fits in my scope or don't bother responding.
Fostah
Your analogy is bad. You're asking me to put rocket fuel in your Ford Taurus, and I'm telling you that while it might be possible, you want either an actual jet or plain old E87 gasoline. Industry standard practice for what you are trying to achieve is a centralized version control repository so your devs can have their own checked out copies of your `loader.php` file. Look into bit bucket and spend your time automating updates rather than going down ugly hack road. My route will at least give you a tool for other future logistics.
dcousineau
If you don't want to listen to me, get on IRC: freenode.net #phpc. The PHP core maintainers, as well as pillars of the PHP community all congregate there and will all tell you the same thing, most from personal experience going down the same road of "oh it's just one quick hack and it's a private tool"
dcousineau
Again, the question is why this variable is not being retained, not how should I implement this. If you don't have an answer to my question, please stop wasting my time.
Fostah