简体   繁体   中英

How do I keep a PHP code alive?

I am a newbie at web programming. I have basic PHP programming skills.

From what I understand about PHP is, it runs on the server side and generates a HTML code, server-side PHP script ends, and then that generated HTML code is sent to us (us = browsers of the clients). But in some webpages (programmed with PHP) which I encounter on the internet, the server-client interaction keeps running silently at the background even a long time after the page had completed loading.

For example, in the Stack Exchange communities, when I am viewing new questions I see that the vote counts are changing (as other users vote) without refreshing the page. This indicates that a background script is communicating with a database, right?

Are these kind of pages implemented by pure PHP?
If yes, I thought PHP script had to finish at the server side before the generated HTML code to be sent. What is the secret of this method? If no, what additional programming languages and/or web technologies do I have to study to learn it? Do they combine another technologies with PHP?

Please guide me on this.

这不是PHP主动运行,这是AJAX

No single language can build an entire webpage. The modern webpages you see are generated by using many languages.

You are asking about means to an end (how to make pages dynamic) when is seems what you need to know about is an end (dynamic pages).

You are basically correct about how PHP works.

From what I understand about PHP is, it runs on the server side and generates a HTML code, server-side PHP script ends, and then that generated HTML code is sent to us (us = browsers of the clients).

However, you overlooked a thing about how webpages work: for the server to send anything, a webpage for instance, there must be request from the client. Therefore, it would be more appropriate to ask how the server can do this without a request from the client.

To answer some of your questions:

Are these kind of pages implemented by pure PHP? --> No it isn't.

If no, what additional programming languages and/or web technologies do I have to study to learn it? --> Rather than particular PLs or technologies it would be wiser to think in the lines of "How can the server initiate communication with the client (browser/page)?" (or make it appear as though) as this is the "secret" (or more appropriately the key) in implementing such pages. You can find a number of technologies, techniques, and combination of both that deals with this (push technology, long polling, ajax, etc.).

Do they combine another technologies with PHP? --> Yes, as with most stuff on the web, dynamic pages are the combinations of a number of technologies and techniques.

I believe you looked at what you want to accomplish on the wrong angle. Hope this gets you on track!

The front-end continues to comunicate with the back-end even after the server-side processing is over using AJAX. AJAX can and should be combined with any back-end technology like JAVA/ ASP/ PHP/ PYTHON etc. in order to create a dynamic website.

The short answer is: you can't.

The continuous interaction that you see in some websites is achieved by using a Javascript technique called Ajax, which periodically (or based on an browser event like a mouse click) calls a PHP script in the background and does something based on that response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM