User Personalization with PHP: Beginning the Application / Page 2

User Personalization with PHP: Beginning the Application [con't]

The Template

The other script in the application is the application template. We will be using this template throughout the entire application, bevause firstly, it gives a very neat appearance to our application and secondly, it helps to keep the presentation layer well apart from the actual PHP code. It has the look and code shown in Figure 1).

The template design is very simple. Basically, the main part of the page contains an HTML table that hosts the heading of the page. There is also a section of the page titled main. This is the part in which the other scripts will execute and display their output.

The Database Connection Script

Finally, since we are going to use a database to get our information, we need to store our database connection details. We will place them in a file called connect.php. The file also has other code, as you can see below:

Basically, we store the password, host and username information and run it through the MySQL functions to connect to the server. Other information that we stored here are functions that we are going to use in some of the scripts. The ones that are of particular importance is the checkurl and checkemail functions. These will help us in validating the e-mail address and URLs that the user will store in the database.

The Functions Explained

Both of the functions use regular expressions to match a pattern. For example, we know that an e-mail address has an 'at' (@) symbol that separates the front part of the address from the second part, and that the second part contains a period (.). With that in mind, we use the following pattern to match the given e-mail address:

Matching a URL is a little bit more complicated, since a it can start with www., http://, ftp://, https, and other protocol identifiers. To match it we need to make provision for all these possibilities:

The functions below return "true" if the pattern is matched, and "false" if the pattern is not matched:

Conclusion

That's it for this first section. In the next installment, we will look a bit more at the functions that are included in this file as well as other functions that we will be using throughout the application.