In this tutorial, we’re going to build a PHP/MySQL powered forum from scratch. This tutorial is perfect for getting used to basic PHP and database usage. Let’s dive right in!
It’s always a good idea to start with creating a good data model when building an application. Let’s describe our application in one sentence: We are going to make a forum which has users who create topics in various categories. Other users can post replies. As you can see, I highlighted a couple of nouns which represent our table names.
These three objects are related to each other, so we’ll process that in our table design. Take a look at the scheme below.

Looks pretty neat, huh? Every square is a database table. All the columns are listed in it and the lines between them represent the relationships. I’ll explain them further, so it’s okay if it doesn’t make a lot of sense to you right now.
Today we will learn how to work with file operations using PHP. This is one of the most fundamental subjects of server side programming in general. Files are used in web applications of all sizes. So let’s learn how to read, write, create, move, copy, delete files and more.
We are going to start learning with these operations first, because they are very simple one liners, and they do not involve dealing with file pointers.
1 2 3 4 | $file = "test.txt"; $new_file = "test_copy.txt"; copy($file, $newfile); |
Push the limits of your PHP knowledge with this advanced tutorial. Implement techniques including object-oriented programming, regular expressions, and function currying to build a templating system from scratch.
The short answer? You don’t.
So why bother?
From the bottom of my geeky little heart, I believe that all developers should constantly be pushing themselves to learn new and/or difficult concepts. It’s what makes us smarter, keeps our work interesting, and makes our day-to-day workload seem like less of a burden (because, you know, we’re getting so much smarter).
To that end, rolling your own templating system gives you the opportunity to hone your PHP chops with brain-bending concepts such as currying and regular expressions. And, hey! You just might find that you could put a templating system to use in one of your future projects.
(more…)
In this tutorial I will be showing you how to make a simple login system consisting of a login page, register page, forgotten password page, email activation, logout page and finally a users online page. I made this tutorial to mainly target new-to-PHP developers, due to the fact when I started I noticed the lack in quantity of basic login systems. Therefore, I decided to make one myself giving high quality advice on how to make your first login system with a users online script!
We are going to create a very basic CSS stylesheet just to add a little bit of design and tidy up the way this login system looks. So too start off with open your text editor and we can begin making our styelsheet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | body { font-family: arial; font-size: 10pt; } table { font-size: 10pt; margin: 0 auto; } #border { border: 2px solid #999; background: #CCC; padding: 15px; margin: 0 auto; width: 300px; } |
Save this file as style.css so we can link back to it whenever we need to. There we have our simple stylesheet! Now we can begin making our pages without having to worry too much about making them look reasonably good.
Today I am going to show you how to make a simple user counter with total and unique hit statistics. This is just a simple script tutorials so you will be able to build upon it.
Let set up our database structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < ?php // This is how your DB will be setup. /* CREATE TABLE IF NOT EXISTS `visitors` ( `visitor_id` int(11) NOT NULL auto_increment, `name` varchar(225) NOT NULL default '', `value` varchar(225) NOT NULL default '', PRIMARY KEY (`visitor_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; INSERT INTO `visitors` (`visitor_id`, `name`, `value`) VALUES (1, 'ip', ''), (2, 'hits', '0,0'); */ ?> |
Now to call the database in conf.inc.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | < ?php $db_user = ""; // Username $db_pass = ""; // Password $db_database = ""; // Database Name $db_host = ""; // Server Hostname $db_connect = mysql_connect ($db_host, $db_user, $db_pass); $db_select = mysql_select_db ($db_database); // Code to pull the results from the db. $q = mysql_query("SELECT * FROM `visitors` WHERE `name` = 'hits'"); // Finds the row with the numbers $visitor = mysql_fetch_array($q); $visitor = split(",", $visitor['value']); // Splits the query result into an array. define('SITE_HITS', $visitor['0']); // Defines a constant for all hits define('SITE_UNI_HITS', $visitor['1']); // Defines a constant for unique hits function visitor($ip) { // Function that will be put on main page. $q_ip = mysql_query("SELECT * FROM `visitors` WHERE `name` = 'ip'") or die(mysql_error()); // Finds the info for all of the ip's $r_ip = mysql_fetch_array($q_ip); // Fetches the ip array we will use later. $array_ip = split("-", $r_ip['value']); // Splits the results and turns it into an array of all the ips $q_hit = mysql_query("SELECT * FROM `visitors` WHERE `name` = 'hits'") or die(mysql_error()); // Finds the info for the hits $r_hit = mysql_fetch_array($q_hit); $hit = split(",", $r_hit['value']); // Splits the query results into an array. $hits = $hit['0']; // The total hits $uni = $hit['1']; // The unique hits if (in_array($ip, $array_ip)) { // If the users ip is in the array we defined earlier $hits++; // Adds a hit, but not a unique hit } else { $hits++; // Adds both a unique and hit because he is a new user. $uni++; $ip = $r_ip['value'] . $ip; // Adds the new ip to the ips already in the db mysql_query("UPDATE `visitors` SET `value` = '$ip' WHERE `name` = 'ip'"); // Updates the table } $hits = $hits . "," . $uni; // Combines the hits and unique hits mysql_query("UPDATE `visitors` SET `value` = '$hits' WHERE `name` = 'hits'"); // Updates the table } ?> |
Breakdown:
First we set the db info.
Next we define the hit/unique constants to display it on the stats page.
After we create the function visitors to add the users ip and hits into the db.
Now the code you would put in the index.php or the page you want the stats for.
1 2 3 4 5 | < ?php include("conf.inc.php"); visitor($_SERVER["REMOTE_ADDR"]); // Use the function we defined earlier with the users ip. echo "Main page"; ?> |
Breakdown:
First we include the conf.inc.php for the db info and the functions.
Next we initiate the function visitors with the users ip.
(more…)