ℙℕℤℝ 2012-12-12 13:03
浏览 206
已采纳

客户端和服务器端编程之间有什么区别?

I have this code:

<script type="text/javascript">
    var foo = 'bar';
    <?php
        file_put_contents('foo.txt', ' + foo + ');
    ?>

    var baz = <?php echo 42; ?>;
    alert(baz);
</script>

Why does this not write "bar" into my text file, but alerts "42"?


NB: Earlier revisions of this question were explicitly about PHP on the server and JavaScript on the client. The essential nature of the problem and solutions is the same for any pair of languages when one is running on the client and the other on the server. Please take this in to account when you see answers talking about specific languages.

转载于:https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming

  • 写回答

5条回答 默认 最新

  • 胖鸭 2012-12-12 13:03
    关注

    Your code is split into two entirely separate parts, the server side and the client side.

                        |
                   ---------->
                  HTTP request
                        |
    +--------------+    |    +--------------+
    |              |    |    |              |
    |    browser   |    |    |  web  server |
    | (JavaScript) |    |    |  (PHP etc.)  |
    |              |    |    |              |
    +--------------+    |    +--------------+
                        |
      client side       |      server side
                        |
                   <----------
              HTML, CSS, JavaScript
                        |
    

    The two sides communicate via HTTP requests and responses. PHP is executed on the server and outputs some HTML and maybe JavaScript code which is sent as response to the client where the HTML is interpreted and the JavaScript is executed. Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in.

    The example code executes like this:

    <script type="text/javascript">
        var foo = 'bar';
        <?php
            file_put_contents('foo.txt', ' + foo + ');
        ?>
    
        var baz = <?php echo 42; ?>;
        alert(baz);
    </script>
    

    Step 1, PHP executes all code between <?php ?> tags. The result is this:

    <script type="text/javascript">
        var foo = 'bar';
    
        var baz = 42;
        alert(baz);
    </script>
    

    The file_put_contents call did not result in anything, it just wrote " + foo + " into a file. The <?php echo 42; ?> call resulted in the output "42", which is now in the spot where that code used to be.

    This resulting HTML/JavaScript code is now sent to the client, where it gets evaluated. The alert call works, while the foo variable is not used anywhere.

    All PHP code is executed on the server before the client even starts executing any of the JavaScript. There's no PHP code left in the response that JavaScript could interact with.

    To call some PHP code, the client will have to send a new HTTP request to the server. This can happen using one of three possible methods:

    1. A link, which causes the browser to load a new page.
    2. A form submission, which submits data to the server and loads a new page.
    3. An AJAX request, which is a Javascript technique to make a regular HTTP request to the server (like 1. and 2. will), but without leaving the current page.

    Here's a question outlining these method in greater detail

    You can also use JavaScript to make the browser open a new page using window.location or submit a form, emulating possibilities 1. and 2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理