douhuan9289 2018-12-24 21:19
浏览 106

如何包含一个PHP文件的文件夹,使应用程序成为一个HTML页面?

I've built a standalone php chat app. Works great with the classic localhost/chat, except I need it to run on an html page I already have. Does it have to be strictly html, the answer is no. The thing is, I know a browser can render html, but it can't render php.

I would just really like to know if there was any way for me to import a php app to a website. Chat app is consisted of 3 files, 2 of them are php which define the form, look and post method, the 3rd one is a log.html, which is used as a dumping place, that's how the app displays the messages,connected users, leaving users, etc.

I tried several different things.

-Tried using iframes

No good. It display raw php code

-Placed everything in www folder in wamp, tried to "require" the files in html page

No good, displays nothing.

There's really no code for me to show off, I've got everything working separately, except I need the php to work from inside my contact.html page.

  • 写回答

1条回答 默认 最新

  • duanji9311 2018-12-24 22:37
    关注

    It sounds like you simply don't understand HTML or PHP. You say that "The thing is, I know a browser can render html, but it can't render php." as if you're doing us a favor, when all it does is confuse us and then you attack us in the comments. A browser can 100% render a PHP file as long as it sends over HTML to the client, and seeing as you believe your PHP app works right now I'm not sure how you've tested it other than through the browser. Maybe you've used PHP from the command line but there'd be no graphical interface, and any HTML code (such as the form you say you've made) would simply be printed into the console where it should be obvious to you that a browser receiving the same thing would render it as HTML.

    To prove this to you, just take a look at facebook:

    • https://www.facebook.com/ will direct you to the index.XXX within the root directory which is standard knowledge when working on sites.
    • https://www.facebook.com/index.html sends you to the error page as it doesn't exist (no html exists at index.html they just have the server configured to serve the error page any time a bad request comes in).
    • https://www.facebook.com/index.php works perfectly because that's the page that is normally chosen as the index.XXX and is rendered.

    And finally, some demo code to prove my point:

    <?php
    
    $thingA = "dogs";
    $thingB = "cats";
    $finalThing = $thingA . $thingB;
    
    ?>
    

    The above code would work perfectly fine, $finalThing would be "dogscats", and your server would take the output of the file (nothing) and send it as HTML.

    <?php
    
    $thingA = "dogs";
    $thingB = "cats";
    $finalThing = $thingA . $thingB;
    
    echo $finalThing;
    
    ?>
    

    The above code would do everything the first example did, but the output "dogscats" would be sent to the client as HTML. This would just render the text "dogscats" as if you had an HTML file with just those characters in it (no html tag, body tag, meta tags, or anything else at all).

    <?php
    
    $thingA = "dogs";
    $thingB = "cats";
    $finalThing = $thingA . $thingB;
    
    echo "<!doctype html>";
    echo "<html lang='en'>";
    echo "<body>";
    echo "\t<p> " . $finalThing . "</p>";
    echo "</body>";
    echo "</html>";
    
    ?>
    

    The above would print a valid HTML file (without meta tags or many other useful things but sorry, I don't want to write much more and there's still another example) which should look exactly like the previous example, but with the text displayed on the screen being in a p tag rather than being raw text just sitting there.

    <!doctype html>
    <html lang="en">
        <head>
            <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
            <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
            <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
            <meta charset='UTF-8'/>
            <title>Website</title>
            <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
        </head>
    
        <body>
            <?php
    
            $thingA = "dogs";
            $thingB = "cats";
            $finalThing = $thingA . $thingB;
    
            echo "\t<p> " . $finalThing . "</p>";
    
            ?>
        </body>
    </html>
    

    And this final example is the best of all, as it shows that a PHP file is simply an HTML file with <?php ?> tags. These tags are parsed by PHP, evaluated, and anything that needed to get printed in the middle (or wherever) of the HTML file is printed right there. Then the resulting HTML file (including both the raw HTML and the results of your php tags) is sent to the client.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图