Home  >  Q&A  >  body text

What is the difference between client-side and server-side programming?

<p>I have this code:</p> <pre class="brush:php;toolbar:false;"><script type="text/javascript"> var foo = 'bar'; <?php file_put_contents('foo.txt', ' foo '); ?> var baz = <?php echo 42; ?>; alert(baz); </script></pre> <p>Why doesn't this code write "bar" to my text file, but does it pop up "42"? </p> <hr /> <p>Note: An earlier revision of this question explicitly addressed PHP on the server and JavaScript on the client. When one language runs on the client and another on the server (even if they are the same language), the nature of the problem and solution are the same. Please consider this when seeing answers about specific languages. </p>
P粉757640504P粉757640504446 days ago558

reply all(2)I'll reply

  • P粉111927962

    P粉1119279622023-08-23 15:50:54

    To determine why PHP code is not working within JavaScript code, we need to understand what client-side and server-side are Languages, and how they work.

    Server-side languages ​​(such as PHP, etc.) : They retrieve records from the database, maintain state on a stateless HTTPconnection, and execute Many operations require security. They reside on the server and the source code of these programs is never exposed to users.

    So you can easily see that the server side language handles the HTTP request and processes it, as @deceze said, PHP executes on the server and outputs some HTML, and possibly JavaScript code, which The response is sent to the client, where the HTML is interpreted and JavaScript executed.

    On the other hand, Client-side languages ​​(such as JavaScript) reside in and run in the browser. Client-side script Generally refers to a computer program that is executed on the Web by the user's Web browser rather than the server side.

    JavaScript is visible to the user and can be easily modified, so for security issues we cannot rely on JavaScript.

    So when you make a HTTP request on the server, the server first carefully reads the PHP file to see if there are any tasks that need to be performed, and sends a response to the client. Again, as @deceze said, *once PHP has finished outputting the response, the script will end and nothing will happen on the server until a new HTTP request is received. *

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

    1. You can do this by reloading the page and sending a HTTP request
    2. You can use JavaScript to make AJAX calls - this does not require reloading the page

    Good reading materials:

    1. Wikipedia: Server Side Scripting
    2. Wikipedia: Client Script
    3. Madara Uchiha: The difference between client-side and server-side programming

    reply
    0
  • P粉103739566

    P粉1037395662023-08-23 09:17:40

    Your code is split into two completely separate parts, server-side and client-side.

                        |
                   ---------->
                  HTTP请求
                        |
    +--------------+    |    +--------------+
    |              |    |    |              |
    |    浏览器    |    |    |  网络服务器 |
    | (JavaScript) |    |    |  (PHP等)    |
    |              |    |    |              |
    +--------------+    |    +--------------+
                        |
      客户端            |      服务器端
                        |
                   <----------
              HTML, CSS, JavaScript
                        |
    

    The two parts communicate via HTTP requests and responses. PHP executes on the server and outputs some HTML and possibly JavaScript code, which is sent as a response to the client, where it interprets the HTML and executes the JavaScript. Once PHP finishes outputting the response, the script ends and nothing happens on the server until a new HTTP request comes in.

    The execution process of the sample code is as follows:

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

    In the first step, PHP executes all the code between the <?php ?> tags. The results are as follows:

    <script type="text/javascript">
        var foo = 'bar';
    
        var baz = 42;
        alert(baz);
    </script>

    file_put_contentsThe call produces no results, it just writes "foo" to the file. And <?php echo 42; ?>The result of the call is to output "42", which is now at the location of the original code.

    The resulting HTML/JavaScript code is now sent to the client, where it is evaluated. The alert call works and the foo variable is not used anywhere.

    All PHP code is executed on the server before the client starts executing any JavaScript. There is no PHP code left in the response to interact with JavaScript.

    To call some PHP code, the client must send a new HTTP request to the server. This can be achieved in one of three possible ways:

    1. link, causes the browser to load a new page.
    2. Form submission, submits data to the server and loads a new page.
    3. AJAX request, which is a JavaScript technology used to make a regular HTTP request to the server (similar to 1 and 2), but without leaving the current page.

    Here is an issue outlining these methods in more detail

    You can also use JavaScript to cause the browser to open a new page, using window.location or submit a form, simulating possibilities 1 and 2.

    reply
    0
  • Cancelreply