Sending Email with Attachment
The last variation that we will consider is email with attachments. To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email.
Sending HTML Email Using PHP
The next step is to examine how to send HTML email. However, some mail clients cannot understand HTML emails. Therefore it is best to send any HTML email using a multipart construction, where one part contains a plain-text version of the email and the other part is HTML. If your customers have HTML email turned off, they will still get a nice email, even if they don't get all of the HTML markup.
Sending a Simple Text Email
At first let's consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters. These parameters are, in order, the email address to send to, the subject of the email, the message to be sent, additional headers you want to include and finally an additional parameter to the Sendmail program. The mail() function returns True if the message is sent successfully and False otherwise.
Record locking in Web applications
Record locking is used for preventing simultaneous update of the same data and therefore avoiding inconsistent results. A locked record means it is not available for editing by other users. In this article we describe how to implement record locking in web applications.
Creating Word, Excel and CSV files with PHP
Browsing the World Wide Web you can find out various methods of creating files with PHP. In this article we demonstrate several ways to create Microsoft Word and Excel documents, and also CSV files using PHP.
Debugging PHP with Xdebug
The Xdebug is the extension for PHP that helps debugging PHP scripts by providing a lot of valuable debug information. The debug information includes stack traces and function traces in error messages, memory allocation and protection for infinite recursions.
Reading the "clean" text from PDF with PHP
Portable Document Format (PDF) is a file format created for the document exchange. Each PDF file encapsulates a complete description of a fixed-layout 2D document (and, with Acrobat 3D, embedded 3D documents) that includes the text, fonts, images, and 2D vector graphics which compose the documents.
PHP: Reading the "clean" text from RTF
Rich Text Format (often abbreviated as RTF), to surprise of many, is quite complex text data format. During its long history RTF bought a lot of add-ons that disturb the process of getting "clean" text. Let's try to solve that...
Reading the "clean" text from DOCX and ODT
In this article we will resolve the task of reading the “clean” text from the Office Open XML (more known as DOCX) andOpenDocument Format ODT using PHP. Note that we are not going to apply any third-party software.
Using Regular Expressions with PHP
Regular expressions are a powerful tool for examining and modifying text. Regular expressions themselves, with a general pattern notation almost like a mini programming language, allow you to describe and parse text. They enable you to search for patterns within a string, extracting matches flexibly and precisely. However, you should note that because regular expressions are more powerful, they are also slower than the more basic string functions. You should only use regular expressions if you have a particular need.
Create Thumbnail Images using PHP
This tutorial will describe how to create thumbnail images on the fly using PHP. Furthermore you will learn how to process a whole folder of images and create their thumbnails. Since this requires the GD library, you will need an installation of PHP with at least GD 2.0.1 enabled.
PHP: Export Database Schema as XML
Sometimes it can be useful to have a dump of the current database schema. The script below reads the schema from a MySQL database and outputs XML that describes the schema.
Get the Current Page URL Using PHP
Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can do that.
Passing JavaScript variables to PHP
JavaScript is mainly used as a client side scripting language, while PHP is a server side technology. Unlike Java or ASP.Net, PHP doesn't have tools to make it work client side. That is why you need to combine JavaScript and PHP scripts to develop powerful web-applications.
Create CAPTCHA Protection using PHP and AJAX
CAPTCHA is a simple test to determine if a user is a computer or a human. It is used to prevent spam abuse on the websites. So if you use CAPTCHA on your web site forms, this can help in stopping some bots and making life harder for other bots in accessing or using your forms.
Encrypt Passwords in the Database
If you are developing a password-protected web site, you have to make a decision about how to store user password information securely.
Blocking access to the login page after three unsuccessful login attempts
Sometimes you need to add an extra protection to password-protected website. This article explains how access to the login page can be restricted after three unsuccessful login attempts. This schema uses visitors IP address to store log attempts in the database and block access to login feature for 30 minutes after third unsuccessful attempt.
There are a number of reasons to restrict access. One reason is security. Quite often users try to guess login and password combination to get unauthorized access to the system. Another reason is extra load on server.
So let's start. At first you need to create a new table in your database to store information about login attempts from a certain computer. SQL script creating such table in MySQL Server will be the following. For other databases it will slightly differ.
CREATE TABLE `LoginAttempts` ( `IP` VARCHAR( 20 ) NOT NULL , `Attempts` INT NOT NULL , `LastLogin` DATETIME NOT NULL ) |
It is assumed that you have already had an authorization page. Otherwise you can create it using PHP, SSI, and similar languages. There are no major difficulties in writing this program (script).
Authorization page should work with two tables: one table where information about registered users is stored and the other one where unsuccessful login attempts are listed.
Before verifying entered data, system has to check if the user exceeded attempts limit or not. If in the LoginAttempts table there are more than two records correspondent to one IP address, then error message will appear saying that access is blocked for a certain period of time. You can set time period at your discretion. Depending on your security policy it can vary from 1 minute to 24 hours or more. In the following example access will be blocked for 30 minutes.
Before verifying entered data, system has to check if the user exceeded attempts limit or not. If in the LoginAttempts table there are more than two records correspondent to one IP address, then error message will appear saying that access is blocked for a certain period of time. You can set time period at your discretion. Depending on your security policy it can vary from 1 minute to 24 hours or more. In the following example access will be blocked for 30 minutes.
<?php function confirmIPAddress($value) { $q = "SELECT attempts, (CASE when lastlogin is not NULL and DATE_ADD(LastLogin, INTERVAL ".TIME_PERIOD. " MINUTE)>NOW() then 1 else 0 end) as Denied FROM ".TBL_ATTEMPTS." WHERE ip = '$value'"; $result = mysql_query($q, $this->connection); $data = mysql_fetch_array($result); //Verify that at least one login attempt is in database if (!$data) { return 0; } if ($data["attempts"] >= ATTEMPTS_NUMBER) { if($data["Denied"] == 1) { return 1; } else { $this->clearLoginAttempts($value); return 0; } } return 0; } function addLoginAttempt($value) { //Increase number of attempts. Set last login attempt if required. $q = "SELECT * FROM ".TBL_ATTEMPTS." WHERE ip = '$value'"; $result = mysql_query($q, $this->connection); $data = mysql_fetch_array($result); if($data) { $attempts = $data["attempts"]+1; if($attempts==3) { $q = "UPDATE ".TBL_ATTEMPTS." SET attempts=".$attempts.", lastlogin=NOW() WHERE ip = '$value'"; $result = mysql_query($q, $this->connection); } else { $q = "UPDATE ".TBL_ATTEMPTS." SET attempts=".$attempts." WHERE ip = '$value'"; $result = mysql_query($q, $this->connection); } } else { $q = "INSERT INTO ".TBL_ATTEMPTS." (attempts,IP,lastlogin) values (1, '$value', NOW())"; $result = mysql_query($q, $this->connection); } } function clearLoginAttempts($value) { $q = "UPDATE ".TBL_ATTEMPTS." SET attempts = 0 WHERE ip = '$value'"; return mysql_query($q, $this->connection); } ?> |
DSN and DSN-less connections
DSN stands for 'Data Source Name'. It is an easy way to assign useful and easily rememberable names to data sources which may not be limited to databases alone. If you do not know how to set up a system DSN read our tutorial How to set up a system DSN.
Connect to MS SQL Server database
Creating New Directories
Creating new directories in PHP is accomplished using the mkdir() function, which takes two parameters. These parameters are, in order, a directory name you want to create and a permission mode for a new directory, which must be an octal number. The mode parameter is optional and has an effect only on Unix systems.
Deleting the Directory and Its Contents
PHP has the rmdir( ) function that takes a directory name as its only parameter and will remove the specified directory from the file system, if the process running your script has the right to do so. However, the rmdir() function works only on empty directories. The example below deletes empty directory named "temporary":
PHP: Working with Directories - Reading the Contents of a Directory
As is necessary for any language, PHP has a complete set of directory support functions. PHP gives you a variety of functions to read and manipulate directories and directory entries. Like other file-related parts of PHP, the functions are similar to the C functions that accomplish the same tasks, with some simplifications. This tutorial describes how PHP handles directories. You will look at how to create, remove, and read them.
Secure File Upload with PHP
PHP makes uploading files easy. You can upload any type of file to your Web server. But with ease comes danger and you should be careful when allowing file uploads.
In spite of security issues that should be addressed before enabling file uploads, the actual mechanisms to allow this are straight forward. In this tutorial we will consider how to upload files to some directory on your Web server. We will also discuss security issues concerned with the file uploading.
Building a bar chart
In the example below, using graphic functions we will build a bar chart based on the values stored in MySQL database. In our case, values represent poll results.
Dynamic Image Generation
Creating images on the fly can be a very useful skill. PHP has some built-in image generation functions, further more, to generate new images or edit existing images on the fly using PHP, we need to have the GD library installed. In this tutorial we will show you how to get quite interesting and useful effects using image handling functions. We will review two practical tasks: creating security images (captcha) on the fly and building a bar chart using numerical values retrieved from MySQL database.
Using Cookies in PHP
A cookie is a message given to a Web browser by a Web server. The browser stores the message in a small text file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, the cookie is sent back to the server too.
Processing the Form Data ( PHP Code )
we are going to create our PHP file that will process the data. When you submit your HTML form PHP automatically populates two superglobal arrays, $_GET and $_POST, with all the values sent as GET or POST data, respectively. Therefore, a form input called 'Name' that was sent via POST, would be stored as $_POST['Name'].
Form Processing with PHP
One of the best features of PHP is possibility to respond to user queries or data submitted from HTML forms. You can process information gathered by an HTML form and use PHP code to make decisions based off this information to create dynamic web pages. In this tutorial we will show how to create an HTML form and process the data.

