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

客户端编程主要涉及HTML、CSS和JavaScript,代码在浏览器中执行,影响用户界面。服务器端编程(如PHP)在服务器上运行,处理数据和业务逻辑。两者通过HTTP请求和响应通信。要调用服务器端代码,客户端需要发送新请求,可通过链接、表单提交或AJAX实现。
摘要由CSDN通过智能技术生成

本文翻译自:What is the difference between client-side and server-side programming?

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"? 为什么这不将“ bar”写到我的文本文件中,但提示“ 42”?


NB: Earlier revisions of this question were explicitly about PHP on the server and JavaScript on the client. 注意:此问题的早期修订版本明确涉及服务器上的PHP和客户端上的JavaScript。 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 (even if they are the same language). 当一种语言在客户端上运行而另一种语言在服务器上运行时,问题和解决方案的本质对于任何一对语言都是相同的(即使它们是相同的语言)。 Please take this in to account when you see answers talking about specific languages. 当您看到有关特定语言的答案时,请考虑到这一点。


#1楼

参考:https://stackoom.com/question/w4Wj/客户端编程和服务器端编程有什么区别


#2楼

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. 双方通过HTTP请求和响应进行通信。 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. PHP在服务器上执行,并输出一些HTML以及可能的JavaScript代码,作为响应发送给客户端,在客户端解释HTML并执行JavaScript。 Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in. PHP完成输出响应后,脚本将结束,并且在服务器上将什么都不会发生,直到有新的HTTP请求进入为止。

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. 步骤1,PHP执行<?php ?>标记之间的所有代码。 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. file_put_contents调用没有任何结果,它只是将“ + foo +”写入文件中。 The <?php echo 42; ?> <?php echo 42; ?> <?php echo 42; ?> call resulted in the output "42", which is now in the spot where that code used to be. <?php echo 42; ?>调用导致输出“ 42”,该输出现在位于该代码以前所在的位置。

This resulting HTML/JavaScript code is now sent to the client, where it gets evaluated. 现在,此生成的HTML / JavaScript代码将发送到客户端,并在此进行评估。 The alert call works, while the foo variable is not used anywhere. alert调用有效,而foo变量未在任何地方使用。

All PHP code is executed on the server before the client even starts executing any of the JavaScript. 在客户端甚至开始执行任何JavaScript之前,所有PHP代码都在服务器上执行。 There's no PHP code left in the response that JavaScript could interact with. JavaScript可以与之交互的响应中没有剩下PHP代码。

To call some PHP code, the client will have to send a new HTTP request to the server. 要调用一些PHP代码,客户端将必须向服务器发送新的HTTP请求。 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. AJAX请求,这是一种向服务器发出常规HTTP请求的Javascript技术(如1.和2.将),但不离开当前页面。

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. 您还可以使用JavaScript来使浏览器使用window.location打开新页面或提交表单,以模拟可能性1.和2。


#3楼

Your Javascript will execute on the client, not on the server. 您的Javascript将在客户端而不是服务器上执行。 This means that foo is not evaluated on the server side and therefore its value can't be written to a file on the server. 这意味着foo不会在服务器端进行评估,因此无法将其值写入服务器上的文件。

The best way to think about this process is as if you're generating a text file dynamically. 考虑此过程的最佳方法是好像正在动态生成文本文件。 The text you're generating only becomes executable code once the browser interprets it. 您生成的文本仅在浏览器解释后才成为可执行代码。 Only what you place between <?php tags is evaluated on the server. 服务器上仅评估您在<?php标记之间放置的内容。

By the way, making a habit of embedding random pieces of PHP logic in HTML or Javascript can lead to seriously convoluted code. 顺便说一下,养成在HTML或Javascript中嵌入随机PHP代码逻辑的习惯会导致代码复杂化。 I speak from painful experience. 我从痛苦的经历中发言。


#4楼

To determine why PHP code doesn't work in JavaScript code we need to understand what client side and server side languages are, and how they work. 为了确定PHP代码为什么不能在JavaScript代码中运行,我们需要了解什么是客户端服务器端语言,以及它们如何工作。

Server-side languages (PHP etc.) : They retrieve records from databases, maintain state over the stateless HTTP connection , and do a lot of things that require security. 服务器端语言(PHP等) :它们从数据库检索记录,通过无状态HTTP连接维护状态,并执行许多需要安全性的事情。 They reside on the server, these programs never have their source code exposed to the user. 它们驻留在服务器上,这些程序永远不会向用户公开其源代码。

图片来自Wikipedia_http://en.wikipedia.org/wiki/File:Scheme_dynamic_page_en.svgimage attr 图像属性

So you can easily see that server side languages handle HTTP requests and process them, and, as @deceze said, PHP is executed on the server and outputs some HTML, and maybe JavaScript code, which is sent as a response to the client, where the HTML is interpreted and JavaScript is executed. 因此,您可以轻松地看到服务器端语言处理并处理了HTTP请求,正如@deceze所说, PHP在服务器上执行,并输出一些HTML(也许是JavaScript代码),并作为响应发送到客户端, HTML被解释,JavaScript被执行。

On the other hand, Client Side Languages (like JavaScript) reside in browser and run in the browser. 另一方面, 客户端语言(如JavaScript)驻留在浏览器中并在浏览器中运行。 Client-side scripting generally refers to the class of computer programs on the web that are executed client-side, by the user's web browser, instead of server-side . 客户端脚本通常是指Web上由用户的Web浏览器而不是服务器端在客户端执行的计算机程序的类。

JavaScript is visible to the user and can be easily modified, so for security stuff we must not rely on JavaScript. JavaScript对用户是可见的,并且可以轻松修改,因此对于安全性方面,我们决不能依赖JavaScript。

So when you make a HTTP request on server, the server first reads the PHP file carefully to see if there are any tasks that need to be executed, and sends a response to the client side. 因此,当您在服务器上发出HTTP请求时,服务器首先会仔细读取PHP文件,以查看是否有任何需要执行的任务,然后将响应发送到客户端。 Again, as @deceze said, *Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in.* 再次,如@deceze所说,*一旦PHP完成输出响应,脚本便会结束,并且在服务器上什么也不会发生,直到新的HTTP请求进入为止。*

图示

Image source 图片来源

So now what can I do if I need to call PHP? 那么现在如果我需要调用PHP怎么办? It depends how you need to do it: either by reloading the page or by using an AJAX call. 这取决于您需要执行的操作:通过重新加载页面或使用AJAX调用。

  1. You can do so by reloading the page and sending a HTTP request 您可以通过重新加载页面并发送HTTP请求来实现
  2. You can make an AJAX call with JavaScript - this does not require reloading page 您可以使用JavaScript进行AJAX调用-不需要重新加载页面

Good Read: 读得好:

  1. Wikipedia : Server-side scripting Wikipedia:服务器端脚本
  2. Wikipedia : Client-side scripting Wikipedia:客户端脚本
  3. Madara Uchiha : Difference between client side and server side programming Madara Uchiha:客户端和服务器端编程之间的区别

#5楼

In web application every task execute in a manner of request and response. 在Web应用程序中,每个任务都以请求和响应的方式执行。

Client side programming is with html code with Java script and its frameworks, libraries executes in the internet explorer, Mozilla, chrome browsers. 客户端编程是使用带有Java脚本及其框架的html代码进行的,库是在Internet Explorer,Mozilla和chrome浏览器中执行的。 In the java scenario server side programming servlets executes in the Tomcat, web-logic , j boss, WebSphere severs 在Java场景中,服务器端编程servlet在Tomcat,web-logic,j boss,WebSphere服务器中执行


#6楼

I will try to explain it in simple way. 我将尝试以简单的方式进行解释。

Client Side is what user see/ code which is visible on browser. 客户端是用户在浏览器上看到/看到的代码。

Client Side Programming includes HTML(HTML, HTML5, DHTML), CSS(CSS, CSS3) and JavaScript(JavaScript, ES5, ES6, ES7, TypeScript, JQuery, ReactJs, AngularJs, BackboneJs or any other JavaScript Front-end framework). 客户端编程包括HTML(HTML,HTML5,DHTML),CSS(CSS,CSS3)和JavaScript(JavaScript,ES5,ES6,ES7,TypeScript,JQuery,ReactJs,AngularJs,BackboneJs或任何其他JavaScript前端框架)。

Client Side programming focus on "how page will look like" and its behavior over browsers. 客户端编程侧重于“页面外观”及其在浏览器中的行为。

  1. HTML is what we see. HTML是我们所看到的。
  2. CSS decides its designing(Colours, Floating Images, Padding, etc). CSS决定其设计(颜色,浮动图像,填充等)。
  3. JavaScript monitor page information. JavaScript监视器页面信息。 All the API calls and maintaining data over DOM is done by JavaScript. 所有API调用和DOM上的数据维护都是由JavaScript完成的。

Server Side Programming includes code which provide data to Client-Side. 服务器端编程包括向客户端提供数据的代码。 User is never able to see server-side. 用户永远无法看到服务器端。

Server Side Programming involves Programming Language(Java, PHP, .Net, C#, C, C++, NodeJS etc), Database(SQL, Oracle, MySql, PostgreySql, No-Sql, MongoDB, etc), Third Party API(Rest, Soap), Business Logic. 服务器端编程涉及编程语言(Java,PHP,.Net,C#,C,C ++,NodeJS等),数据库(SQL,Oracle,MySql,PostgreySql,No-Sql,MongoDB等),第三方API(Rest,Soap) ), 商业逻辑。

Server Side Programming focus on "how to make data available for Client-Side". 服务器端编程侧重于“如何使数据可用于客户端”。

  1. Server-Side Language is responsible for communicating between different source of data like database, third-party API, file system, blockchain etc,. 服务器端语言负责在不同的数据源之间进行通信,例如数据库,第三方API,文件系统,区块链等。 These languages maintain certain API for client-side to interact with. 这些语言维护用于客户端交互的某些API。
  2. Database is responsible for storing information. 数据库负责存储信息。
  3. Business Logic defines "how to use data and what to do with data". 业务逻辑定义“如何使用数据以及如何处理数据”。

Client-Side request data or request to store data, from Server-side via API provided by Server-Side. 客户端通过服务器端提供的API从服务器端请求数据或请求存储数据。 This request and response of data is done by following HTTP/FTP protocol like REST API, SOAP API. 数据请求和响应通过遵循HTTP / FTP协议(例如REST API,SOAP API)完成。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以。下面是一个简单的Python示例,演示如何使用Socket编程实现服务器端客户端程序的编写。 服务器端程序: ```python import socket # 创建一个socket对象 server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 获取主机名和端口号 host = socket.gethostname() port = 12345 # 绑定主机名和端口号 server_socket.bind((host, port)) # 开始监听客户端连接 server_socket.listen(5) # 服务器端等待客户端连接 while True: # 接受客户端连接 client_socket, addr = server_socket.accept() print('Got connection from', addr) # 向客户端发送数据 message = 'Thank you for connecting' client_socket.send(message.encode()) # 关闭客户端连接 client_socket.close() ``` 客户端程序: ```python import socket # 创建一个socket对象 client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 获取主机名和端口号 host = socket.gethostname() port = 12345 # 连接服务器 client_socket.connect((host, port)) # 接收服务器发送的数据 message = client_socket.recv(1024).decode() print(message) # 关闭客户端连接 client_socket.close() ``` 在这个示例中,服务器端客户端程序都使用了Python的`socket`模块。服务器创建了一个`socket`对象,并通过`bind()`方法绑定了主机名和端口号,然后通过`listen()`方法开始监听客户端连接。在接受到客户端连接后,服务器向客户端发送了一条消息,并关闭了客户端连接。 客户端程序先创建了一个`socket`对象,然后通过`connect()`方法连接到服务器。客户端接收到服务器发送的一条消息后,关闭了连接。 这只是一个简单的示例,实际应用中,Socket编程可以用于实现各种各样的应用程序,例如聊天程序、文件传输程序等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值