Home arrow PHP arrow Page 2 - Using the PHP Crypt Function

A Practical Example - PHP

The PHP crypt function is a one-way encryption function that lets you confirm that an entered password matches a stored encrypted one -- without having to decrypt anything. Chris Root explains how it works.

TABLE OF CONTENTS:
  1. Using the PHP Crypt Function
  2. A Practical Example
  3. Login
  4. A Few Words About Storage and Security
By: Chris Root
Rating: starstarstarstarstar / 33
January 17, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

First let's set up a form for entering a username and password to add a new user into the system. Let's use an example of a form for an admin interface to administer a Company X product database for Web display.

<html>
<head>
<title>Company X Products Admin Application Add New User </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1>Administration Interface login: Add new User</h1>
<form name="form1" method="post" action="admin_login_newuserbe.php">
<p>
<input name="uname" type="text" id="uname" size="8" maxlength="8">
   <strong>Enter User Name</strong>
<br>
<input name="pword" type="password" id="pword" size="8" maxlength="8">
   <strong>Enter Password</strong>
   <br><input type="submit" name="Submit" value="Add">
</p>
</form>
</body>
</html>

When the form is submitted to "admin_login_newuserbe.php" the crypt function is used to encrypt the new username. We first confirm that both fields have entries:

if(empty($_POST[pword]) || empty($_POST[uname]))
{
   echo "<html><head></head>
   <body><h1>You must enter both a username and password.
   <a href = \"admin_login_newuser.html\">Try Again?
   </a></h1></body></html>";
}

Additional validation could be added to this either on the client side (using Javascript) or on the server side as needed. Next we will use crypt to encrypt our password and neatly trim leading and trailing white space from our username. They can then be stored in a database or flat file.

else
{
   $pwrd = crypt(trim("$_POST[pword]"));
   $user = trim("$_POST[uname]");
//Code for accessing you database or flat file goes here.



 
 
>>> More PHP Articles          >>> More By Chris Root
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: