Currently Browsing: Tutorials

Tutorial: Object-Oriented PHP for Beginners

For many PHP programmers, object-oriented programming is a frightening concept, full of complicated syntax and other roadblocks. In this tutotial you’ll learn the concepts behind object-oriented programming (OOP), a style of coding in which related actions are grouped into classes to aid in creating more-compact, effective code.

Understanding Object-Oriented Programming

Object-oriented programming is a style of coding that allows developers to group similar tasks into classes. This helps keep code following the tenet “don’t repeat yourself” (DRY) and easy-to-maintain.

“Object-oriented programming is a style of coding that allows developers to group similar tasks into classes.”

(more…)

Tutorial: Create a PHP/MySQL Powered Forum from Scratch

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!

Step 1: Creating Database Tables

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.

Users

  • Categories
  • Topics
  • Posts

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.

(more…)

Tutorial: Simple Techniques to Lock Down your Website

One crucial part of PHP development practice is always keeping in mind that security is not something you can simply buy off the shelf at your local convenient store. Ensuring the security of your web applications is a process, which over time, needs to be constantly evaluated, monitored, and hardened.

Introduction

While the use of filters and validating data is one part of the security process, a web developer should be aware that Randomization, Obfuscation, and Cryptography in PHP can make a difference in the security of web applications. This tutorial will guide you through some simple techniques at creating and using random or unique values within your web applications, taking a look and applying some general obfuscation techniques, and looking deeper into the science of Cryptology and it’s use within PHP.

What you Will Learn

  • How to generate random values with PHP
  • Generating random Passwords
  • Salting Passwords and Authenticating The User
  • Obfuscation in PHP, an Overview
  • Cryptography in PHP and it’s Applications

(more…)

Tutorial: Creating a Crypter Class with PHP

In this article I will explain how to create a PHP Class that will encrypt and decrypt any data with a given password. It is object programmed and uses existing PHP algorithms.

Introduction

Think about what we might need a class like this for? We want to encrypt important data with a password for security reasons. We also want, as already mentioned, to be able to decrypt that data when necessary. Why should you use symmetric algorithms? It’s easy; when you’re offering a password sent via email or something like that, you need the password to be sent in plaintext. The hash algorithms are not reversible. Once you have hashed a string you can’t decipher the original text from the hash.

Maybe you have already heard of MD5? It’s not really the best option anymore because it tends to be unsafe. There are databases around the web – that I don’t want to mention – that can be used to retrieve the plaintext from a hash simply by typing in the hash into a search box. So you should use something like SHA which was developed by the NSA (National Security Agency). SHA is the abbreviation for Secure Hash Algorithm and is one of the most secure hash algorithms. There are some others as well, such as WHIRLPOOL, PANAMA and RIPEMD, but SHA is currently the secure standard for hashes and is used in numerous applications.

(more…)

Tutorial: 9 Useful PHP Functions and Features You Need to Know

Even after using PHP for years, we stumble upon functions and features that we did not know about. Some of these can be very useful, yet underused. Not all of us have read the manual and the function reference from cover to cover!

1. Functions with Arbitrary Number of Arguments

You may already know that PHP allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number of function arguments.

First, here is an example with just optional arguments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// function with 2 optional arguments
function foo($arg1 = '', $arg2 = '') {
 
	echo "arg1: $arg1\n";
	echo "arg2: $arg2\n";
 
}
 
foo('hello','world');
/* prints:
arg1: hello
arg2: world
*/
 
foo();
/* prints:
arg1:
arg2:
*/

Now, let’s see how we can build a function that accepts any number of arguments. This time we are going to utilize func_get_args():

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
// yes, the argument list can be empty
function foo() {
 
	// returns an array of all passed arguments
	$args = func_get_args();
 
	foreach ($args as $k => $v) {
		echo "arg".($k+1).": $v\n";
	}
 
}
 
foo();
/* prints nothing */
 
foo('hello');
/* prints
arg1: hello
*/
 
foo('hello', 'world', 'again');
/* prints
arg1: hello
arg2: world
arg3: again
*/

(more…)

Page 1 of 3123