functions.php
Let’s jump right into the project, starting with Example 21-1, functions.php, the include file of main functions. This file contains a little more than just the functions, though, because I have added the database login details here instead of using yet another separate file.
The first half-dozen lines of code define the host, database name,
username, and password of the database to use. It doesn’t matter what you
call the database, as long as it already exists (see Chapter 8 for how to create a new database). Also
make sure to correctly assign a MySQL username and password to $dbuser and $dbpass. With correct values, the subsequent two
lines will open a connection to MySQL and select the database. The last of
the initial instructions sets the name of the social networking site by
assigning the value “Robin’s Nest” to the variable $appname. If you want to change the name, here’s
the place to do so.
The Functions
The project uses five main functions:
createTableChecks whether a table already exists and, if not, creates it.
queryMysqlIssues a query to MySQL, outputting an error message if it fails.
destroySessionDestroys a PHP session and clears its data to log users out.
sanitizeStringRemoves potentially malicious code or tags from user input.
showProfileDisplays a user’s image and “about me” message, if he has one.
All of these should be obvious in their action to you by now, with
the possible exception of showProfile, which looks for an image of the
name <user>.jpg (where ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access