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

时间:2022-03-14 16:15:13

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. 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.

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

5 个解决方案

#1


394  

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.

双方通过HTTP请求和响应进行通信。 PHP在服务器上执行并输出一些HTML和JavaScript代码,这些代码作为响应发送到解释HTML并执行JavaScript的客户端。一旦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. The result is this:

第1步,PHP执行 标记之间的所有代码。结果如下:

<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.

file_put_contents调用没有产生任何结果,它只是将“+ foo +”写入文件中。 调用导致输出“42”,现在是该代码所在的位置。

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.

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

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.

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

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:

要调用某些PHP代码,客户端必须向服务器发送新的HTTP请求。这可以使用以下三种方法之一进行:

  1. A link, which causes the browser to load a new page.
  2. 一个链接,导致浏览器加载新页面。

  3. A form submission, which submits data to the server and loads a new page.
  4. 表单提交,将数据提交到服务器并加载新页面。

  5. 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.
  6. 一个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。

#2


138  

To determine why PHP code doesn't work in JavaScript code we need to understand what is client side and server side language 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. They reside on the server, these programs never have their source code exposed to the user

服务器端语言(PHP等):它们从数据库中检索记录,通过无状态HTTP连接维护状态,并执行许多需要安全性的事情。它们驻留在服务器上,这些程序永远不会将其源代码暴露给用户

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

So you can easily see that server side language handle HTTP request and process it and as @deceze said 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.

因此,您可以轻松地看到服务器端语言处理HTTP请求并处理它,并且@deceze表示PHP在服务器上执行并输出一些HTML和JavaScript代码,这些代码作为响应发送到解释HTML的客户端和JavaScript被执行。

While at the other hand Client Side Language (like JavaScript) resides on browser and runs at the browser, 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.

另一方面,客户端语言(如JavaScript)驻留在浏览器上并在浏览器上运行,而客户端脚本通常是指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 than The server first reads the PHP file carefully to see if there are any tasks that need to be executed and send response to client side and 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.*

因此,当您在服务器上发出HTTP请求时服务器首先仔细读取PHP文件以查看是否有任何需要执行的任务并将响应发送到客户端并再次发送@deceze说*一旦PHP完成输出响应,脚本结束,在新的HTTP请求进入之前,服务器上不会发生任何事情。*

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

Image source

So now what can I do if I need to call PHP? It depends how you need to do it: either by reloading the page or by using an AJAX call.

那么现在如果我需要调用PHP,我该怎么办?这取决于您需要如何操作:通过重新加载页面或使用AJAX调用。

  1. You can do by reloading page and send HTTP request
  2. 您可以通过重新加载页面并发送HTTP请求来完成

  3. you can make AJAX call with JavaScript and this does not require reloading page
  4. 你可以使用JavaScript进行AJAX调用,这不需要重新加载页面

Good Read:

  1. Wikipedia : Server-side scripting
  2. *:服务器端脚本

  3. Wikipedia : Client-side scripting
  4. *:客户端脚本

  5. Madara Uchiha : Difference between client side and server side programming
  6. Madara Uchiha:客户端和服务器端编程之间的区别

#3


22  

Your Javascript will execute on the client, not on the server. 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.

您的Javascript将在客户端上执行,而不是在服务器上执行。这意味着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.

考虑此过程的最佳方式就是您动态生成文本文件。您生成的文本只有在浏览器解释后才会成为可执行代码。只在服务器上评估

By the way, making a habit of embedding random pieces of PHP logic in HTML or Javascript can lead to seriously convoluted code. I speak from painful experience.

顺便说一句,养成在HTML或Javascript中嵌入随机PHP逻辑片段的习惯会导致严重错综复杂的代码。我说的是痛苦的经历。

#4


2  

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.In the java scenarioserver side programming servlets executes in the Tomcat, web-logic , j boss, WebSphere severs

客户端编程是带有Java脚本的html代码及其框架,库在Internet Explorer,Mozilla,chrome浏览器中执行。在java scenarioerver端编程servlet在Tomcat,web-logic,j boss,WebSphere severs中执行

#5


0  

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.
  2. HTML就是我们所看到的。

  3. CSS decides its designing(Colours, Floating Images, Padding, etc).
  4. CSS决定其设计(颜色,浮动图像,填充等)。

  5. JavaScript monitor page information. All the API calls and maintaining data over DOM is done by JavaScript.
  6. 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(休息,肥皂) ), 商业逻辑。

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,. These languages maintain certain API for client-side to interact with.
  2. 服务器端语言负责不同数据源之间的通信,如数据库,第三方API,文件系统,区块链等。这些语言为客户端维护某些API以进行交互。

  3. Database is responsible for storing information.
  4. 数据库负责存储信息。

  5. Business Logic defines "how to use data and what to do with data".
  6. 业务逻辑定义了“如何使用数据以及如何处理数据”。

Client-Side request data or request to store data, from Server-side via API provided by Server-Side. This request and response of data is done by following HTTP/FTP protocol like REST API, SOAP API.

客户端请求数据或存储数据的请求,从服务器端通过服务器端提供的API。此请求和数据响应通过遵循REST API,SOAP API等HTTP / FTP协议来完成。

#1


394  

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.

双方通过HTTP请求和响应进行通信。 PHP在服务器上执行并输出一些HTML和JavaScript代码,这些代码作为响应发送到解释HTML并执行JavaScript的客户端。一旦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. The result is this:

第1步,PHP执行 标记之间的所有代码。结果如下:

<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.

file_put_contents调用没有产生任何结果,它只是将“+ foo +”写入文件中。 调用导致输出“42”,现在是该代码所在的位置。

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.

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

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.

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

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:

要调用某些PHP代码,客户端必须向服务器发送新的HTTP请求。这可以使用以下三种方法之一进行:

  1. A link, which causes the browser to load a new page.
  2. 一个链接,导致浏览器加载新页面。

  3. A form submission, which submits data to the server and loads a new page.
  4. 表单提交,将数据提交到服务器并加载新页面。

  5. 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.
  6. 一个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。

#2


138  

To determine why PHP code doesn't work in JavaScript code we need to understand what is client side and server side language 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. They reside on the server, these programs never have their source code exposed to the user

服务器端语言(PHP等):它们从数据库中检索记录,通过无状态HTTP连接维护状态,并执行许多需要安全性的事情。它们驻留在服务器上,这些程序永远不会将其源代码暴露给用户

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

So you can easily see that server side language handle HTTP request and process it and as @deceze said 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.

因此,您可以轻松地看到服务器端语言处理HTTP请求并处理它,并且@deceze表示PHP在服务器上执行并输出一些HTML和JavaScript代码,这些代码作为响应发送到解释HTML的客户端和JavaScript被执行。

While at the other hand Client Side Language (like JavaScript) resides on browser and runs at the browser, 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.

另一方面,客户端语言(如JavaScript)驻留在浏览器上并在浏览器上运行,而客户端脚本通常是指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 than The server first reads the PHP file carefully to see if there are any tasks that need to be executed and send response to client side and 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.*

因此,当您在服务器上发出HTTP请求时服务器首先仔细读取PHP文件以查看是否有任何需要执行的任务并将响应发送到客户端并再次发送@deceze说*一旦PHP完成输出响应,脚本结束,在新的HTTP请求进入之前,服务器上不会发生任何事情。*

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

Image source

So now what can I do if I need to call PHP? It depends how you need to do it: either by reloading the page or by using an AJAX call.

那么现在如果我需要调用PHP,我该怎么办?这取决于您需要如何操作:通过重新加载页面或使用AJAX调用。

  1. You can do by reloading page and send HTTP request
  2. 您可以通过重新加载页面并发送HTTP请求来完成

  3. you can make AJAX call with JavaScript and this does not require reloading page
  4. 你可以使用JavaScript进行AJAX调用,这不需要重新加载页面

Good Read:

  1. Wikipedia : Server-side scripting
  2. *:服务器端脚本

  3. Wikipedia : Client-side scripting
  4. *:客户端脚本

  5. Madara Uchiha : Difference between client side and server side programming
  6. Madara Uchiha:客户端和服务器端编程之间的区别

#3


22  

Your Javascript will execute on the client, not on the server. 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.

您的Javascript将在客户端上执行,而不是在服务器上执行。这意味着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.

考虑此过程的最佳方式就是您动态生成文本文件。您生成的文本只有在浏览器解释后才会成为可执行代码。只在服务器上评估

By the way, making a habit of embedding random pieces of PHP logic in HTML or Javascript can lead to seriously convoluted code. I speak from painful experience.

顺便说一句,养成在HTML或Javascript中嵌入随机PHP逻辑片段的习惯会导致严重错综复杂的代码。我说的是痛苦的经历。

#4


2  

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.In the java scenarioserver side programming servlets executes in the Tomcat, web-logic , j boss, WebSphere severs

客户端编程是带有Java脚本的html代码及其框架,库在Internet Explorer,Mozilla,chrome浏览器中执行。在java scenarioerver端编程servlet在Tomcat,web-logic,j boss,WebSphere severs中执行

#5


0  

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.
  2. HTML就是我们所看到的。

  3. CSS decides its designing(Colours, Floating Images, Padding, etc).
  4. CSS决定其设计(颜色,浮动图像,填充等)。

  5. JavaScript monitor page information. All the API calls and maintaining data over DOM is done by JavaScript.
  6. 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(休息,肥皂) ), 商业逻辑。

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,. These languages maintain certain API for client-side to interact with.
  2. 服务器端语言负责不同数据源之间的通信,如数据库,第三方API,文件系统,区块链等。这些语言为客户端维护某些API以进行交互。

  3. Database is responsible for storing information.
  4. 数据库负责存储信息。

  5. Business Logic defines "how to use data and what to do with data".
  6. 业务逻辑定义了“如何使用数据以及如何处理数据”。

Client-Side request data or request to store data, from Server-side via API provided by Server-Side. This request and response of data is done by following HTTP/FTP protocol like REST API, SOAP API.

客户端请求数据或存储数据的请求,从服务器端通过服务器端提供的API。此请求和数据响应通过遵循REST API,SOAP API等HTTP / FTP协议来完成。