Help - Search - Members - Calendar
Full Version: Passing URL parameters to #include php
HTMLHelp Forums > Programming > Server-side Scripting
Paul Webster
I am trying to minimse changes to a pre-existing PHP script and to the format of an existing HTML-based web site ... and have hit a stumbling block.

The script (a guestbook) can be called with parameters (e.g. page number).
I am currently experimenting with calling the php script via #include in a hosted Apache server (on Unix).
If I call the .php directly and pass in my parameters
e.g. http://somewhere/mysample.php?a=1&b=2
then it works fine
if I called the surrounding .html
e.g. http://somewhere/htmlfile.html?a=1&b=2
then the parameters (a and b) are not available to the .php - presumably because the #include invokes the .php directly.

I thought that QUERY_STRING_UNESCAPED might be a way forward - but it doesn't seem to be set.

Is there a way to get Apache or some in-line PHP to take the URL parameters that were passed to the .html and then pass them on to the #include of the .php ?
Brian Chandler
QUOTE(Paul Webster @ Jan 24 2007, 05:06 PM) *

I am trying to minimse changes to a pre-existing PHP script and to the format of an existing HTML-based web site ... and have hit a stumbling block.

The script (a guestbook) can be called with parameters (e.g. page number).
I am currently experimenting with calling the php script via #include in a hosted Apache server (on Unix).
If I call the .php directly and pass in my parameters
e.g. http://somewhere/mysample.php?a=1&b=2
then it works fine
if I called the surrounding .html
e.g. http://somewhere/htmlfile.html?a=1&b=2
then the parameters (a and b) are not available to the .php - presumably because the #include invokes the .php directly.

I thought that QUERY_STRING_UNESCAPED might be a way forward - but it doesn't seem to be set.

Is there a way to get Apache or some in-line PHP to take the URL parameters that were passed to the .html and then pass them on to the #include of the .php ?


Are you sure PHP is parsing the file? Have you made the apache settings so that .html files are parsed by php as well as .php files? How to find out: add the following somewhere...

<?php
echo "<p>Hello world!</p>";
?>

If this appears as "Hello world!" then you're ok; if you just see the php code in the browser, then this is the problem. HTH - please investigate then ask again.




Paul Webster
I will resaerch that later this evening - but ... I don't think that this is the issue that I am hitting at the moment because I do not have any PHP embedded inside the .html

the .html simply has (at the moment) a #include of the PHP file.
This is working fine - because the PHP is running and outputting stuff inside the pre-existing (calling) page.

Are you suggesting that if I had Apache putting all of the .html pages through PHP processor then it would cascade the received URL parameters down to the PHP that I #include ?

I guess I should make a simple demo page to show the problem better.
I'll have a go at that tonight - unless someone finds the magic bullet before then.
Brian Chandler
QUOTE(Paul Webster @ Jan 25 2007, 12:37 AM) *

I will resaerch that later this evening - but ... I don't think that this is the issue that I am hitting at the moment because I do not have any PHP embedded inside the .html

the .html simply has (at the moment) a #include of the PHP file.
This is working fine - because the PHP is running and outputting stuff inside the pre-existing (calling) page.


What is #include? Are you using Apache SSI and php together? (My impression is that this is a recipe for problems.)

Anyway, the only way a php script can run is by being included (directly or whatever) in the .html source file, and the source file being passed through the php interpreter. If you try to combine SSI includes with php you need to understand (I don't) exactly how the file will be processed, to make sure the SSI include is done before the file goes through the php interpreter.

Do you _really_ mean the _including_ ("calling") page has php in it, like this:

(Myfile.html)
<html><><><>Bla blah blagh...

<?php
// neat php script producing database results
...
?>

< #include fred.php : === whatever the syntax for SSI ... >

...
</html>

And are you saying the first directly written php script works, but the contents of fred.php appears without having been processed? Is that it?

In this case the simplest thing is to avoid (and probably switch off) the SSI, and just include the php script with
<?php
include '../lib/myscriptfred.php'; // check syntax!
?>


QUOTE

Are you suggesting that if I had Apache putting all of the .html pages through PHP processor then it would cascade the received URL parameters down to the PHP that I #include ?


I'm suggesting that unless the file is passed through the php interpreter, the php interpreter won't run it.
Paul Webster
The top level HTML is something like this ... demo.html
CODE
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>
Hello world - PHP include next<BR>
<!--#include virtual="demo.php" -->
<BR>that was the PHP include output
<BR>Now include php with a fixed parameter ...<BR>
<!--#include virtual="demo.php?a=fixed" -->
</body>
</html>



The PHP is something like this ... demo.php
CODE
<?php
$a=htmlspecialchars($_REQUEST['a']);
echo "value of parameter a=".$a;
?>



then - if I use a browser and access http://somewhere/demo.php?a=alpha
we should see
value of parameter a=alpha
but if I go to http://somewhere/demo.htm?a=alpha
I will see
value of parameter a=
because the parameter is not passed down.

I know that this is unusual request - but if there was a readily available solution then it would make life much easier.

I thought that it might be possible with some Apache specific (non-PHP) scripting around the #include might be able to do it but I haven't found a way yet.

(I include the HTML in 2 chunks within the PHP file - but then the person that maintains that HTML code (not me) in is going to moan.)
Christian J
QUOTE(Paul Webster @ Jan 24 2007, 06:02 PM) *

CODE

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

The above is SSI including a file called "demo.php". Apparently using SSI stops the PHP code in the included file from being parsed. Try this instead:

CODE

<?php include "demo.php"; ?>


QUOTE
I thought that it might be possible with some Apache specific (non-PHP) scripting around the #include might be able to do it but I haven't found a way yet.

Why use SSI in the first place rather than PHP include()?
Brian Chandler
QUOTE(Paul Webster @ Jan 25 2007, 02:02 AM) *

The top level HTML is something like this ... demo.html
CODE
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>
Hello world - PHP include next<BR>
<!--#include virtual="demo.php" -->
<BR>that was the PHP include output
<BR>Now include php with a fixed parameter ...<BR>
<!--#include virtual="demo.php?a=fixed" -->
</body>
</html>



The PHP is something like this ... demo.php
CODE
<?php
$a=htmlspecialchars($_REQUEST['a']);
echo "value of parameter a=".$a;
?>



Well, this:

<!--#include virtual="demo.php?a=fixed" -->

isn't going to "work". Apache includes include *files*; the arguments on the end are something interpreted by a Web server. (Yes, I know Apache is a server, but when it gets a file to include it can only get the file.)

Suppose you have a large script you don't want to fiddle with, then you still need to check how the script is accessing the parameters (probably by accessing $_GET['a'] ). Then you just set this manually (yeugh: this isn't very robust style...)

<?php
$_GET['a'] = 'fixed';
include 'demo.php';
?>

Something like that...

As Christian said - first get rid of the Apache SSI includes, and just use php.
Paul Webster
QUOTE(Christian J @ Jan 24 2007, 05:29 PM) *

QUOTE(Paul Webster @ Jan 24 2007, 06:02 PM) *

CODE

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

The above is SSI including a file called "demo.php". Apparently using SSI stops the PHP code in the included file from being parsed.

It does not stop the demo.php from being run through PHP interpreter. The echo command in the called .php works fine - but it doesn't see the ?a=alpha part.

QUOTE

Try this instead:

CODE

<?php include "demo.php"; ?>

I need FTP access to the box to tell Apache to pass all .html/.htm through PHP interpreter for that .... and I can't do that right now .... so this test has to wait until later.

QUOTE

QUOTE
I thought that it might be possible with some Apache specific (non-PHP) scripting around the #include might be able to do it but I haven't found a way yet.

Why use SSI in the first place rather than PHP include()?

I did it initially because it meant that I didn't have to change .htaccess file again to get every .html/.htm parsed by PHP and because I thought that I would easily be able to get the URL parameters ... unfortunately that turns out not to be the case.
Paul Webster
QUOTE
Well, this:

<!--#include virtual="demo.php?a=fixed" -->

isn't going to "work". Apache includes include *files*; the arguments on the end are something interpreted by a Web server. (Yes, I know Apache is a server, but when it gets a file to include it can only get the file.)

Please don't think that I am being contrary for the sake of it - but it does work.
When I found that it was posible to pass parameters via Apache SSI like this then I thought that my hack approach was going to be feasible.
I thought that I could simply stuff some Apache SSI scripting along the lines of (this is 100% pseudo code)
var urlparams=$realurlparams;
If urlparams="" then incfile="demo.php" else incfile="demo.php?"+urlparams
<!--#include virtual=incfile -->

clearly the above is not real code - but I had hopes that something like this could work.
So looks like I will have to embed the php into the calling .html and set Apache to push all .html through PHP.
Still needs to pass the original URL parameters though but I presume that it will be easy enough.
Paul Webster
I've just put those 2 pages on a site just to show what happens
replace the "somewhere" from post#5 above with www dot dandelionradio dot com
As you will see the #include with the ?a=fixed really does pass the parameter to the PHP script.

Meanwhile I am having a go at gettinghte php inside the .html method to work.
Paul Webster
Well - I couldn't get the PHP code inside the .htm file to be actioned. So perhaps I have hit the issue on my hoster of not being able to mix SSI and embedded PHP ...
I can't turn of the SSI as a whole because it will mess up other stuff that is already there ... so I had to dig harder.

I found that _SERVER["REDIRECT_QUERY_STRING"] does contain the query string that the .htm was called with
i.e. the php that I call via the SSI #include can see the original query.
So I am now writing a quick hack to parse that and I think I'll be OK.
I updated the demo.htm on my site to show it in action.
Brian Chandler
Me:
<!--#include virtual="demo.php?a=fixed" -->

isn't going to "work". Apache includes include *files*; the arguments on the end are something interpreted by a Web server. (Yes, I know Apache is a server, but when it gets a file to include it can only get the file.)

PW:
Please don't think that I am being contrary for the sake of it - but it does work.
When I found that it was posible to pass parameters via Apache SSI like this then I thought that my hack approach was going to be feasible.

------
You've lost me, I'm afraid. I am only guessing at how the php interpreter gets a file from an SSI. You say it "works" and I would have to believe you, but then you seem to say that it doesn't work after all.

===Post #10 PW:

I've just put those 2 pages on a site just to show what happens
replace the "somewhere" from post#5 above with www dot dandelionradio dot com
As you will see the #include with the ?a=fixed really does pass the parameter to the PHP script.

I can't see much, really, because I can't see the script itself. But it looks as though (perhaps) an SSI puts the params to the "include" in the following variable:

_SERVER["REDIRECT_QUERY_STRING"]

But why should that help? Does the php script only/always look at this particular variable? How is the php script originally meant to be used? If as a standalone page, then it will probably look for its parameters in the $_GET[] array (which is what I suggested to you).

PW:
Meanwhile I am having a go at gettinghte php inside the .html method to work.

Again, I'm lost. What is the "php inside the .html" method?

===Post #11

PW:
Well - I couldn't get the PHP code inside the .htm file to be actioned.

This sounds like the orginal problem - that your html file is not being php parsed.

PW:
So perhaps I have hit the issue on my hoster of not being able to mix SSI and embedded PHP ...
I can't turn of the SSI as a whole because it will mess up other stuff that is already there ... so I had to dig harder.

You don't need (probably) to turn SSI off, but you don't need to use it, either. I suppose it's possible to just SSI #include a php script you write, that makes the appropriate includes of the guest book script, setting the parameters appropriately, but you need to understand enough about how the script works to do this.

PW:
I found that _SERVER["REDIRECT_QUERY_STRING"] does contain the query string that the .htm was called with
i.e. the php that I call via the SSI #include can see the original query.
So I am now writing a quick hack to parse that and I think I'll be OK.
I updated the demo.htm on my site to show it in action.[/quote]

The best test is to SSI #include a test.php that includes just

<?php
phpinfo();

Good luck anyway.
Paul Webster
QUOTE(Brian Chandler @ Jan 25 2007, 04:37 AM) *

Me:
<!--#include virtual="demo.php?a=fixed" -->

isn't going to "work". Apache includes include *files*; the arguments on the end are something interpreted by a Web server. (Yes, I know Apache is a server, but when it gets a file to include it can only get the file.)

PW:
Please don't think that I am being contrary for the sake of it - but it does work.
When I found that it was posible to pass parameters via Apache SSI like this then I thought that my hack approach was going to be feasible.

------
You've lost me, I'm afraid. I am only guessing at how the php interpreter gets a file from an SSI. You say it "works" and I would have to believe you, but then you seem to say that it doesn't work after all.

What I was referring to as working is that including a query string in the SSI #include does result in the query being passed to the invoked .php in the _GET/_REQUEST etc.
Hence my contrived example of ?a=fixed

What I was referring to as not working was my hoped for overall approach - i.e. have the query string from the outermost call (i.e. bit after the ? in http://somewhere/demo.htm?a=outermost ) making its way through transparently in the same way.

QUOTE

===Post #10 PW:

I've just put those 2 pages on a site just to show what happens
replace the "somewhere" from post#5 above with www dot dandelionradio dot com
As you will see the #include with the ?a=fixed really does pass the parameter to the PHP script.

I can't see much, really, because I can't see the script itself. But it looks as though (perhaps) an SSI puts the params to the "include" in the following variable:

_SERVER["REDIRECT_QUERY_STRING"]

But why should that help? Does the php script only/always look at this particular variable? How is the php script originally meant to be used? If as a standalone page, then it will probably look for its parameters in the $_GET[] array (which is what I suggested to you).

The demo.php and demo.htm that are on that site are essentially the same as the example that I first wrote in #5 above - but I later included some code in the .php file to dump out the environment variables and I spotted that _SERVER["REDIRECT_QUERY_STRING"] and _SERVER["REDIRECT_QUERY_STRING_UNESCAPED"] had the query string. IN fact, if you look at my very first post you will see that I was trying to use something like that having spotted references to QUERY_STRING_UNESCAPED ... but I hadn't realised at the time that the real name was slightly different (i.e. it ihas "REDIRECT_" on the front..

Regarding $_GET - as I mentioned above - the script uses $_REQUEST. $_REQUEST includes _get, _post and _cookies ... i.e. it is (sort of) using _GET.
So - given that _SERVER["REDIRECT_QUERY_STRING"] contains the original query string ... I have (in my latest version of the real script - but not in the demo.php) replaced all references to $_REQUEST with a function call. In this new function I look in $_REQUEST and if the required value is not there then I use parse_str on $_SERVER['REDIRECT_QUERY_STRING'] to see if it is in there.
This revised code is now working fine for parameters that come in via the query string.

QUOTE

PW:
Meanwhile I am having a go at gettinghte php inside the .html method to work.

Again, I'm lost. What is the "php inside the .html" method?

A file with a .html extension that contains php code in it somewhere.
Up until then
- my .html files contained only HTML plus the Apache SSI code.
- all of my PHP code was in files with .php extension

QUOTE

===Post #11

PW:
Well - I couldn't get the PHP code inside the .htm file to be actioned.

This sounds like the orginal problem - that your html file is not being php parsed.

You are correct in that the code was not being parsed - but this was not my original problem because I did not have any PHP code inside any .html code until I made the test referred to above.

QUOTE

PW:
So perhaps I have hit the issue on my hoster of not being able to mix SSI and embedded PHP ...
I can't turn of the SSI as a whole because it will mess up other stuff that is already there ... so I had to dig harder.

You don't need (probably) to turn SSI off, but you don't need to use it, either. I suppose it's possible to just SSI #include a php script you write, that makes the appropriate includes of the guest book script, setting the parameters appropriately, but you need to understand enough about how the script works to do this.

The SSI functionality as a whole is enabled by a setting in the .htaccess file.
The suggestion from other threads in this forum is that enabling it at all (i.e. in the .htaccess file) will sometimes prevent Apache from parsing PHP code inside .html files. I appear to have that problem.
At this stage I can't risk turning the facility off (by editing the .htaccess file) because other web pages on the site are successfully using SSI to invoke PHP (e.g. the Now Playing part on the main page). I could experiment on another site or perhaps in a lower level subdirectory - but that might have to wait for another day.

QUOTE
QUOTE
PW:
I found that _SERVER["REDIRECT_QUERY_STRING"] does contain the query string that the .htm was called with
i.e. the php that I call via the SSI #include can see the original query.
So I am now writing a quick hack to parse that and I think I'll be OK.
I updated the demo.htm on my site to show it in action.


The best test is to SSI #include a test.php that includes just

<?php
phpinfo();

It was doing something like that that let me finally find the _SERVER["REDIRECT_QUERY_STRING"] field.

QUOTE

Good luck anyway.

Thanks. I realise that all of this quoting makes it a bit difficult to work out what has been going on - but I thought that I should post back just in case anyone else ever hits something like this.
To whoever hits on this thread in the future ... I suggest reading slowly with a long cool drink wink.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.