Book Image

PHP 5 E-commerce Development

By :
Book Image

PHP 5 E-commerce Development

By:

Overview of this book

<p>The popularity of online shopping has increased dramatically over the past few years. There are plenty of options available if you not are planning to build your own e-commerce solution but sometimes it's better to use your own solutions. It may be easy to find an e-commerce system but when it comes to extending it or using it you might come across a lot of difficulties.<br /><br />This book will show you how to create your own PHP framework that can be extended and used with ease, particularly for e-commerce sites. Using this framework you will be able to display and manage products, customize products, create wish-lists, make recommendations to customers based on previous purchases, send email notifications when certain products are in stock, rate the products online, and much more.<br /><br />This book helps you build a Model-View-Controller style framework, which is then used to put together an e-commerce application. The framework contains template management, database management, and user authentication management. With core functionality in place, e-commerce-focused features are gradually added to the framework including products, categories, customizable products with different variations and customer input, wish-lists, recommendations, the shopping basket, and a complete order process.<br />&nbsp;<br />At the end of the book, you will have an e-commerce architecture that will take you from viewing or searching for products, and adding them to your basket, through the checkout process and making payment for your order, to your order being dispatched. Focus is placed on flexibility, so that the framework can be extended as the needs of a particular store change, as illustrated by one of the appendices, which goes through the process of modifying the store to sell downloadable products, as well as physical ones.<br /><br />Supplementary information, such as how to market and promote an online store, as well as take regular backups and perform maintenance is also covered, ensuring you have every chance of success with you own e-commerce framework backed store.</p>
Table of Contents (23 chapters)
PHP 5 e-commerce Development
Credits
About the Author
About the Reviewers
Preface

Authentication reminders


One useful feature for our framework would be to allow our customers to easily reset their password or to send them notification of their username.

Help! I forgot my password!

When a customer forgets their password, we can't just e-mail them a copy, because passwords are stored as a hash in the database. We also can't just reset the password, as fraudulent requests for new passwords would become a nuisance for customers.

The solution to this is to generate a password reset key when a customer informs us that they have forgotten their password. We then e-mail the customer a link to a "reset password" page, with the reset key in the URL. The reset key is used to verify the customer resetting the password is the owner of that user account.

Our users table already has a suitable field for this, pwd_reset_key; all we need now is the code!

Generate the reset key, update the user record, and e-mail the customer

This section of code simply creates a reset key for the user, and e-mails it to the customer, as part of a special URL the customer can use to reset their password.

$email = $this->registry->getObject('db')->
sanitizeData( $_POST['email'] );
$sql = "SELECT * FROM users WHERE email='{$email}'";
$this->registry->getObject('db')->executeQuery( $sql );
if( $this->registry->getObject('db')->numRows() == 1 )
{
$changes = array();
$changes['pwd_reset_key'] = generatePasswordKey(8);
$this->registry->getObject('db')->updateRecords('users', $changes, "email='{$email}'");
// email the customer a link to // user/reset-password/userid-pwd_reset_key
}
function generatePasswordKey( $length = 8 )
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';
for ( $i = 0; $i < $length; $i++ )
{
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}

Reset the password

This code would be part of the "reset password" page (which is accessed using the "reset password" URL). This splits part of the URL to extract the user ID and the password reset key. It then updates the user's password, assuming their password and confirmation match and the reset key matches that of the user ID.

$data = explode('-', $urldata[2]);
$userid = intval( $data[0] );
$key = $data[1];
if( $_POST['new_password'] == $_POST['confirm_newpassword'] )
{
$pwd = md5( $_POST['new_password'] );
$sql = "SELECT * FROM users WHERE ID={$userid} AND pwd_reset_key='{$key}'";
$this->registry->getObject('db')->executeQuery( $sql );
if( $this->registry->getObject('db')->numRows() == 1 )
{
$changes = array();
$changes['password'] = $pwd;
$this->registry->getObject('db')-> updateRecords('users', $changes, "ID=" . $userid);
// e-mail customer confirmation?
}
}

Help! I forgot my username!

If a customer forgets their username, we will require them to enter their e-mail address into a reminder form. If they can't remember their e-mail address, there is little we can do automatically, but they could still get in contact and inform us of their delivery address or confirm some details from a recent order, should they need to.

$email = $this->registry->getObject('db')->
sanitizeData( $_POST['email'] );
$sql = "SELECT username FROM users WHERE email='{$email}'";
$this->registry->getObject('db')->executeQuery( $sql );
if( $this->registry->getObject('db')->numRows() > 0 )
{
$data = $this->registry->getObject('db')->getRows();
// send email to the customer, include their username
}