Forum Moderators: coopster

Message Too Old, No Replies

PHP include variables in the middle of a string

         

Murdoch

2:44 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



I'm finding it hard to assign a variable at the beginning of a file and then throw the variable in the middle of an include string. For example:

&var = "filename";

<?php include "http://www.mysite.com/".$filename;.".php";?>

I've also tried it without the semi-colon after the filename.

What am I doing wrong here?

Thanks

joelgreen

2:56 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Why not like this? Not sure i understood your requirements correctly

$filename = "filename here";

<?php include "http://www.mysite.com/{$filename}.php";?>

whoisgregg

3:05 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The semicolon is what's throwing it off. :)

<?php include("http://www.mysite.com/".$filename.".php");?>

joelgreen

3:11 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Note:
I believe it would include parsed php file. So do not expect to include php code when including urls.

<?php include "http://www.mysite.com/".$filename.".php";?>

if you want php code included, then include via local path (i.e. server path)

<?php include "/home/users/mysite.com/".$filename.".php";?>

Murdoch

3:46 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Doh I just realized none of the variables were being passed so I have to figure out what I'm doing wrong there first I suppose. Thanks for the help though!

Murdoch

5:20 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Okay so here seems to be the REAL problem.

Your suggestions are working fine with the syntax on the include.

The problem seems to be that none of the variables are even passing through to the header include after they've been assigned in the index file. This is strange since in the files in the root directory this seems to work without a hitch.

The files I'm working with here are 2 directories down from the main folder. The folders all have index files and I'm trying to assign the variables at the beginning of the index file and then call the header file, where all the real code begins. The php in THAT file seems to work fine except it won't recognize any of the variables from the parent file.

As I said before, this setup seems to work fine for files in the root directory, but not here. Any ideas?

joelgreen

5:47 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Here index.php will NOT see $var, as it is including via web server
$var = 123;
<?php include "http://www.mysite.com/index.php";?>

Here index.php WILL see $var, as it is including via php parser
$var = 123;
<?php include "/home/mysite.com/index.php";?>

If you want to include with "http://" (like in first example) then try passing vars like this:
<?php include "http://www.mysite.com/index.php?var=123";?>

Murdoch

5:55 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Joel you rule.

Where can I send you a box of cookies?

joelgreen

6:53 am on Oct 4, 2007 (gmt 0)

10+ Year Member



Murdoch, glad it worked :)