Page 1 of 2

pass variables with includes

Posted: Tue Oct 09, 2012 6:23 am
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

Re: pass variables with includes

Posted: Tue Oct 09, 2012 2:23 pm
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";
?>

Re: pass variables with includes

Posted: Tue Oct 09, 2012 3:23 pm
by leewad
Yes in a php file but I'm wanting HTML

Re: pass variables with includes

Posted: Tue Oct 09, 2012 3:53 pm
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";

Re: pass variables with includes

Posted: Tue Oct 09, 2012 4:01 pm
by Christopher
leewad wrote:Yes in a php file but I'm wanting HTML
PHP files can mix PHP and HTML

Re: pass variables with includes

Posted: Tue Oct 09, 2012 4:25 pm
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

Re: pass variables with includes

Posted: Tue Oct 09, 2012 4:31 pm
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.

Re: pass variables with includes

Posted: Tue Oct 09, 2012 4:45 pm
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]

Re: pass variables with includes

Posted: Tue Oct 09, 2012 5:28 pm
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>

Re: pass variables with includes

Posted: Wed Oct 10, 2012 2:43 am
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

Re: pass variables with includes

Posted: Wed Oct 10, 2012 2:56 am
by Benjamin
Who or what book taught you this method of programming?

Re: pass variables with includes

Posted: Wed Oct 10, 2012 3:13 am
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?

Re: pass variables with includes

Posted: Wed Oct 10, 2012 10:40 am
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]

Re: pass variables with includes

Posted: Sat Oct 13, 2012 1:00 pm
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?)

Re: pass variables with includes

Posted: Sat Oct 13, 2012 1:19 pm
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