Article

Managing Users with PHP Sessions and MySQL

Page: 1 2 3 4 5 6 Next

Part One: The Signup Process

The Signup Form

A natural place to start building a site that will require users to register for access is the registration process itself. As one would expect, a simple Web-based form will do the trick. Here's what it will look like:

New User Registration Form

And here's the code for this form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
 <title>New User Registration</title>  
 <meta http-equiv="Content-Type"  
   content="text/html; charset=iso-8859-1" />  
</head>  
<body>  
 
<h3>New User Registration Form</h3>  
<p><font color="orangered" size="+1"><tt><b>*</b></tt></font>  
  indicates a required field</p>  
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">  
<table border="0" cellpadding="0" cellspacing="5">  
   <tr>  
       <td align="right">  
           <p>User ID</p>  
       </td>  
       <td>  
           <input name="newid" type="text" maxlength="100" size="25" />  
           <font color="orangered" size="+1"><tt><b>*</b></tt></font>  
       </td>  
   </tr>  
   <tr>  
       <td align="right">  
           <p>Full Name</p>  
       </td>  
       <td>  
           <input name="newname" type="text" maxlength="100" size="25" />  
           <font color="orangered" size="+1"><tt><b>*</b></tt></font>  
       </td>  
   </tr>  
   <tr>  
       <td align="right">  
           <p>E-Mail Address</p>  
       </td>  
       <td>  
           <input name="newemail" type="text" maxlength="100" size="25" />  
           <font color="orangered" size="+1"><tt><b>*</b></tt></font>  
       </td>  
   </tr>  
   <tr valign="top">  
       <td align="right">  
           <p>Other Notes</p>  
       </td>  
       <td>  
           <textarea wrap="soft" name="newnotes" rows="5" cols="30"></textarea>  
       </td>  
   </tr>  
   <tr>  
       <td align="right" colspan="2">  
           <hr noshade="noshade" />  
           <input type="reset" value="Reset Form" />  
           <input type="submit" name="submitok" value="   OK   " />  
       </td>  
   </tr>  
</table>  
</form>  
 
</body>  
</html>

There is actually one piece of PHP code already embedded in this form:

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">.

Notice the action attribute. In a familiar design pattern, which I have called multipurpose pages in previous articles, we are causing the form to be submitted to the same page containing the form code. By watching for the PHP variable $_POST['submitok'], which will be created by the submit button in this form (notice name="submitok" in the tag), our script will be able to handle the form submissions as well as the display of the form itself.

We're also using a relatively new form of syntax to print out the $_SERVER['PHP_SELF'] variable. In case you are unfamiliar with this syntax, allow me to clarify that <?=expression?> is functionally identical to <?php echo expression; ?>. Thus, in this case, we could have instead written the following:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Now that we've got the code for our form, we can move on to writing the complete signup script.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links