Jump to content

Recommended Posts

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?

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.

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?

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.

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.

 

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.

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?

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.

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 ???

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"?

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.

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.

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.