PHP Include variable problem

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!

Moderator: General Moderators

Post Reply
darkmatter
Forum Newbie
Posts: 2
Joined: Sun Nov 15, 2009 4:43 pm

PHP Include variable problem

Post 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
Last edited by pickle on Tue Nov 17, 2009 4:47 pm, edited 1 time in total.
Reason: Fixed the code tags. No space before the closing "]"
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Include variable problem

Post 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?
darkmatter
Forum Newbie
Posts: 2
Joined: Sun Nov 15, 2009 4:43 pm

Re: PHP Include variable problem

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Include variable problem

Post 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";
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: PHP Include variable problem

Post 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
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: PHP Include variable problem

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Include variable problem

Post 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.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: PHP Include variable problem

Post 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.
Post Reply