基于浏览器特性动态包含PHP

dynamically include PHP based on browser features

本文关键字:动态 包含 PHP 浏览器      更新时间:2023-09-26

重写

我有一个用HTML5编写的网站,还有一个用XHTML编写的网站。我想根据是否有人使用支持HTML5一些最基本功能的浏览器来呈现其中一个。

注意

该网站不使用HTML5的画布、音频或视频功能。它只是简单地标记了side、section、nav等,并使用CSS3的一些有趣功能进行造型装饰。HTML5网站和XHTML网站之间的差异很小,如果我能做到这一点,可能任何人都不会注意到。内容是一样的,只是呈现方式略有不同。

我这样做的原因

一旦恐龙浏览器消失了,我希望我可以简单地发布HTML5网站,并废除旧的XHTML。

我遇到了一些后勤方面的障碍,还没有完全制定出我想怎么做。我最初的想法是使用Javascript条件语句来确定要渲染的PHP。是的,继续笑吧,也许有一天我也会笑的。在对此进行调查时,有人评论说,XML可能使这成为可能。我对Javascript、PHP和XML都很在行。这是我第一次尝试将Javascript与PHP集成,所以现在我明白了为什么我最初的计划需要更多的工作。

最终,我非常强烈地感觉到这就是我想要前进的方式。我读到关于渐进增强与优雅降级的文章,但我决定给我的客户一个美丽的网站,使用所有新的语义标签和简单的风格选择器来提高SEO,并保证当HTML4消失时,这个新网站将经得起时间的考验。。。至少有一段时间。

如果你强烈反对这种方法,我愿意听听你的意见。请以任何一种方式分享你的想法。

你想要的是不可能的;正如我已经解释过的,PHP是服务器端,JS是客户端;php端完成的任何操作都是在页面交付给用户时完成的,因此js不可能影响php端,除非您的网站或内容交付在ajax中完全完成,ajax是一种使用js和php检索信息的方法;简而言之,js向服务器上的另一个php页面发送请求并返回结果。

然而,这要复杂得多,在您更熟悉JS和PHP之前,我不建议您使用它。

不过,除此之外,php中还有一个解决方案,尽管我现在还没有完整的代码。

解决方案是get_browser():的php 4和5函数

$arr = get_browser(null, true);
$var = "some browser";
if ($arr['parent'] == $var) {
   require('/php/file.php');
}
else {
   //etc
}

以上是在您的答案更新之前;关于上述更新,我没有其他要说的。

更新:关于下面关于ajax的一条评论,我将尝试。。一个例子。我不会试图称之为"简单",因为ajax绝不是。。尽管回到正题。。。

HTML:

<html>
    <body>
        <div id="main_body">
        </div>
    </body>
</html>

JS:

//some code to determine user-agent/browser, set variable 'agent' with result
var use_html5;
if (agent == browser) {
    use_html5 = 'yes'
}
else {
    use_html5 = 'no'
}
function retrv_body() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {//readState 4 is when the request has finished;
            //0: request not initialized 
            //1: server connection established
            //2: request received 
            //3: processing request 
            //4: request finished and response is ready
            document.getElementById('main_body').innerHTML = xmlhttp.responseText;
            //set html of div with id 'main_body' to rendering retrieved from php_file_in_same_dir.php
        }
    }
    xmlhttp.open("POST","php_file_in_same_dir.php",true); 
    //set type of form, boolean is in regards to whether the request is asynchronus or synchronous
    //most ajax requests are async, which means they themselves finish executing usually after the function itself has run.  I'm not truly knowledgeable regarding this specific thing since I've only ever used async requests, though I would assume being a sync request would mean the function actually waits until it returns a value before it finishes executing.
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    //set the headers of the content.
    xmlhttp.send("html5=" + use_html5);
    //finally, send the data.  Depending on the data, the data may need to be url-encoded.
}
retrv_body();

PHP:

<?php
if ($_POST['html5'] == 'yes') {
    include('body5.php');
}
else {
    include('body_other.php');
}
//body generating code, render page.
?>

以上只是一个例子,我不建议实际使用它,保存retrv_body()函数,并将其更改为真正可以使用的函数。

希望我在代码中的注释将有助于理解;如果还有什么问题,请随时要求我更彻底地解释。

编号编号编号编号!

两点:

  1. 使用HTML5 shiv-赋予旧版IE浏览器HTML5功能)
  2. 渐进增强-你不应该为不同的浏览器编写代码。您的代码应该可以在所有浏览器中使用。然后添加一些功能,这些功能可以增强用户体验,但不是网站运行所必需的