Most foundations. How does PHP.


  • Web application Unlike regular program
  • How does PHP, where he performed?
  • How to pass a variable from PHP to JavaScript and vice versa?
  • Methods of communication with the server browser.
  • Viewing HTTP headers exchange
  • VERY IMPORTANT
  • Comments

    Web application Unlike regular program
    Starting to write programs for the Web, many beginning programmers encounter this error. They see the browser-server system as a normal application. Interactive. He pressed the button - the system responded. Spent a mouse - I responded. All the information that is available to the customer - and the program is available, the program is always in memory.
    So, in web programming is not so! .
    At a time when the user sees a page and starts to make some action with it, PHP has already completed work! And not a user interacts with a PHP script and the HTML page with her, which he received in the browser. The result of a PHP script in most cases is plain text. The text of an HTML page. Who gave the browser and displays them as a normal HTML. You can see for yourself by writing a script
    <? echo "Привет, <b>Вася!</b>" ; ?> ; And then looking at the browser source code of the page received. No PHP tag is not there! Only
    Привет, <b>Вася!</b>
    Because PHP is executed on the server!


    The server and the browser communicate by sending each other requests for a special protocol - HTTP. The connection can only initiate a browser. He sends the server a request - show a certain file. The server sends the file to the client.
    Just what happens. The client requested - server gave. And immediately I forgot about the customer. It becomes clear answer to the question whether it is possible to know exactly how many users sechas online. You can not. because "on site" does not have any. They are connected, request pages and disconnected. Do not have a permanent Connection to the server, such as players Kwaku. Learn about only by recording the time of each connection and selecting records for a certain period of time.

    Also, from here it becomes clear that the server can identify the client is very small. Only the fact that the client sends in the HTTP-request. Screen resolutions are not there ;-)
    All that the server can know about the client, you can see the team
    phpinfo()
    Example browser communicate with the server:
    The user clicks on the link, the browser sends a request to the server and waits for a response:
    Browser -> PHP

    PHP executes the script, gives the result in the browser and exits:
    PHP -> Browser

    The browser renders the page, "looking" for her links that should be requested from the server (tag <img src>, <script src > , and so on), and sends the appropriate inquiries. They can be seen by viewing the exchange of titles, as it will be just below:
    Browser -> Server Browser -> Server Browser -> server ...

    The user fills out the form and clicks on the button:
    Browser -> PHP

    PHP handles the form, write data to the database and sends the browser Location header:
    PHP -> Browser

    The browser received the title, given page requests
    Browser -> PHP

    PHP executes it ... and so on.

    How does PHP, where he performed?
    PHP runs on the server. The browser sends a request to a server with php code page. The server sends the page to execute the interpreter PHP, interpreter generates HTML code that gives the server, and the server sends to the client. No PHP code in the browser does not get (this is important! This means that to see the PHP script code is not possible!). The only way to send something to the script - is click on the link or click on the button in the form. So that PHP handles some user actions in the browser - it is impossible. PHP left on the server, waiting for new requests to the data for processing. PHP, but not script! The script, which was carried out, giving the user a page has finished work. All data that was in it - gone. That is why, if some variable is needed for subsequent calls to the script, it is necessary to pass this script again.

    How to pass a variable from PHP to JavaScript and vice versa?
    Let's start with the fact that no variable is passed, of course, impossible. Since the variable - is part of the program. And from one to the other to pass it nalzya. Send only variable. Those. text. That is, the difference between the "transmission variable in JavaScript" and the formation of html table NO NO!
    Hence the conclusion - "Pass the variable" in Javascript is very easy. Especially, I repeat, if we consider that there is no "transfer" does not occur. Quite simply PHP generates JavaScript the same way as the rest of the page, along with all the variables.
    In the same way as you draw a line in the browser "Hello World, this is John Doe!" Is displayed and any JavaScript, with all its variables.
    The only condition - you have to imagine the javascript you want to receive.
    For example, in PHP there is a variable $name = "Вася" $name = "Вася" Whose value should be transferred to JavaScript, to get
    <script>name="Вася";</script>
    We just write
    <?
    $name
    = "Вася" ;
    ?>
    <script>name=" <? echo $name ; ?> "</script>
    <?
    $name
    = "Вася" ;
    ?>
    <script>name=" <? echo $name ; ?> "</script>
    echo $ name;?>" </ script> That is, in fact, we have just formed our PHP script, a text that looks like you want us to code javascript. Or, on the other hand, we wrote a javascript in the right places by inserting the output variables from PHP.
    In order not to go crazy with different quotes, it is strongly recommended that all the javascript output not via the echo, and exactly as written here - closing PHP tag and opening them only where you want to display the variable.

    How to pass a variable from Javascript to PHP?
    Just like any other data - sending a request to the server.
    But we must clearly understand that to get something from the javascript, of course, not possible during the execution of php script. Can only transmit on the next request. And handle it will be another PHP script.
    If necessary but rbratitsya onClick event to a database, it should be remembered that it is on the server. That is, it is necessary to request a server that runs PHP script that will appeal to the database, get an answer from her and give it to the browser.

    All of the above does not conflict with, of course, fashion technology asynchronous communication with the server browser. All the methods are the same, just communicating with the server performs not the browser itself, but on javascript program.
    More details can be read on the resources devoted to JavaScript. From the perspective of PHP AJAX-requests are no different from the usual.

    Methods of communication with the server browser.
    Ways provided by the HTTP protocol, a little. This is important information. No other way is not present. In practice, use two:
    GET - this is when the data is transmitted in the address bar, for example, when the user clicks a link.
    POST - when he presses a button on the form.
    Formed page with a link or a form of the GET method - a request comes GET-th. Formed with the form in which the specified POST method - come POST-th.
    To determine which method should be used, it is very simple. If the form used to request certain information, for example - in the search, it should send the GET method. To be able to refresh the page, you can bookmark and or send to a friend.
    If as a result of the form submission data is written to or changed on the server, you should send them to the POST method, and certainly after the form processing is necessary to redirect the browser using the GET method. Also, POST may be necessary if the server is necessary to transfer a large amount of data (in GET it is very limited), as well as if the "light" should not be passed to the data in the address bar (when entering the user name and password, for example). But in any case, after the POST processing should always redirect the browser to any page, even if the same, but without the form data to the upgrade are not re-written page. For example:
    header ( "Location: http://" . $_SERVER [ 'HTTP_HOST' ]. $_SERVER [ 'REQUEST_URI' ]);
    exit;

    The most important thing to remember: the server on their own initiative to apply to the client can not. We can only upon request to give something to the browser - or page, or click to request a different resource.

    Useful information may be contained in various HTTP headers.
    Cookie - if the server set a cookie, and it is up to date, the browser sends it along with each request.
    HTTP authentication - if the HTTP server requests authentication, the browser sends whenever address entered login and password.

    PHP can send HTTP headers to two teams - header () and setcookie ().

    Viewing HTTP headers exchange
    I highly recommend practicing with HTTP headers, see how they exchange server and the client.
    To do this, there are many different ways. If you have a popular download manager FlashGet, you can use it. Just headers shows popular Proxomitron program, you can download any special tools.
    Also, there are many other utilities, being found easily on the network by HTTP sniffer request.
    Be sure to use any means to see the HTTP headers that the browser communicates with the server. This is a very good practice, as well as check - that sends your script. It is convenient when debugging installation cookies or sessions issues.
    A rough idea of who came Titles can also be obtained by using the function getallheaders (). But it should be borne in mind that it only works when PHP is compiled as a module.

    VERY IMPORTANT
    From the fact that PHP is executed on the server, and sends the result to the browser, it should be a simple, but very important conclusion. What PHP, in principle, CAN NOT be displayed in a browser is nothing that could not be done by means of html.
    BEFORE you write something on PHP - try to make it pure HTML.
    "Pressing Enter" does not translate the string? And in html you do not try so newline? Did not work out? What a shame. Read how in html make a line break and come back.

    PHP as a result of their work forms the image with no text, as you see it on the screen! PHP generates HTML code! And this code is significantly different from the image you see on the screen. If you have something does not work, you have to always look exactly the page's source code, and not how it draws you browser. In Internet Explorer, the source code can be viewed by selecting the View menu - View HTML-Code.
    If you are not running Javascript generated PHP script or html shows not what you want, then this is easy to fix the problem.

    1. First, you write the necessary JavaScript or html hands. If you have a problem with that - please contact the appropriate forum - for javascript or html. PHP nothing to do with.
    2. compare with those obtained from the PHP
    3. To make corrections in the PHP script to the text, to give them, did not differ from what is written by hand.

    The browser can not display files that crammed both html pictures. The browser can display only known to him data types. In particular, this or html or images. But not together. If the picture - the ONE. Some pictures show the browser in a row can not. The browser can display HTML, which defines the references to several images.
    Please, before you learn PHP - learn at least the basics HTML! Before you demand something from PHP - try to do it on the html.