客户端和服务器端编程有什么区别? [英] What is the difference between client-side and server-side programming?

查看:304
本文介绍了客户端和服务器端编程有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

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

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

为什么这不会在我的文本文件中写入bar,但警告42?

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

注意:此问题的早期版本明确是关于服务器上的PHP和客户端上的JavaScript。当一个语言在客户端上运行而另一个在服务器上运行时,问题和解决方案的基本性质与任何语言相同。当你看到有关特定语言的答案时,请考虑到这一点。

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.

推荐答案

你的代码分为两个完全独立的部分, 服务器端客户端

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
                    |

双方通过HTTP请求和响应进行通信。 PHP在服务器上执行并输出一些HTML和JavaScript代码,这些代码作为响应发送到解释HTML并执行JavaScript的客户端。 PHP完成输出响应后,脚本结束,服务器上什么都不会发生,直到有新的HTTP请求进入。

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.

示例代码执行如下:

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

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

步骤1,PHP执行<?php?>之间的所有代码标签。结果如下:

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>

file_put_contents 来电没有任何结果,它只是在文件中写了+ foo +。 <?php echo 42; ?> 调用导致输出42,现在位于该代码的位置。

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.

生成的HTML / JavaScript代码现在发送到客户端,在那里进行评估。 警告调用有效,而 foo 变量不在任何地方使用。

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.

在客户端甚至开始执行任何JavaScript之前,所有PHP代码都在服务器上执行。 JavaScript可以与之交互的响应中没有任何PHP代码。

要调用某些PHP代码,客户端必须发送新的HTTP请求服务器。这可以通过三种可能的方法之一来实现:

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. 一个链接,导致浏览器加载新页面。

  2. 表单提交,它将数据提交给服务器并加载新页面。

  3. 一个 AJAX 请求,这是一种向服务器发出常规HTTP请求的Javascript技术(如1.和2.将),但不离开当前页面。

  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.

这是一个更详细概述这些方法的问题

您还可以使用JavaScript来制作浏览器使用 window.location 打开新页面或提交表单,模拟可能性1.和2.

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.

这篇关于客户端和服务器端编程有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆