PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
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.
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.
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
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.
<?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.
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.