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

发布于 2025-02-11 17:18:58 字数 438 浏览 1 评论 0 原文

我有此代码:

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

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

为什么这不将“栏”写入我的文本文件,而是警报“ 42”?


NB:此问题的早期修订明确是有关服务器上的PHP和客户端上的JavaScript。问题和解决方案的基本性质对于 的语言是相同的,当一种语言在客户端上运行,而另一个在服务器上运行(即使它们是同一语言)。当您看到有关特定语言的答案时,请考虑一下。

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 (even if they are the same language). Please take this in to account when you see answers talking about specific languages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2025-02-18 17:18:58

您的代码分为两个完全独立的部分,服务器端 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请求进入。

示例代码执行这样的执行:

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

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

步骤1,PHP在&lt;?php?&gt之间执行所有代码; 标签。结果是:

<script type="text/javascript">
    var foo = 'bar';

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

file_put_contents 呼叫没有导致任何内容,它只是将“ + foo + +”写入文件中。 &lt;?php Echo 42; ?&gt; 呼叫导致输出“ 42”,现在该代码曾经是该代码的位置。

现在,将此结果的HTML/JavaScript代码发送到对客户端的评估。 警报呼叫起作用,而 foo 变量均在任何地方使用。

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

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

  1. 一个链接之一,这会导致浏览器加载新页面。
  2. 表单提交,将数据提交到服务器并加载新页面。
  3. an ajax 请求,这是向常规http请求的JavaScript技术服务器(例如1。和2。将),但没有离开当前页面。

这是一个更详细概述这些方法的问题< /a>

您还可以使用JavaScript使用 window.location.location 或提交表单,模拟可能性1。和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. 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.

红焚 2025-02-18 17:18:58

要确定为什么 php 代码在 javaScript 代码中不起作用,我们需要了解 client side 服务器sine 语言是,以及它们的工作方式。

服务器端语言(php等):他们从数据库中检索记录,通过无状态 http 连接,并做很多需要安全性的事情。它们驻留在服务器上,这些程序从未将其源代码暴露于用户。

“来自Wikipedia_http://en.wikipedia.org/wiki/wiki/file/from
image attry> attry

因此您可以轻松地看到服务器端语言处理http请求并处理它们,正如@deceze所说, php在服务器上执行并输出一些HTML,也许是JavaScript代码,该代码是作为对客户端的响应发送的,在该客户端,该响应是对客户的解释和解释的,并且 执行JavaScript。

另一方面, 客户端脚本通常是指通过用户的Web浏览器执行的网络上的计算机程序类,而不是 server端

JavaScript对用户可见,并且可以轻松修改,因此对于安全性,我们绝不能依靠JavaScript。

因此,当您在服务器上提出A http 请求时,服务器首先仔细读取PHP文件,以查看是否需要执行任何任务,并向客户端发送响应。同样,正如@deceze所说, *一旦PHP完成了输出响应,脚本结束,服务器上什么也不会发生,直到新的http 请求出现为止。

“图形表示​​”

image source

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

  1. 您可以通过重新加载页面并发送a http 请求来做到这一点,
  2. 您可以使用JavaScript进行AJAX调用 - 这不需要重新加载页面

好读:

  1. wikipedia:server-side scippting
  2. wikipedia:客户端脚本

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.

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.

Image from wikipedia_http://en.wikipedia.org/wiki/File:Scheme_dynamic_page_en.svg
image 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.

On the other hand, Client Side Languages (like JavaScript) reside in browser and run in 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 is visible to the user and can be easily modified, so for security stuff we must not rely on 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. 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.*

Graphical representation

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.

  1. You can do so by reloading the page and sending a HTTP request
  2. You can make an AJAX call with JavaScript - this does not require reloading page

Good Read:

  1. Wikipedia : Server-side scripting
  2. Wikipedia : Client-side scripting
  3. Madara Uchiha : Difference between client side and server side programming
蛮可爱 2025-02-18 17:18:58

您的JavaScript将在客户端上执行,而不是在服务器上执行。这意味着 foo 未在服务器端进行评估,因此其值不能写入服务器上的文件。

思考此过程的最佳方法是,您正在动态生成文本文件。您要生成的文本仅一旦浏览器将其解释就变为可执行代码。只有您在服务器上评估&lt;?php 标签之间的位置。

顺便说一句,养成将随机的PHP逻辑嵌入HTML或JavaScript中的习惯可能导致严重的代码。我从痛苦的经历中说话。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文