客户端编程和服务器端编程有什么不同?

oyxsuwqo  于 2022-09-18  发布在  Java
关注(0)|答案(4)|浏览(208)

我有这样的代码:

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

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

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

注:这个问题的早期版本明确地提到了服务器上的PHP和客户端上的JavaScript。当一种语言在客户机上运行,另一种语言在服务器上运行时,问题和解决方案的本质对于任何一对语言都是相同的(即使它们是相同的语言)。当你看到关于特定语言的答案时,请考虑到这一点。

qfe3c7zg

qfe3c7zg1#

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

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

双方通过HTTP请求和响应进行通信。PHP在服务器上执行,并输出一些HTML代码,可能还有作为响应发送给客户端的JavaScript代码,在客户端解释该HTML并执行该脚本。一旦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执行<?php ?>标记之间的所有代码。结果是:

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

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

file_put_contents调用没有产生任何结果,它只是将“+foo+”写入文件。<?php echo 42; ?>调用产生输出“42”,它现在位于该代码曾经所在的位置。

现在,将生成的HTML/JavaScript代码发送到客户端,在那里对其进行计算。alert调用有效,而foo变量不在任何地方使用。

所有的PHP代码甚至在客户端开始执行任何JavaScript之前就已经在服务器上执行了。在响应中没有留下可供JavaScript与之交互的PHP代码。

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

1.一个链接,使浏览器加载一个新页面。
1.表单提交,将数据提交到服务器并加载新页面。
1.AJAX请求,这是一种向服务器发出常规HTTP请求(如1.和2.will)的Java脚本技术,但不离开当前页面。

Here's a question outlining these method in greater detail

您还可以使用JavaScript让浏览器使用window.location打开一个新页面或提交一个表单,模仿可能性1.和2。

kmpatx3s

kmpatx3s2#

To determine why PHP code doesn't work in JavaScript code we need to understand whatclient sideandserver sidelanguages 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 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.

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
xlpyo6sf

xlpyo6sf3#

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.

kx5bkwkv

kx5bkwkv4#

In web application every task execute in a manner of request and response.

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 scenario server side programming servlets executes in the Tomcat, web-logic , j boss, WebSphere severs

相关问题