Click to See Complete Forum and Search --> : require() based on query string values


the_idiot
07-19-2003, 12:43 PM
I would like to set a page that prints a header based on the client's screen resolution. I am attempting to do this with some JavaScript on the index.htm page that retrieves the resolution information and passes it, via a query string, to a php page that uses selection to require() one of two headers depending on the query string variable's value.

*****Script to get resolution*****


<script language="JavaScript" type="text/javascript">

function choose_header() {
if (window.screen.width==800 && window.screen.height==600) {
window.location.href="/mc/dev/hometest.php?sr=1"
} else {
window.location.href="/mc/dev/hometest.php?sr=2"
}
}

</script>

*****************************

******Script to print header******

<?php
function print_header($res) {
if ($res == 1) {
require('header_800.inc');
} elseif (res == 2) {
require('header_1024.inc');
} else {
echo '</head>'."/n".
'<body>'."/n".
"<h1>res: $res</h1>";
}

$reso = $_GET['sr'];

if (isset($reso)) {
print_header($reso);
} else {
echo '</head><body><h1>$reso is not set.</h1>';
}
?>

*****************************

The problem is that the server does not render the php script at all. I set up a simple php page with a single require() statement which printed one of the aforementioned headers:

<?php
require('header_1024.inc');
?>

This worked just fine.

I also wrote a script to try and test values of the query string variable:

<?php

$reso = $_GET['sr'];

if (empty($reso)) {
echo '<h1>$reso is empty.</h1>';
} elseif (isset($reso)) {
echo '<h1>$reso is'.$reso.'</h1>'
}
?>

This script was not rendered either.

Examples can be seen using the following links:
hometest.php (http://www.solas.cc/mc/dev/) (redirect via index.htm: query string value passed)
hometest.php (http://www.solas.cc/mc/dev/hometest.php) (direct link: no query string value passed)
myphp.php (http://www.solas.cc/mc/dev/myphp.php) (test page for include file and php engine)
foo.php (http://www.solas.cc/mc/dev/foo.php) (script to test query string values: no values passed)

Questions........

Have I passed the value from the JavaScript correctly?

Have I retrieved the variable correctly in the php scripts?

Why aren't hometest.php and foo.php rendered while myphp.php is?

Any help is greatly appreciated.

pyro
07-19-2003, 09:36 PM
You're javascript looks ok... For your PHP, try this:

if (isset($_GET["sr"])) {
$sr = $_GET["sr"];
if ($sr == 1) {
require('header_800.inc');
}
else if ($sr == 2) {
require('header_1024.inc');
}
else {
echo "header not set";
}
}

the_idiot
07-20-2003, 09:21 AM
Thanks, pyro: the new php did the trick. I still don't understand why the old code was failing; I'd like to know so that I don't make the same mistake again. Was it possibly that I used single quotes rather than double on the $_GET index?

Thanks again for the fix.

pyro
07-20-2003, 09:35 AM
This part is going to be a problem:

} elseif (res == 2) {

should be:

} elseif ($res == 2) {

the_idiot
07-20-2003, 11:26 AM
I can't believe I missed that. I must have checked over that tiny script 100 times.


Thanks again.

pyro
07-20-2003, 12:47 PM
You're welcome... :)