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.

  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



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.

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

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?

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.

   $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
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: