Cupidvogel Posted April 18, 2012 Share Posted April 18, 2012 Hi, imagine a site like Facebook, where lots of content are being uploaded dynamically and asynchronously. How is the page stored in a server? For example, imagine a wall post where lots of friends (including me) are commenting. Now I am in my home-page when I see a notification in the crown-jewel, stating that somebody has commented, I click that, and is taken to that page. Now when I say "taken to the page", and I am seeing its contents, what I want to know is how is the page served, does the page exist just as HTML page in the server from where it is served, where each update, after being sent from client to server (through Ajax, perhaps), invokes a PHP/Perl script that opens the page (as a normal text file), appends the content, and closes it, or is it that the update invokes a script that updates a database with the comment, the commenter, (or the person who has liked a comment), and when a request arrives from the client for that page, a script uses a basic HTML/CSS template and inserts all the necessary comments and other metadata from the database and dynamically serves the page? If it is the second case, does the URL resource actually need to exist, for I think that the script can be configured to do its task whenever it receives that client request, prepare a page on the fly, and change its URL through mod_rewrite? Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/ Share on other sites More sharing options...
plznty Posted April 18, 2012 Share Posted April 18, 2012 What is your actual question? If its longer than 2 lines it is not very clear usually. Are you asking what Facebook actually does or a way it could be done? Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338537 Share on other sites More sharing options...
Cupidvogel Posted April 18, 2012 Author Share Posted April 18, 2012 Ummm, please tell me which part of the question was not clear, I will elaborate. What I want to know is how to serve dynamic pages, by regularly updating them through server side code when client does something, or just storing all client-supplied data in database, retrieve them when a client requests that page, and update a copy of the page (a template, perhaps) with that data, and then serve it. Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338538 Share on other sites More sharing options...
plznty Posted April 18, 2012 Share Posted April 18, 2012 You would use a database and use a Model View Controller (MVC) perhaps. That way you could format the data accordingly. Facebook does use PHP, they only use some other tools because of scaling. I don't really see your question though, what makes you think you cannot achieve your task using a database etc? Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338540 Share on other sites More sharing options...
Psycho Posted April 18, 2012 Share Posted April 18, 2012 There is no all encompassing answer to your request. A "dynamic" page can be a page that is 99% static (e.g. HTML code) which simply has a value that is replaced dynamically. Or the entire structure and content of the page can be generated in real-time. It all depends upon the needs of the application. Â For an example, let's say you want to be able to display user profile information. You wouldn't want to create static pages for each user. Instead you build a "template" or wire-frame for a profile page. That page would be used to show the profile for any user. For example, when calling the page the user would click on a link for "Bob's" profile. That link could point to the template page and pass the unique ID for bob: display_profile.php?id=23. The template page would take that ID and query the database for bob's profile information. Then it would inject that profile information into the page and return the result back to the user. Â But, that is only one example. The process could be extremely complex where the process determines which type of content from many different possible output and uses many different files to create and generate different parts of the output. It would not be uncommon for a page request to 10, 20, 30 or more different files to generate the output for a single page. Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338541 Share on other sites More sharing options...
Cupidvogel Posted April 18, 2012 Author Share Posted April 18, 2012 I think an example will help you to help me. Suppose there is answers forum, where, on a particular question page (like this one), a user has asked some question, and I gave the answer. Prior to my answering, surely the page existed as a HTML page in the server, right? Now after I have answered, and after some other users had answered as well, another user will now see the updated page, a question followed by some answers. What I want to know is what is the way to update the page. Suppose the original page (before anybody answered) had a html structure like this: Â <html> <head> <title>Blah</tilte> <link rel = "stylesheet" style = "text/css" href = "blah.css" /> <script type = "text/javascript" src = "blah.js"></script> </head> <body> <div> The actual question is here. </div> </body> </html> Â This is the current HTML file as stored in the server. When some guys post answers, and afterwards see the page, the page source should be something similar to the above (except for the last few answer divs): Â <html> <head> <title>Blah</tilte> <link rel = "stylesheet" style = "text/css" href = "blah.css" /> <script type = "text/javascript" src = "blah.js"></script> </head> <body> <div> The actual question is here. </div> <div id = "answer1">Answer1</div> <div id = "answer2">Answer2</div> <div id = "answer3">Answer3</div> </body> </html> Â So now that they are seeing a changed page, what I want to know is where has the change occurred. It can be either that when a user has posted an answer, a Perl script in the server receives that post, opens the HTML file in append-mode, and appends the answer, after wrapping it with div tags and other appropriate metadata, so that the HTML file has been modified in the server in-place, so that next request for that page will automatically reflect this change. Or is it done this way, that once the user has posted a question, no further modification is done to that page, instead a table in a database is associated with that question, and each subsequent answer is added to that table through a script when a user posts an answer, and when a page request is made, another script extracts each answer and its metadata from the database, formats them to fit into the HTML page (like wrapping them within <div id = "answer2" class = "answer"></div> tags) appends them to a copy of the actual HTML file, and serves it to the user? The second option kind of looks more flexible to me, as it offers much more fine-grained control over the posts, whereas with the first method, it will be very tedious to manipulate content within the page. Â Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338548 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 These are purely static pages. To make them dynamic, you need to use PHP to display the question and any or all answers it has in the "database". Following is sudo code: Â <html> <head> <title>Blah</tilte> <link rel = "stylesheet" style = "text/css" href = "blah.css" /> <script type = "text/javascript" src = "blah.js"></script> </head> <body> <div> <?php //fetch question with the id in the URL from the database and display here using PHP/MySQL ?> </div> <?php //fetch all answers to the question from the database for the selected question. //loop on each answer ?> <div id = "answer<?php echo id from current record ?>"><?php echo answer from current record ?></div> <?php //end loop ?> </body> </html> Â This way the you dont have to touch the page on the server everytime an answer is submitted. Â On submission of answer you insert the record in the database with appropriate questions id. Â when you refresh this page you will see the new answer displayed. Â I could write the entire code for you, but i think you need to read and understand the server side scripting basics first. Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338574 Share on other sites More sharing options...
Cupidvogel Posted April 18, 2012 Author Share Posted April 18, 2012 Exactly. That is what I was trying to say, so the original page with just the question remains untouched, subsequent answers are stored in the database, a page request invokes a script that takes a copy of that page, adds the answers from the database, and serves them, right? Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338578 Share on other sites More sharing options...
Cupidvogel Posted April 18, 2012 Author Share Posted April 18, 2012 Wait a minute, are you saying that even the question should exist in the database? If that is so, a request to the page fetches the question too from database, where does the page www.blah.html exist? Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338579 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 If you put it that way, yes PHP interpreter reads the server page, interprets the PHP code inside it, puts the answers in places and outputs a new HTML page... !!! Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338580 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 blah.html resides in the web directory of your server, where your URL is pointing to. Â It will have exactly what i mentioned in the first post. Â Yes, it is good idea to keep the question as well in the database so you dont have to prepare a static page for each question. The same page will work for multiple questions and multiple answers. We need it to be "dynamic" right? Â To identify what question is to be displayed, it will depend on user input either by form or URL Â www.blah.com/blah.php?id=12 Â This tells blah.php to fetch question with id = 12 from the database and its respective answer and display it to the user. Â www.blah.com/blah.php?id=13 will display question 13 and its answers and so on. Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338583 Share on other sites More sharing options...
Cupidvogel Posted April 18, 2012 Author Share Posted April 18, 2012 There is a technique called mod_rewrite which enables URL-rewriting. So the server may send the page blah.php?id=1 as blah/question_statement. But can the server reverse engineer it, that is, if the user requests blah/question_statement, can the server identify that it is actually required to serve blah.php?id=1 ??? Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338585 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 yes. Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338587 Share on other sites More sharing options...
Cupidvogel Posted April 18, 2012 Author Share Posted April 18, 2012 Cool. So let's consider Amazon, you do a search for an item, it returns a number of items. Each item is actually a link to an item page, which contains product info, user reviews, cost details, photos, etc. So basically all such pages use a general template and insert data retrieved from database? The links, which the search returns, are actually referring to this "virtual pages"? Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338589 Share on other sites More sharing options...
KevinM1 Posted April 18, 2012 Share Posted April 18, 2012 Cool. So let's consider Amazon, you do a search for an item, it returns a number of items. Each item is actually a link to an item page, which contains product info, user reviews, cost details, photos, etc. So basically all such pages use a general template and insert data retrieved from database? The links, which the search returns, are actually referring to this "virtual pages"?  Yup. One template can be used for an infinite amount of items. That's the power of dynamic sites. The same basic principle works for blogs, review sites, forums (including this one), and just about anything else you can think of. Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338592 Share on other sites More sharing options...
Cupidvogel Posted April 18, 2012 Author Share Posted April 18, 2012 Wow. Can't wait to master PHP. Mastered Perl before, hoping that it will help me in understanding server-side scripting. Perl is the awesomest language of all, but when it comes to server side scripting, I think PHP wins comfortably. Perl requires lots of stuff to do what PHP can do seamlessly. Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338593 Share on other sites More sharing options...
Psycho Posted April 18, 2012 Share Posted April 18, 2012 Cool. So let's consider Amazon, you do a search for an item, it returns a number of items. Each item is actually a link to an item page, which contains product info, user reviews, cost details, photos, etc. So basically all such pages use a general template and insert data retrieved from database? The links, which the search returns, are actually referring to this "virtual pages"? Â I don't mean to be rude (well maybe a little bit) but isn't that exactly the same as the example I provided to you above for displaying user profiles? Â But, I want to reiterate that a "dynamic" page doesn't mean a single page such as a profile or a product page. You could have a single page such as "user.php" which could perform any number of tasks and outputs that are determined by the parameters sent (POST, GET, etc.). For example: Â users.php?action=list: provides a list of users users.php?action=show&id=23: displays the profile for user with the id 23 users.php?action=new: Displays the form to add a new user users.php (POST['action']='add'): Add a new user from the post data users.php?action=edit&id=23: Displays a form to edit user 23. Current values are already populated users.php (POST['action']='update'): Updates user 23 with the data sent in the post data users.php?action=delete&id=23: Provides a confirmation asking if you want to delete user 23 users.php?action=delete&id=23&confirm: Performs the delete of user 23 Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338616 Share on other sites More sharing options...
Cupidvogel Posted April 19, 2012 Author Share Posted April 19, 2012 I don't want to be rude either, (well maybe a little bit), but I am a newbie, and for full clarification I may end up not understanding everything the first time someone explains something, even if he thinks he has explained pretty lucidly, so I may ask the same question twice. If you are too busy to answer, well, nobody is forcing you, right? Quote Link to comment https://forums.phpfreaks.com/topic/261185-how-are-dynamic-pages-served-from-server/#findComment-1338655 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.