PHP Trainee Tutorials Coding in Plain English en-us Sun, 09 Apr 2017 12:57:36 +0000 Sun, 09 Apr 2017 12:57:36 +0000 Custom Laravel Blog (Richard Hung) (Richard Hung) HTTP Authentication There are numerous ways to protect your website content from unauthorized access, depending on your situation. If you have an API, you will authenticate through tokens. If you need to protect non-programming files like images or documents, you will need HTTP authentication using .htaccess file. If you have a registration system, you'll want a database and a login form on your website. Sun, 09 Apr 2017 12:57:36 +0000 PHP Extending and Importing Functions into Classes Using Traits Today we'll talk more about classes. Before we begin, make sure you read up on PHP Class basics. What are traits? In the abovementioned tutorial, I explained what classes are, and how to create an umbrella class and have multiple children that extends from it. So what if you need a class that imports functions from multiple sources? The obvious (but incorrect) way to extend classes Your first attempt after running into this issue is probably to just include or require files with methods into your classes. Sun, 01 May 2016 00:16:59 +0000 PHP JavaScript Module Loaders Web "apps" are slowly becoming the norm on the internet. Websites are not just for show anymore and developers have starting adding a lot of interactivity; you can watch movies, play games, voice and video chat. You can do on your browser what used to require an actual program. What is a module? Short Version: It's a plugin. Mon, 28 Sep 2015 02:39:57 +0000 JavaScript PHP classes and Object Oriented Programming Hello everybody and welcome back to PHP Trainee. Today we will talk about Object Oriented Programming. These words have scared a lot of new developers but it's very easy once you start to use it. Before we start, be sure to read up on Custom PHP Functions which this lesson builds upon. What is Object Oriented Programming? Object Oriented Programming (OOP) is a method of programming where developers group similar functions together. Thu, 04 Jun 2015 01:47:16 +0000 PHP HTML5 Storage using JavaScript localStorage and sessionStorage Hello everybody and welcome back to PHP Trainee. Today you will learn how to pass data between pages using JavaScript. JavaScript was originally created for the purpose of interactivity on websites. First rule of JavaScript they taught me in school was to make sure the website works fine without JavaScript. Sun, 11 Jan 2015 01:10:59 +0000 JavaScript JavaScript Event Bubbling and Return False Hello everybody and welcome back to PHP Trainee. Today's tutorial is about JavaScript's event triggers with Return False. $('a').click(function() { // Do stuff on click // Prevent browser from going to actual link return false; }); I'm sure you've seen the return false statement a lot when you do JavaScript. Mon, 01 Sep 2014 00:41:35 +0000 JavaScript AJAX Content Have you ever been to a website that offers "real-time" content updates? Google Maps only loads the part of the map you're looking at so you don't load the entire world. Twitter notifies you of new tweets as you're reading. Facebook has a chatroom that updates witout refreshing the page. They are using AJAX to reload content without refreshing the page. Thu, 27 Feb 2014 23:16:00 +0000 JavaScript Protect files using htaccess and htpasswd, and display using PHP Today's tutorial will be on how to serve and protect your files with PHP. Your server can only process code from PHP files so what about other kinds of files? Documents, PDFs, images and other files are all served as-is by the server. Anybody with the correct URL can get these files. Securing your files First step of is to secure the files on your server. Mon, 09 Dec 2013 23:38:57 +0000 PHP Responsive Web Design Today's tutorial will be on making your website work on all resolutions while still having control over your layout. A lot of users are using their phones to surf the web. What is responsive web design? Responsive websites are websites with different CSS rules for different situations. You can change the width, height, margin, or any other CSS property. Sat, 28 Sep 2013 23:35:31 +0000 HTML/CSS RSS Parse This tutorial will show you how to display a RSS feed on your website. What is a RSS feed? RSS stands for Rich Site Summary. It's usually a list of content that would be updated regularly. Users can grab the content without having to visit your site. Grab the Data RSS feeds come as XML files. Tue, 16 Jul 2013 20:51:40 +0000 PHP PDO Insert Update Delete This time you will learn how to insert, update and delete rows using PDO. Prepared statements quote and escape strings for you so there's no worry about SQL injections. Insert a new row To insert a row, you need to have all the data first. You can use PHP variables to fill in data. When you insert the data, the order has to be the same. Tue, 11 Jun 2013 01:53:43 +0000 MySQL PHP Data Objects Recently, I changed this site from MySQL to PDO, and you should do the same. MySQL is depreciated and will be removed from PHP in the near future. PDO stands for PHP Data Objects and it uses Object Oriented Protocol to connect to databases. PDO vs MySQLi MySQLi is useful and easy to learn, but once you master PDO, you will never want to go back. Fri, 07 Jun 2013 00:43:46 +0000 MySQL Table Highlight Today's tutorial shows how to create a plugin to highlight table data. Code Copy this code into a JavaScript file and include it in your page, after jQuery. (function($) { $.fn.highlight = function() { return this.each(function() { var table = $(this); var cell = table.find('td'); cell. Wed, 15 May 2013 23:01:09 +0000 JavaScript Scrolling Sticky This tutorial will show you how to create an element that will scroll with the browser after a certain amount of scrolling. CSS First of all, we need to make a class with a fixed position. A fixed position means that element will always scroll with the browser. .sticky { position:fixed; top:0; } JavaScript The above code works if you want that element to always scroll, but sometimes you want to stick after a certain amount. Fri, 19 Apr 2013 23:14:03 +0000 JavaScript Creating a jQuery Plugin Today's tutorial will be how to create your own jQuery plugin. What can you do in a plugin? Anything you can normally do with JavaScript. Why create a plugin when I have custom functions? You can put a plugin into a giant function, but it gets really messy. It's like classes in PHP, or good folder structure with your files on your computer; it's just more organized. Thu, 07 Mar 2013 22:51:04 +0000 JavaScript Backup MySQL database Today's tutorial will be how to back up your database. You can create a backup with phpMyAdmin or you can use PHP. This tutorial will show you how to use PHP. How do you back up? All you have to do is create a file using this code. Change the database information and run this file on your server. Fri, 15 Feb 2013 21:00:33 +0000 MySQL Z-index Today's tutorial will be how to use the z-index property in CSS. This property has baffled a lot of designers and developers, myself included, but it's very easy to use once you get used to it. What is z-index? Z-index is a CSS property that allows positioning of elements on top of each other. The z-index property only accepts numbers. Wed, 12 Dec 2012 13:16:55 +0000 HTML/CSS CSS Selectors Today's tutorial will be how to select different elements with CSS. CSS Selectors You can mix and match these to determin what div Selects all div tags div#wrapper Selects all div tags with "wrapper" in the id attribute. div.wrapper Selects all div tags with "wrapper" in the class attribute. Sun, 23 Sep 2012 11:01:53 +0000 HTML/CSS Random jQuery Animation This builds upon the last tutorial with jQuery functions. Today you can learn how to randomize numbers, animate object properties, and loop animations. How does it work? (function($) { $.fn.myAnim = function() { // Loop through objects this.each(function() { // Get three random numbers var x = Math. Sun, 09 Sep 2012 02:03:21 +0000 JavaScript Custom jQuery Function Today's tutorial will be about how to create your function in jQuery. I know there are a lot of plugins out there but there's always one feature that you need and they don't have. Today, you make your own custom function. Creating your first function (function($) { $.fn.myfunc = function() { // Loop through objects this. Sat, 11 Aug 2012 15:23:45 +0000 JavaScript Drop-Down Menus This tutorial will teach you how to create a nested drop-down menu. There are two ways to do this; you can use JavaScript or CSS. This tutorial will use the CSS method because it is easier to create. HTML Code For the HTML, all you need is a nested list. Home About Us Products Locations Contact Us Services Economy First Class Business Cargo Hold Blog CSS Code The CSS code is very simple. Mon, 04 Jun 2012 22:28:13 +0000 HTML/CSS Drupal Themes I haven't updated my tutorials in a while because I got a new job! They're very Drupal oriented so I've had to learn how to create themes. What files do you need? You only need two files for the theme to work. page.tpl.php and a *.info file. The filename of the *.info should be the same name as your theme. Fri, 04 May 2012 20:33:48 +0000 PHP Comparison with == and === Today's lesson will be on the difference between multiple equal signs in your comparisons. One equal sign is assignment, two is content comparison, three is content and type comparison. Whats the difference between two equal signs and three? Let's do a simple test to find out. $string = '123'; $integer = 123; // Two equal signs comparison // Answer is that it does match if ($string == $integer) { echo 'Two equal signs match. Mon, 02 Apr 2012 14:59:56 +0000 PHP Filtering Content Today's tutorial will teach you how to filter strings. I'm a big supporter for freedom of speech but one man's joke is another man's insult. Sometimes you have to filter out offensive or dangerous content. How do you filter content? Well, you could do a str_replace for every word you want to filter, or you could put it all into an array. Sun, 11 Mar 2012 15:02:50 +0000 PHP Creating a WordPress Theme Today I'll teach you how to create a custom WordPress theme from scratch. Why would you do this when there are so many themes out there? Challenge, efficiency, or the fact that stuff that's mainstreamed for everybody isn't tailered for you. The Essential Files A WordPress theme requires a minimum of two files, but usually has at least five. Sun, 19 Feb 2012 00:59:19 +0000 PHP Uploading Files Today you will learn how to upload files to your website. What types of files can you upload? You can upload anything. Although it is highly recommended you filter your results with size and type to prevent unauthorized files. You should be careful with executable files as those could contain viruses and other malware. Mon, 30 Jan 2012 00:56:26 +0000 PHP Looping Files in a Folder Today you will learn how to use PHP to load files from a folder. If you have something like a bunch of images or documents in a folder that you just want to throw up on the page, this becomes really handy. How do I open a folder and loop files? // select folder $docs_directory = "folder"; // open folder $dir_handle = opendir($docs_directory) or die("There is an error with your folder"); // loop through files while ($file = readdir($dir_handle)){ echo 'File: '. Sun, 15 Jan 2012 20:50:54 +0000 PHP Encryption In a previous tutorial, we learned about hashing strings. Hashing works very well for strings like passwords where you don't need the data back in plain text. But what if you needed a way to get it back in the original format for things like credit card numbers? Remember that hashing is a one way street and there is no way to revert it back; so we use something else: encryption. Sun, 01 Jan 2012 14:05:49 +0000 PHP Writing to a file Today you will learn how to use PHP to write text to another file. This is very useful if you want to keep a log file on your site. How do I write a file? If there is no file currently there, a new file will be created. // location and name of the log file $logfile = 'log.txt'; // open the log file $log = fopen($logfile, 'w') or die('Error opening log file. Sun, 18 Dec 2011 23:55:41 +0000 PHP Reading another file Today you will learn how to display the contents of another file. How do I display a file? One simple line will do the trick. echo file_get_contents("file.txt"); What files can I read? Pretty much any plain text file. HTML, TXT and PHP will work fine. Does this process code? The server won't process the code but your browser will. Sat, 03 Dec 2011 14:15:27 +0000 PHP Custom PHP functions Today you will learn how to create your own custom PHP functions. Create your own function PHP reads from top to bottom. The actual function has to be above whatever you're calling. To create a custom function, you need to create a custom variable for the function to use; this variable will be whatever you send to the function. Sat, 19 Nov 2011 13:15:06 +0000 PHP Includes and Requires Today you will learn how to insert a file into PHP. This is a very useful function as sometimes you might want only one copy of a global feature. If you have a navigation bar, headers or footers that stay the same throughout your site, it is really useful to have it in one central location. Imagine having a big website with a million pages and your boss tells you to change the new copyright year. Sun, 06 Nov 2011 16:26:42 +0000 PHP Selecting a MySQL row Hello and welcome to PHP Trainee. Now that your database is set up, you will learn how to grab data from it. How do you select data from a MySQL row? After you access the database, you have to select and query your results. There are 3 fetch statements used by PHP to grab queries: mysql_fetch_assoc, mysql_fetch_row and mysql_fetch_array. Sat, 22 Oct 2011 13:11:45 +0000 MySQL Session Hijacking In my previous cookie and session tutorial, I explained that they were not secure in their basic state. Hackers love to take control of your security sessions. So what can we do to increase security? There are 3 common methods hackers use to steal sessions: prediction, fixation and capture. Sessions 101 Before I get into what session hacking is and how to stop it, lets get some basics down on how sessions work. Sat, 08 Oct 2011 17:40:36 +0000 PHP VeriSign, SSL and Certificates Allowing user interaction with your server is great. But it's also a hacker's favorite hunting ground for unsecured data. This lesson will be about how to stop hackers from stealing your data. No code today, it's just going to be a lecture. What is a man-in-the-middle attack? It's a peeping tom with binoculars. Sat, 24 Sep 2011 17:26:01 +0000 Computer Sessions and Cookies Today we will talk about sessions and cookies. These are essential to helping users login to sites like this. What are sessions? Sessions are pieces of data that is saved on the server. The server then creates a session identifier, also called session id, on the user's computer. It can be set either through the cookie or the URL and links back to the data on the server. Sat, 10 Sep 2011 15:13:38 +0000 PHP MySQL Injections Hi and welcome back to PHP Trainee. Today will be how to secure your database from injections. You already know what XSS and SMTP injections are. Now we have arrived at the most dangerous injection: SQL. Your database is your most valuable asset in your web server. You can attempt to hide your information in case of a security breach, but you shouldn't be in that situation in the first place. Sun, 28 Aug 2011 13:40:18 +0000 MySQL DoS and DDoS attacks With all the recent talks about hackers, I decided to make a lesson on a common network attack, the DoS aka denial of service. Developers are scared of it, network security can't stop it, users are pissed off by it, and website owners lose a ton of money because of it. What is this attack that has everybody with a website dreading the day they called to arms and go to war? What is DoS? First off, DoS is an attack, not a hack. Sat, 13 Aug 2011 15:45:49 +0000 Computer MySQL Inserting and Manipulating Rows Hello and welcome back to PHP Trainee! This time you will learn how to insert, update and delete rows. This is the last time I'm going to say this but every MySQL tutorial from now on is going to require: a MySQL database, a table and the connection information. Read the previous tutorials on what this stuff is. Sat, 30 Jul 2011 17:44:20 +0000 MySQL Creating New MySQL Tables with PHP Hello and welcome to another tutorial at PHP Trainee. Today's tutorial will build upon last week's tutorial on MySQL. This time we will learn how to create a table in PHP. You can do this in phpMyAdmin but sometimes it might be faster to code it out. Before you start First, you need to have a MySQL database. Sun, 17 Jul 2011 20:45:15 +0000 MySQL Database Basics This tutorial will focus on MySQL. MySQL is a database that is very popular with web servers and it works very well with PHP. What is a database? Let's get the basics down first. A database is a bunch of tables. If you ever used Microsoft Excel, it's basically the same thing. You have different columns and rows and every table has its own name. Mon, 04 Jul 2011 19:20:19 +0000 MySQL Encryption, hashes, and salts, oh my! Today's lesson will be on hashes, encryption, and salts. Encryption and hashes are often mixed up because most people who use these two words can't tell them apart. I'm going to teach you what they are and how they work together. What is encryption? Encryption is a process that turns data into an unreadable format that requires a key or password. Sun, 19 Jun 2011 13:38:32 +0000 Computer PHP Arrays Today you will learn about arrays. This will make it easier when you have multiple pieces of data that you need to process. What is an array? An array is like a list. It contains multiple pieces of data. $array = array('Michelangelo','Leonardo','Donatello','Raphael'); How do you call an array? You need to call it by the order in which it appears in the array. Sun, 05 Jun 2011 14:47:42 +0000 PHP Dates and Timestamps This week's lesson will be about dates. How do I store dates? The easiest way is to store dates in timestamp format. Timestamp is a number that has counted every second since 1970. Since it is just a number, it is a lot easier to manipulate the date format. How do I get the current timestamp and date? // Timestamp format $timestamp = time(); // Date format $date("M j Y, g:i a"); How do I convert timestamp into a valid date format? You need to tell it what format you want the date to be. Sun, 29 May 2011 12:36:08 +0000 PHP Sending data to your Email Today I will show you how to send emails with PHP. Do I have mail? First of all, you need to find out if your server allows you to send mail. This is between you and your server. Most paid servers have mail and most free servers do not. If you do not have this function, this tutorial is useless to you. Sat, 23 Apr 2011 23:01:04 +0000 PHP XSS injections Today will be how to secure your website from XSS injections. Injections are ways for hackers to break your website. Fortunately, they are easily prevented with a few lines of code. What are injections? Injections are when you allow users to submit anything to your site without filtering the data. Sun, 17 Apr 2011 15:11:26 +0000 PHP Hackers, Firewall, and Internet 101 Hi guys, I've got a special tutorial this week. A friend of mine asked me what is a firewall and how do they work? I decided it would be a nice idea to make it into a tutorial. Computers, hackers, firewalls and the internet. How do they work? The internet and your computer So how does your computer interact with the internet? Your computer uses ports to allow programs to go in and out. Sat, 09 Apr 2011 16:52:18 +0000 Computer Send Data with GET and POST Today's lesson builds upon last week's lesson on forms. Actually, this lesson should have been first to grab the data before validation. Oh well, no harm no foul. Anyways, there are two ways to send data through PHP: GET and POST. They are relatively simple to use. What is Post? POST is data sent through a form. Sat, 02 Apr 2011 20:50:40 +0000 PHP Validating Forms As our first project, I'll show how to validate a form. I'll use a simple contact form for this demonstration. Basic Form First let's create a form that allows our users to enter data. It will contain simple information like name, comment, email, and website. It's important that the file name is the same as the form's action. Sun, 27 Mar 2011 00:25:31 +0000 PHP Regular Expressions I'm going to show you how to match and replace different statements in variables. What are regular expression? Regular expressions are patterns that PHP recognizes. Common statements on the web all have the same patterns. URLs always start with http://______.___ or https://______.___ and emails look like ______@____. Sat, 19 Mar 2011 22:39:04 +0000 PHP Comparisons and Order of Operations Today's tutorial will be about how PHP compares things and in which order. IF statements are simple to understand but difficult to master. It's used to check the validity of a statement. If a statement is true, then do something. Comparison Operators So what can you check with the IF statement? =for integers. Sat, 12 Mar 2011 22:22:17 +0000 PHP PHP Basics This is a site to teach people the power of PHP. This is my first tutorial and I plan on updating the site once a week, probably on weekends when I have a bit of spare time. The first few tutorials might feel like a lecture but it'll pick up once we get the basics down. Before you start In order to code PHP, there are a few requirements. Sun, 06 Mar 2011 10:40:46 +0000 PHP