Archive for the ‘Examples’ Category

PHP - Sending an HTML Message

Friday, February 16th, 2007

Sending an HTML message is very similar to sending a plain text message.
The important difference is the message header.

<?php
$Recepient = "somebody@somewhere";
$MsgSubject = "Message subject";
// You must set sender through message header
$MsgHeader = "From: Sender name<sender@server>\n";
// You need these two lines
$MsgHeader .= "MIME-Version: 1.0\n";
$MsgHeader .= "Content-type: text/html; charset=us-ascii\n";
// Message body is HTML
$MsgBody = "
<html>
<head>
  <title>HTML message</title>
</head>
<body>
  <h2>Congratulation!</h2>
  <p>You have just learned how to send a HTML message</p>
</body>
</html>"
;
mail($Recepient, $MsgSubject, $MsgBody, $MsgHeader);
?>

PHP - Inside a class

Friday, February 16th, 2007

Classes consist of variables and functions. These elements have a scope, they can be visible or not outside of a class. Scope of class members can be public, protected or private.

When member is declared public, it is visible outside of a class and can be used anywhere where object of that class can be used.

When declared protected, it is visible only inside a class and its subclasses.

When declared private, it is visible only inside a class which declares that member.

We declare class member as public, protected or private by putting one of the keywords public, protected or private in front of member declaration. If we omit scope identifier, member is considered public.
(more…)

PHP - Constructor and Destructor

Friday, February 16th, 2007

Every class has two special functions, constructor and destructor. Even if you do not explicitly declare and define them, they exist (they are empty).

Constructor is a function which is called right after you create a new object. This makes constructor suitable for any object initialization you need.

Destructor is a function which is called right after you release an object. Releasing object means that you do not need it or use it anymore. This makes destructor suitable for any final actions you want to perform.

Here we come across first difference between object oriented code in PHP4 and PHP5.
(more…)

PHP - Simple content management system

Friday, February 9th, 2007

PHP LogoContent management system, or CMS, is a very common web application type. The main idea is to enable site administrator to easily change the content of a web site, without need to know anything about web design and web development process. Usually, CMS provides easy-to-use interface for changing the web site content.

We will see a very simple CMS in this example. It consists of 4 files and an sql script for creating database tables needed.
(more…)

PHP - Implementing Secure Login with PHP, JavaScript, and Sessions (without SSL)

Friday, February 9th, 2007

HTTPS (HTTP over SSL) is the most common mechanism on the internet used to ensure server authenticity and provide data privacy. Unfortunately, SSL is often too complex and prohibitively expensive for many small-scale sites where all that is needed is a secure authentication mechanism.

Challenge-Response Authentication Mechanism

The biggest drawback of a regular non-SSL login is that the password is sent in clear-text, which can be easily sniffed by a potential attacker. But if the password were never to leave the client, there would be no chance of capturing it.

We can use a cryptographically-secure one-way hash function, such as MD5, to convert the password into a 128-bit hash number, which we could send instead. Even though this method would preserve the secrecy of the password, it would fail under a replay attack where an attacker could log in using the sniffed hash.
(more…)

PHP - Inheritance

Friday, February 9th, 2007

Inheritance is a mechanism of extending an existing class.
By inheriting a class, we create a new class with all functionality of that existing class, and we can add new members to the new class. This way, we can extend existing class without modifying its code.

When we inherit one class from another we say that inherited class is a subclass, because it is created from another class. Term successor can be also used when referring to to the subclass. Class from which we inherit is called a parent class, or predecessor. In our examples we will use terms subclass and parent class.

(more…)

PHP - Object oriented code - basics

Friday, February 9th, 2007

We will be more oriented on objects in PHP5 than PHP4, because there are major changes in object model and more features in PHP5. If you want to use objects in PHP, I would recommend you to switch to PHP5.

In object oriented programming, you have something called class and something called object. A class could be described as a blueprint for an object, description of an object, a template which is used to build an object. When we build an object from a class, we say we have created object of that class. For example, if we use class Animal to build object Cat, then object Cat is of Animal class.

(more…)

PHP - Basic architecture - One for one

Thursday, February 8th, 2007

Here we will describe "One on one" PHP application architecture.

In this architecture, you have one file for every function you have in your application. Every file contains code to perform only one function and displays results.

Example is same as in "One for all" architecture, but notice the difference in the link code. Also, notice the link in the address bar when you click on link. In "One for all" scripts are executed in context of index.php (address is index.php?cmd=displayserverinfo). In this example, they are executed in their own context (address is cmd1.php). We call the scripts directly, we do not include them.
This approach is good because you can better organize your code (in more files), the code is more "replaceable", but you may have a problem with common functions. For example, if you have a function you need to call in all your scripts (usually security functions, or functions to start displaying a html page), you need to call them in all of these files.

(more…)

PHP - Basic architecture - One for all

Thursday, February 8th, 2007

We have two architectures we can use to build our PHP application. We will refer to them as "One for all" and "One on one". There are pros and cons for both of them, so you will pick the one that best suits your needs.
Here we will describe "One for all" PHP application architecture.

(more…)

PHP - Including and requiring other files

Thursday, February 8th, 2007

While writing a PHP application (like any other application), you will, very often, find yourself splitting your code into several files. You may do this for a better code organization, ability to use your code elsewhere, to separate graphical presentation from data manipulation, etc…

Sometimes, code in two separate files depends on each other. Or you may have some useful code in a separate file and want to reuse it. Or you just want to organize your application in that way. In other words, you may need to somehow connect two files, so you can use code from both of them. When I say connect, I do not mean physically connect, like append data from one file to another. I'm thinking about some kind of logical connecting, which will enable us to see these two files as one. We are enabled to use code from both files, but they are staying two separate files at the file system.

PHP provides four functions for logical connecting of files.

(more…)

PHP - Image resizing

Wednesday, February 7th, 2007

You will need image resizing if you are writing a web gallery. Your image can be resized to fit a certain dimensions, or to make a thumbnail. This short and handy script can help you to do that. Script is using GD library functions to manipulate images. You can make your images larger or smaller.

In the example, we will work on a file from a filesystem, but, you can modify script to work with file from database. To resize just uploaded file, you can pass $_FILE['filename']['tmp_name'] as a input filename.

Function parameters are input filename, output filename, new width, new height and keepAspectRatio, respectively.

Last parameter enables you to keep ratio of width and height of an image. This will prevent image deformation if you do not give accurate new dimensions. It is ON by default.

(more…)

PHP - Sending a simple text message

Tuesday, February 6th, 2007

This example shows how to send a simple text email message using PHP.

<?php
$Recepient = "somebody@somewhere";
$MsgSubject = "Message subject";
// You must set sender through message header
$MsgHeader = "From: Sender name<sender@server>\n";
$MsgBody = "Message body.";
mail($Recepient, $MsgSubject, $MsgBody, $MsgHeader);
?>

PHP - Email address validation

Tuesday, February 6th, 2007

User has submitted web form and you have to send him a confirmation e-mail.
Before you try to send a message, you have to be sure that the given text is valid e-mail address.
Function uses regular expression to check if given text comply with e-mail address format.

<?php
function ValidEmailAddress($address) {
  $regex = '/^[A-z0-9][w.-]*@[A-z0-9][w-.]+.[A-z0-9]{2,6}$/\';
  return (preg_match($regex, $address));
}
?>

PHP - Superglobal variables

Monday, February 5th, 2007

Superglobal variables hold values defined by the server, and defined by a HTTP request.
You can use these values to change the behavior of your script.
We will display all values in superglobal variables, and describe them.

$_GET
Values sent to script via HTTP GET request.
Like this script.php?name1=value1&name2=value2

$_POST
Values sent to script via HTTP GET request.
This is usually data you send by submitting web forms.

$_COOKIE
Values sent via HTTP cookies.
Cookies are name/value pairs, saved at client computer.

$_FILES
With this variable you can handle files sent through web forms.

$_SERVER
Environment variables set by the web server.

$_ENV
Environment variables set by system.

$_REQUEST
This variable joins $_GET, $_POST and $_COOKIE variables. It is simple way to access all values provided to script by HTTP request.

$GLOBALS
This variable joins all superglobals and all variables available in global scope of the script.

(more…)

PHP - Usefull array functions

Monday, February 5th, 2007

After looking at string functions, let's look at arrays.
You will find yourself using array very often. In this example we will see just a few of many useful array functions. I will explain these functions in code comments.

(more…)