客户端和服务器端编程之间有什么区别?
我有这样的代码:
<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。当一种语言在客户端上运行而另一种语言在服务器上运行时(即使它们是相同的语言),问题和解决方案的本质对于任何语言都是相同的。当您看到谈论特定语言的答案时,请考虑这一点。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码分为两个完全独立的部分,服务器端和 client side 。
双方通过HTTP请求和响应进行交流。 PHP在服务器上执行,并输出一些HTML,也许是JavaScript代码,该代码以解释HTML并执行JavaScript执行的对客户端的响应发送。 PHP完成输出响应后,脚本结束,服务器上什么也不会发生,直到新的HTTP请求进入。
示例代码执行这样的执行:
步骤1,PHP在
&lt;?php?&gt之间执行所有代码;
标签。结果是:file_put_contents
呼叫没有导致任何内容,它只是将“ + foo + +”写入文件中。&lt;?php Echo 42; ?&gt;
呼叫导致输出“ 42”,现在该代码曾经是该代码的位置。现在,将此结果的HTML/JavaScript代码发送到对客户端的评估。
警报
呼叫起作用,而foo
变量均在任何地方使用。所有PHP代码均在服务器上执行,然后客户端甚至开始执行任何JavaScript。 JavaScript可以与之交互的响应中没有剩余的PHP代码。
要调用一些PHP代码,客户端将必须向服务器发送新的HTTP请求。这可能会使用三种可能的方法之一:
这是一个更详细概述这些方法的问题< /a>
您还可以使用JavaScript使用
window.location.location
或提交表单,模拟可能性1。和2。Your code is split into two entirely separate parts, the server side and the client side.
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:
Step 1, PHP executes all code between
<?php ?>
tags. The result is this: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 thefoo
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:
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.要确定为什么 php 代码在 javaScript 代码中不起作用,我们需要了解 client side 和服务器sine 语言是,以及它们的工作方式。
服务器端语言(php等):他们从数据库中检索记录,通过无状态 http 连接,并做很多需要安全性的事情。它们驻留在服务器上,这些程序从未将其源代码暴露于用户。
因此您可以轻松地看到服务器端语言处理http请求并处理它们,正如@deceze所说, php在服务器上执行并输出一些HTML,也许是JavaScript代码,该代码是作为对客户端的响应发送的,在该客户端,该响应是对客户的解释和解释的,并且 执行JavaScript。
另一方面, 客户端脚本通常是指通过用户的Web浏览器执行的网络上的计算机程序类,而不是 server端。
JavaScript对用户可见,并且可以轻松修改,因此对于安全性,我们绝不能依靠JavaScript。
因此,当您在服务器上提出A http 请求时,服务器首先仔细读取PHP文件,以查看是否需要执行任何任务,并向客户端发送响应。同样,正如@deceze所说, *一旦PHP完成了输出响应,脚本结束,服务器上什么也不会发生,直到新的http 请求出现为止。
现在,如果需要调用PHP,该怎么办?这取决于您需要如何做:通过重新加载页面或使用AJAX调用。
好读:
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.
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.*
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.
Good Read:
您的 Javascript 将在客户端执行,而不是在服务器上执行。这意味着
foo
不在服务器端进行计算,因此它的值无法写入服务器上的文件。考虑此过程的最佳方式是将其视为动态生成文本文件。您生成的文本只有在浏览器解释后才成为可执行代码。只有您放置在
标记之间的内容才会在服务器上进行评估。
顺便说一句,养成在 HTML 或 Javascript 中嵌入随机 PHP 逻辑片段的习惯可能会导致代码变得非常复杂。我是从痛苦的经历中说出来的。
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.