Page 1 of 1
PHP Include variable problem
Posted: Sun Nov 15, 2009 5:09 pm
by darkmatter
Hi,
I have a problem using php variables with an include statement and hope someone can point out what I am doing wrong.
If I use the following code then it works fine.
Code: Select all
<?php
Echo"If you see this then you have loaded the include code<br>";
$A = "Hello";
?>
Code: Select all
<?php
include("Secretcode.php");
echo$A;
?>
Code: Select all
If you see this then you have loaded the include code
Hello
But if I change the url to a fully qualified domain name (I want to use the same code on multiple websites from 1 source) then I get the echo statment but the variable $A has no value.
Code: Select all
<?php
Echo"If you see this then you have loaded the include code<br>";
$A = "Hello";
?>
Code: Select all
<?php
include("http://www.__________.com/Secretcode.php");
echo$A;
?>
Code: Select all
If you see this then you have loaded the include code
I did some searching on this forum before posting and a couple of hours searching on google but I cannot find a solution.
TIA Karl
Re: PHP Include variable problem
Posted: Sun Nov 15, 2009 8:42 pm
by requinix
That's the wrong solution to your problem. Find another way to do it.
Are these websites on the same server? Is there a problem with maintaining a couple copies of the same code?
Re: PHP Include variable problem
Posted: Tue Nov 17, 2009 1:43 pm
by darkmatter
Thanks for the reply.
In answer to your questions, all sites will be located on the same server but the problem is that there could be 100+ sites all accessing the same code. That would make maintaing the same code on each site impossible especially as the code may change regularly.
Some research I have done suggests that what I want to do would work if the include statement referenced the file from it's location on the server rather than by domain.com/include.php.
Problem is that I cannot afford a dedicated server just to then find out that what I want to do wont work.
Any pointers to other solutions to this problem would be greatly appreciated.
Regards Karl
Re: PHP Include variable problem
Posted: Tue Nov 17, 2009 2:52 pm
by requinix
If all the sites are on the same server then they can all include the same file, as in,
Code: Select all
include "/home/account/includes/file.php";
Re: PHP Include variable problem
Posted: Tue Nov 17, 2009 2:55 pm
by iankent
tasairis wrote:If all the sites are on the same server then they can all include the same file, as in,
Code: Select all
include "/home/account/includes/file.php";
however, beware of any basedir restrictions on the server which may stop sites including files from outside their home directory. in that case you'd either need to adjust the basedir restrictions or put the file to include in /usr/local or similar
hth
Re: PHP Include variable problem
Posted: Tue Nov 17, 2009 8:16 pm
by Weiry
I just did a little searching and i found the answer to your problem.
If a file is included using a URL and it has a '.php' extension - the file is parsed by php - not just included as it would be if it were linked to a local file.
For example:
Code: Select all
<?php
include "http://example.com/MyInclude.php";
?>
This would not give you access to any variables, classes or functions in the included file.
Instead you need to rename your included file to have an extension like "MyInclude.bla". This way the php interpreter will not 'get in the way' and the text will be included normally giving you access to your php variables etc.
Re: PHP Include variable problem
Posted: Tue Nov 17, 2009 10:28 pm
by requinix
Weiry wrote:Instead you need to rename your included file to have an extension like "MyInclude.bla". This way the php interpreter will not 'get in the way' and the text will be included normally giving you access to your php variables etc.
And anybody who knows about the file can see the PHP code too.
No offense, Weiry, but that's a bad solution.
Re: PHP Include variable problem
Posted: Tue Nov 17, 2009 10:39 pm
by Weiry
tasairis wrote:No offense, Weiry, but that's a bad solution.
This is not my solution, as i said i only searched and found an answer. But it also seems to be one of the ONLY solutions.
include() Post
tasairis wrote:And anybody who knows about the file can see the PHP code too
Then simply protect the file using .htaccess or any other sort of protection you want.