pass variables with includes

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

leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

pass variables with includes

Post by leewad »

I have 100's of html files with some code like

Code: Select all

<!--#include virtual="header.php" -->

<!--#include virtual="include.php?var=example" -->

<!--#include virtual="footer.php" -->
I`m wanting to pass the text 'example' to the footer.php file, i have tried sessions but cant get this to work any ideas to how i can pass the variable to footer.php without adding it to the actual footer code i.e

Code: Select all

<!--#include virtual="footer.php?var=example" -->
As i dont want to be changing all the files
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: pass variables with includes

Post by Christopher »

leewad wrote:I`m wanting to pass the text 'example' to the footer.php file, i have tried sessions but cant get this to work any ideas to how i can pass the variable to footer.php without adding it to the actual footer code i.e

Code: Select all

<!--#include virtual="footer.php?var=example" -->
As i dont want to be changing all the files
I think that syntax is for Server Side Includes. That is the web server merging that file into the output. To do a PHP include the syntax would be:

Code: Select all

<?php
$var = 'example';
include "footer.php";
?>
(#10850)
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: pass variables with includes

Post by leewad »

Yes in a php file but I'm wanting HTML
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: pass variables with includes

Post by Eric! »

You should think of the include files as being all part of one php script. That's all an include does is include more code.

if you do $var="example"; then the next file that gets included will also be able to access that variable. So for example:

main.php

Code: Select all

$var="example text";
include "footer.php";
footer.php

Code: Select all

if(isset($var)) echo "Your text passed to me is: ".$var."\n";
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: pass variables with includes

Post by Christopher »

leewad wrote:Yes in a php file but I'm wanting HTML
PHP files can mix PHP and HTML
(#10850)
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: pass variables with includes

Post by leewad »

I know this but if you read my post I'm passing the variable to include.php but also want to pass it footer.php without changing any part of HTML file so it has to be passed from include.php to footer.php
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: pass variables with includes

Post by Eric! »

Did you read my reply? Just define your variable before you include both "include.php" and "footer.php" and it can be used in both of those files. You will of course need to modify both "include.php" and "footer.php" to use your new variable. You can pass anything, text, HTML, numbers, links...whatever.

You can not pass data using a GET or POST method to include files, because they are accessed using the file system before they are sent to the client. So something like include "footer.php?var=example" is impossible to do.
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: pass variables with includes

Post by leewad »

The file in question is HTML file not php I cannot define a var?

I cannot change any part of that if I could I would add the var to the end off footer include as I have done with the include above it ie

[syntax]
<!--#include virtual="footer.php?var=example" -->
[/syntax]
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: pass variables with includes

Post by Eric! »

I'm having a hard time understanding your problem and what you want to do.

In the file that you are including you can insert php into the html.

set your $var="SOMETHING";
Call your included "html" file:

Code: Select all

<h1> HTML HERE</h1>
<p>This html is boring</p>
<?PHP
if(isset($var)) echo "<p>Value passed HTML: ".$var."</p>";
?>
<p>And more html</p>
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: pass variables with includes

Post by leewad »

you cannot add php code into a html file??

Right i`ll make it as easier as possible

I have a html file called file_a.html in this file the code is:

Code: Select all

<!--#include virtual="header.php" -->

<!--#include virtual="file_b.php?var=hello" -->

<!--#include virtual="footer.php" -->

as you can see from the code ( in the html file ) it is passing the var "hello" to an include called file_b.php

all is ok with this but without changing any of the file_a.html i want to also pass the var "hello" to the file footer.php
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: pass variables with includes

Post by Benjamin »

Who or what book taught you this method of programming?
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: pass variables with includes

Post by leewad »

The client only wanted html files hence the method of not using php files apart from the includes so this is what i have had to do.

If all pages were in php i would not have this problem, why what METHOD would you do it?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: pass variables with includes

Post by Eric! »

leewad wrote:why what METHOD would you do it?
PHP.

It was not clear to me that you're trying to do this all in HTML using HTML SSI calls (mostly because you posted this question in a PHP CODE forum). There is no way that I know to pass a value to footer.php without modifying the file that is including it without using PHP. HTML does not allow state variables so you must modify the include with the value you want to pass. Depending on the purpose of this variable, you might be better off writing some javascript to show/hide or manipulate the DOM of the footer to create whatever effect you're trying to do with your variable.

Code: Select all

<!--#include virtual="footer.php?var=hello" -->
If your problem is:
leewad wrote:I have 100's of html files with some code
that you don't want to modify by hand. Then you should learn some command line tools like unix's sed to search and replace the section of code you need to modify.
[text]sed -i "s/stuff_to_find_and_replace/stuff_to_replace_it_with/g" filename*.*[/text]
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: pass variables with includes

Post by McInfo »

Without refactoring the HTML, the only way to share data among the included PHP files is to store it in a temporary file or database. Then the problem becomes associating stored data with a particular request. Using some of the variables available in the $_SERVER array, you can uniquely identify a request. Useful keys are REQUEST_TIME, REMOTE_PORT, REMOTE_ADDR, and HTTP_USER_AGENT. These variables will be the same across all included files within a single request, but will be different for separate requests.

If you are willing to do some refactoring on the HTML, you can use the SSI #set command instead of writing the variable in the query string of the SSI include.

Code: Select all

<!--#set var="myvar" value="myvalue" -->
Then the variable will be available to every included PHP script as:

Code: Select all

$_SERVER['myvar']
leewad wrote:The client only wanted html files
Did the client want the files to not contain PHP, or did the client want ".html" to appear in the browser's address bar? If it's the latter, it is possible to tell Apache Server to parse PHP files having a .html extension by using the SetHandler or AddHandler directive.

Code: Select all

<FilesMatch "\.html$">
    SetHandler application/x-httpd-php
</FilesMatch>

Code: Select all

AddHandler application/x-httpd-php .html
In my opinion, the code should be refactored to turn the HTML into PHP. Currently, the included PHP files are isolated from each other; but more importantly, they are isolated from the user because they cannot receive data from the browser through $_POST or $_GET. (Maybe that's a security feature?)
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: pass variables with includes

Post by leewad »

Thanks but cannot rewrite the URL due to having 100's of different files, they already had the HTML files

I have done it now anyway the only way I could think of and that is using database,
With browsers IP address so I you were looking at a certain page it would store the var under your ip then the footer would pull the data using your ip, long way round I know but how the site was already created its only the way I could do it
Post Reply