Forum Moderators: coopster

Message Too Old, No Replies

Use Javascript to delegate php includes

is it possible to only read certain php calls?

         

Philly

1:39 am on Apr 12, 2005 (gmt 0)

10+ Year Member



Hello. Does anyone know if it is possible to:

<script type="text/javascript">
<!--//--><![CDATA[//><!--
if (true)
<?php include ("file1.php");?>
else
<?php include ("file2.php");?>
//--><!]]>
</script>

without the server including both includes anyway?

Thank you for your time.

ironik

1:53 am on Apr 12, 2005 (gmt 0)

10+ Year Member



You'll have to do it in php:

<script type="text/javascript">
<!--//--><![CDATA[//><!--
<?php if (true) {
include ("file1.php");
} else {
include ("file2.php");
}?>
//--><!]]>
</script>

Philly

1:58 pm on Apr 12, 2005 (gmt 0)

10+ Year Member



Thanks for your help, but that doesn't seem to do it either. I tried it two ways.

One which includes the file either way, so that is not good:
<?php
echo '<!--[if IE]>';
include ("Borders/IEnoJavaMenu.php");
echo '<![endif]-->';
?>

And two which will not get the include either way, so that is not good:
<?php
echo '<!--[if IE]>'.include ("Borders/IEnoJavaMenu.php").'<![endif]-->';
?>

What is this "if IE" you say? It can be detailed here
[quirksmode.org...]
or here
[quirksmode.org...]
if you never saw it before.

Why do I use it you say? Because if JavaScript detects the browser that is, let's say, Opera that is set to disquise itself as IE, then the JavaScript thinks Opera is IE. But with the nifty little if IE, Opera is not detected as IE.

Anyway, I use this conditional statement all the time and it works great in IE, Opera, Mozilla, and Netscape. What I tried originally was
<!-- IE menu with no java -->
<!--[if IE]>
<?php include ("Borders/IEnoJavaMenu.php");?>
<![endif]-->
But of course the PHP gets read either way when the page is parsed. What would be ideal, is for the PHP not to be recognized when inside an HTML comment.

Again, thank you for your input in this odd request.

bird

2:02 pm on Apr 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP is executed on the server, before the page is delivered.
JavaScript is executed in the client, after the page is delivered.
So the latter could only influence the former if you had a time machine at hand...

topr8

2:03 pm on Apr 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



am i missing something.

php includes are included by the server,

and the javascript is run by the browser ... by which time the server has already served the page.

KingMacro

4:49 pm on Apr 12, 2005 (gmt 0)

10+ Year Member



the closest you can get to that is to detect the browser version from the request and try to include the correct one from there and hope that the user isn't trying to cloak the browser as something its not

as others said, the javascript is run on the client after the page is sent so by then its too late

you could have a "loading" page which uses javascript to redirect to either "?client=ie" or "?client=notie" if you really wanted to use javascript to detect the browser then have the PHP script check $_GET['client'], however that would be a horrible way to do it and you're better off using the client identified in the request and hope they aren't deliberately requesting the wrong content (which if they are - it really is their fault for messing with it, and they cant expect things to work if they try and prevent you being able to detect the browser to get them to work)

encyclo

5:05 pm on Apr 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As others have said, you can't use Javascript (or conditional comments) to determine the browser type as the PHP includes happen before the page ever gets sent to the browser.

A quick and dirty check for Internet Explorer would give something like this:

<?php if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
include ('file1.php');
}
else {
include ('file2.php');
};

With that I'm checking the user agent string for "MSIE" (all IE browsers should include that) and including file1.php, otherwise I'm including file2.php. You'll need to add further checking for Opera which includes the string "MSIE" when spoofing its user agent (Opera always has the word "Opera" in the string, spoofing or not).

Philly

5:43 pm on Apr 12, 2005 (gmt 0)

10+ Year Member



Thank you all for your wonderful help. Special thanks to encyclo for solving my problem!

The code I finally went with for anyone else who has the same problem is:

<?php if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE") && strstr($_SERVER['HTTP_USER_AGENT'], "Opera")) {
echo 'Stop trying to cloak your browser!';
}
else if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
include ('Borders/IEnoJavaMenu.php');
}
?>

The IEnoJavaMenu.php then swaps the JS code to make IE recognize my hover LI menu with an alternate menu if JS is disabled. The code above saves download time for non-IOE browsers.

Thank you again!