Generate HTML files with PHP to Reduce the Server Load

    November 9, 2004

PIM Team Case Study

How you can have a site with tons of visitors without to crash the server?


PIM Team Bulgaria had the task to create a quiz site able to hold an extreme server load.

The site is currently installed as beta version at


– Create a user friendly quiz editor

– Create static URLs for the quizes

– Users were able to put the quizes on their site. However this shouldnt cause big server load.


Creating database structure and the basic program logic was a game play for the experienced developers in PIM.

However we had to decide the best way to reduce the server load. After investigating together with the help of the customer we found the best solution – the quiz forms were going to be hardcoded.


We don’t need anything special except PHP and MySQL to achieve this task. Having the right permissions to write in the server folders its all about generating strings and saving HTML files.


As this article is not aboput creating a quiz site, we’ll point our attention only to these features which required reducing of the server load.

A quiz editor for creating and editing quizes

While the creating of quizes isnt likely to be done by all the users of the site all the time, we wouldnt need to worry about the server load. Not that we can hardcode this editor in HTML ever!

The editor allows usres to create questions with various number and type of answers on each quiz.

Each quiz also had various number of results which were generated depending of user’s answres.

Here we saw what we could use to reduce the server load:

Quizes themself

What is one quiz in the terms of a website? Its just a HTML form. However our users are creating the quizes so nobody could know what forms will they create. The ordinary solution is to read the database with questions, select possible answers and display them to the users.

But all of this is database queries. We can’t select questions and answers at once with JOIN, because both need to be ordered in the same way the user has put them in the editor.

This means at least one query for each question. Imagine a quiz with 30 questions taken from 100 users at the same time. This is 3000 queries at least. We don’t wan’t to do this.

So we were going to create pure HTML forms from the quizes at the time of creating. We also needed to update the quizes when users update them. So we add a button ‘activate’ for ‘sending’ the quiz online. Hitting this button actually takes the questions and answers from the database and creates HTML form. Below is a piece of code showing how easy this is:

//generate HTML for the quiz page

<head><title> - Quizzes and Personality
<link rel=stylesheet
<table align=center width=600>\n
<tr><td class=heading>$quiz[title] By <a target=_blank
<form method=post action=".SITE_URL."result.php>\n
<input type=hidden name=id value=$quiz[id]>\n";

foreach($questions as $question)

foreach($question[choices] as $choice)
$html.="<input class=radio type=radio
name=question".$question[id]." value=".$choice[id].">
$html.="<input class=radio type=checkbox
name=question".$question[id]."[] value=".$choice[id].">


$html.="<tr><td><input type=submit name=ok value=Go

Now we have to save this as HTML file:

//generate file name
$qfname=str_replace(" ","-",$quiz[title]);

//open file

And thats it? No. Quizes are not the only thing which could be optimized. What else?

Quiz Results

After the user take a quiz we have to calculate their results. Obviously this can’t be done without a script.

When calculate we had to show the right record (result) from the database as created by the user.

We had to select the result by id, to select the quiz and its detail, to check if the result has image associated and if so to show it. Do we need these calculations? Not really. And thats not all. The customer wanted to allow the quiz creators to have their result displayed on their websites or whereever they want.

Imagine some user having a website with 100 000 visits per day and every visit to get the info from quizzy server.

5 users like that and it could crash. We already had the solution – to create HTML files for each the result and put the same HTML in a textarea (yes, in the created HTML!) for the users to copy and put on their sites. Here is a piece of code to see how easy this is:

//save results
WHERE poll=$quiz[id]";

foreach($results as $result)

$head="<head><title> - Quizzes and Personality

$tryit="<tr><th align=center><a target=_blank
By <a target=_blank

$begin.="<table style='border:1px solid
black;font-family:verdana,hebar,arial;font-size:12px;' align=center

$anotherquiz="<p align=center
Another <a href=".SITE_URL.">Quizzy</a> Quiz!</p>";

$html="<tr><td align=center><h2

//select image
WHERE id=$result[image]";

$html.="<tr><td align=center><br>
<img src='".IMAGES_PATH."$image[file]'>

/* ........ */

Save this string in a file, update the quiz database so the calculator script know which HTML to show and all is done.


While site is still in beta testing, the results are already there. Excellent quiz site working really fast.

Static HTML pages allowing ALL the quizes and their results to be indexed by the search engines.

A site able to hold hundred thousands of users in a shared webhost. And it works fast!


– Dynamicly create static HTML files with PHP to reduce the server loads

– Allow users to use the generated HTMLs for their own

– Improve search engine optimisation with the static files

– Allow hundred thousands of users to visit your site without database server crash

Bobby Handzhiev is a senior developer in PIM Team Bulgaria