# Question: Passing PHP variables via "include()"



## knighthawk (Mar 14, 2004)

I am designing a website hosted on GoDaddy.com servers.  They are using the "safe_mode" version of PHP 4.3.1 running on Linux/Apache.

The problem is that according to the documentation at www.php.net, I should be able to pass a variable using the "include()".  Here is an example of what I am trying to do.

*Calling Script:* test.php

```
<?php
    // ...
include( 'metadata.php?mode=long');
    // ...
?>
```

*Included Script:* metadata.php

```
<?php
$mode = $_GET['mode'];
if( $mode == "long" )
{
    // large list of meta "keywords" goes here
}
else
{
    // smaller list of meta keywords (to save on bandwidth)
}
?>
```

I have read the appropriate sections in the php.net documentation, and everything appears like it would be working correctly... however, the results of the include() are like the include() request was completely *ignored*.  *Nothing parses in the metadata.php file!*

Unfortunetly, I do not have access to another PHP server (and don't have time to set it up on my local machine), so I do not know if this is because of the restrictions that GoDaddy imposes, or if I just have the syntax wrong.

Any suggestions?


----------



## Pengu (Mar 14, 2004)

try putting a simple echo() statement in the file to be included. Are you sure you can pass arguments when using an include? try a require().


----------



## knighthawk (Mar 14, 2004)

Pengu said:
			
		

> try putting a simple echo() statement in the file to be included. Are you sure you can pass arguments when using an include? try a require().



Yes, this is the first thing I did.  I put a simple 'echo("working!!!");' statement, but I left out all of the verbage for the code sample.  The point is that it will run inside the 'metadata.php' as long as I do *not* try to pass a variable.  Then the server outputs a message saying that "mode" variable did not exist.

Yes, you can pass arguments using an include.  Example 11-5 "include() through HTTP" of the PHP docs are what I used as an example.  Below is the link.

php.net/manual/en/function.include.php

What I don't know is if I am doing it wrong (I don't think so) or if the GoDaddy servers have disabled this functionality.  I don't find their tech support and online help very useful in answering my questions.

The PHP docs do say that "include() through HTTP" will not work on Windows servers prior to PHP 4.3.0.  However, the GoDaddy server I am using is Linux and using PHP 4.3.1.

Thanks for your suggestion about the "require()".  I will try that.


----------



## Pengu (Mar 14, 2004)

maybe you need to do this:


```
include("http://www.yoursite.com/path/to/file.php?var=val");
```


----------



## twister (Mar 14, 2004)

I use includes with variables on my site all the time.  Try 
a) a full url  
b) use a different variable than $mode = $_GET['mode'];  like $mode2 = $_GET['mode']; 
c) if ( $_GET['mode'] == "long" );  instead of setting $mode.

Also, if you cant figure it out, can we see the code in action?  

Thomas


----------



## azrad (Mar 15, 2004)

yeah... i agree with twister,
u should have use $_GET['mode'] or $HTTP_GET_VARS['mode']

$mode usually true for PHP <= 4.0.6
this is due to Global Variables settings for PHP >= 4.1.0...


----------



## twister (Mar 15, 2004)

After thinking about it, i don't think you can pass variables though an include. This is because when metadata.php looks for your variable 'mode' it's looking up into the URL, which doesn't have any variables because that is not where you put them. You put them into an invisible URL, so it can't see them. Does that make sense?  I think you can only use URL variables if you can physically see them in the address bar.


----------



## azrad (Mar 15, 2004)

actually, u can pass variables through an include... provided that the variables is displayed on the URL string... for an example i want to include metadata.php and at the same time i want it to view page no. 3 on the metadata.php, i would request the following string...



> http://www.yoursite.com/include.php?file=metadata.php&page=3



this would include the file metadata.php in the include.php file and view the 3rd page of the metadata.php

for the include.php, you might call the var $_GET['file'] and as for the metadata.php, you might call the var $_GET['page']

hope this will help... and i answered ur question...


----------



## evildan (Mar 16, 2004)

Or you can just assign the variable values like this 


```
$my_var = "this is my Value";
include("my/include/file.php");
```

The variables will be passed to whatever file you include. So you can apply logic to them on the included page.

Unless I'm missing something?


----------



## twister (Mar 16, 2004)

I agree that

http://www.yoursite.com/include.php?file=metadata.php&page=3 

would work.  As long as the variables are not in the actual PHP include statement.


----------

