End User Recipe: New User Registration


Whether an organization allows new users to register themselves is, of course, a key decision. On the plus side, this policy takes an enormous workload off the administrative staff; however, there are security concerns. This application strikes a balance by allowing self-registration but initially restricting those registered to the lowest level of access. A standard Dreamweaver Insert Record server behavior is used to create the record.

Step 1: Build the Application Design

We start with a basic page, inserting all the form elements needed.

1.

Create a basic dynamic page either by hand or from a template.

In the UserLogin folder, locate the folder for your server model and open the register_user page found there.

2.

Add a table to the content region of your page to contain the interface elements for the application.

From the Snippets panel, drag the Recipes > UserLogin > Wireframes > Register User - Wireframe snippet into the Content editable region.

3.

Within the table, insert the form and any necessary form elements for the application. You'll need five text fieldsone for each of the fields (First Name, Last Name, User Name, Password, and Email Address)as well as a submit button.

Place your cursor in the row below the words REGISTER USER in the second cell and insert the Recipes > UserLogin > Forms > Register User - Form snippet [r1-5].

r1-5.


Enhancing Cookie Production

Our recipe just scratches the surface of what you can do with cookies. The real power of cookies lies in personalization. With sufficient planning, you can personalize the Web experience for every one of your visitors.

Although you can certainly use the recipe code as a starting point for working with cookies, you might find it more productive to use a dedicated extension. WebAssist has created an extension that greatly simplifies creating, accessing, and deleting cookies. With WA Cookies, you can quickly add functionality to remember users when they come back to your site and easily customize their user experience according to stored cookie data.

For more information, visit www.webassist.com/go/Recipes.


Step 2: Add Insert Record Server Behavior

After the framework for the page is in place, we'll apply the first server behaviorInsert Record.

In the process of applying this behavior, you might notice that not all fields have matching form elements. Several columnsUserID, UserAccess, and UserRegDateare automatically populated by the data source when the new record is created. UserID is an autonumber field that increments by 1 for each new user, whereas UserRegDate stores the current date through the use of a special functionNow() in Access, for exampleset as the default value. The UserAccess field defaults to the lowest level of access; it's up to the administrator to raise the level if warranted.

Note

To accommodate the different dialogs for the various server models, these steps are presented for specific servers here and when necessary throughout this recipe.


For ASP

1.

From the Server Behaviors panel, choose Add (+) and select Insert Record to display the dialog [r1-6].

r1-6.


2.

Select the connection to the data source.

Choose Recipes from the Connection list.

3.

Choose the table in your data source to hold the new records.

From the Insert Into Table list, choose the Users table.

4.

In the After Inserting, Go To field, enter the page you want the users to see after registering successfully.

Click the Browse button and locate the login page for your server model.

5.

Declare from which form on the page the data should be taken.

Set the Get Values From field to the form name RegisterUser.

6.

For the form elements shown in the list, set each one to its equivalent in the data source. All form elements should be submitted as Text type:

Set form element FirstName to field UserFirstName.

 

Set form element LastName to field UserLastName.

 

Set form element UserName to field UserName.

 

Set form element Password to field UserPassword.

 

Set form element EmailAddress to field UserEmail.


7.

When you're done, click OK to close the dialog and insert the behavior.

8.

Save your page.

For ColdFusion and PHP

1.

From the Server Behaviors panel, choose Add (+) and select Insert Record.

2.

In the Insert Record dialog, choose the current form.

Select RegisterUser from the Submit Values From list.

3.

Select your data source from the list.

Choose Recipes from the Data Source list.

4.

ColdFusion users should enter their user name and password, if needed.

5.

From the list, select the table in the data source to insert into.

Choose Users (users for PHP) from the Insert Into Table list.

6.

Set the data source fields to their corresponding form elements.

Make sure the UserID data column is set to be an unused Primary Key.

 

UserAccess should not get a value from the form.

 

Set UserFirstName to the FORM.FirstName form element and submit as Text type.

 

Set UserLastName to the FORM.LastName form element and submit as Text type.

 

Set UserName to the FORM.UserName form element and submit as Text type.

 

Set UserPassword to the FORM.UserPassword form element and submit as Text type.

 

Set UserEmail to the FORM.EmailAddress form element and submit as Text type.

 

UserRegDate should not get a value from the form.


7.

In the After Inserting, Go To field, click the Browse button and locate the login page for your server model.

When you're done, click OK to close the dialog and insert the behavior.

8.

Save the page.

Step 3: Store New User Info in a Cookie

As we did on the login page, we'll now store the just-supplied user name in a cookie. The cookie will then be read when the user returns to the login page, automatically filling in the values. This additional bit of code smooths the user experience.

For ASP and ColdFusion

Note

Because the requirements for PHP are different from those of the other server models, the next steps are presented for ASP and ColdFusion users before the application of the Check New Username server behavior. The related PHP steps must be applied after the server behavior is added to the page.


1.

In Code view, ASP users should search for a comment that contains the phrase Insert Record: set variables using Dreamweaver's Find and Replace function. This comment is the first line in a code block; we want to insert another code block just above this one.

ColdFusion users should place their cursor at the top of the file.

2.

Add a paragraph return between the two existing server-side code blocks.

3.

Insert the following code:

From the Snippets panel, open the Recipes > UserLogin > Custom Code folder for your server model and insert the Register - Save Login in Cookie snippet.

[View full width]

<% if (cStr(Request("AddUser"))<>"") then Response.Cookies("UserName") = Request.Form ("UserName") today = DateAdd("d",30,Date()) Response.Cookies("UserName").Expires = today end if %>


[View full width]

<% if (String(Request("AddUser"))!="undefined") { Response.Cookies("UserName") = Request.Form ("UserName"); var today = new Date(); today.setDate(today.getDate()+30); Response.Cookies("UserName").Expires = (today .getMonth()+1) +"/"+today.getDate()+"/"+today. getFullYear(); } %>


[View full width]

<cfif IsDefined("FORM.UserName")> <cflock scope="Session" timeout="30" type="Exclusive"> <cfset Session.MM_Username=FORM.UserName> </cflock> </cfif>


4.

Save your page.

Step 4: Verify User Name Is Unique

One other Dreamweaver server behavior is necessary for the user registration page. To make sure that the user name entered is unique in the data source, we'll employ the Check New Username server behavior. Should a duplicate user name be found, the server behavior will resubmit to the current page with a query string argument that in turn triggers the display of an error message.

1.

From the Server Behaviors panel, select Add (+) and choose User Authentication > Check New Username.

The Check New Username dialog displays [r1-7].

r1-7.


2.

Select the form field containing the user name.

In the Username Field list, choose UserName.

3.

Choose the page you want the application to present if the user name is not unique.

In the If Already Exists, Go To field, click the folder icon and select the register_user page for your server model. Click Parameters in the Select File dialog and enter repeat under the Name column and true under the Value column. Click OK twice to close both the Parameters and the Select File dialogs.

4.

Click OK to close the dialog.

The query string variable technique is useful for displaying conditional text without loading another page. Now we'll add the code that controls what effect the query string variable repeat has on the current page.

1.

In Design view, place the cursor where you'd like the error message to appear.

Put the cursor in the row below the Register User label.

2.

Add the error message code.

From the Snippets panel, open the Recipes > UserLogin > Custom Code folder for your server model and insert the Duplicate User Name - Display Text snippet.

[View full width]

<%if (cStr(Request("repeat"))<>"") then Response .Write("That Username is already taken. Please choose a new name and try again.<br>(Use your browser's back button to update your previous entry.)")%>


[View full width]

<%=(String(Request("repeat"))!="undefined")?"That Username is already taken. Please choose a new name and try again.<br>(Use your browser's back button to update your previous entry.)":""%>


[View full width]

<cfif IsDefined("URL.repeat")><cfoutput> That Username is already taken. Please choose a new name and try again.<br>(Use your browser's back button to update your previous entry.)</cfoutput>< /cfif>


[View full width]

<?php echo (isset($_GET['repeat']))?"That Username is already taken. Please choose a new name and try again.<br>(Use your browser's back button to update your previous entry.)":""; ?>


3.

If you entered the code by hand in Code view, be sure to remove the nonbreaking space character, &nbsp;.

4.

Save the page.

You can test the Register User page from right within Dreamweaver. Select View > Live Data and, in the URL parameter field on the Document toolbar, add the query string repeat=true. Press Tab, and the error message should appear [r1-8].

r1-8.


PHP Only

In addition to adding code to set the cookies, PHP also requires that you temporarily store or cache its processing. Without doing so, the act of setting the cookie would supersede the redirection and prevent it from occurring. PHP activity is cached with the code ob_start() and restored with ob_end_flush().

Note

It is often necessary to insert code in a particular sequence for everything to work as it should. In this case, we needed to include the caching functions after the Insert Record and Check User Name server behaviors were inserted. Had we made our modifications earlier, Dreamweaver would not have recognized the Insert Record server behavior, and the Check Username server behavior therefore could not have been inserted.


First, however, we'll add the code to create the cookies.

1.

In Code view, use Dreamweaver's Find and Replace to search for $insertGoTo = "login.php";.

2.

Insert a paragraph return after the found code and enter the following code:

From the Snippets panel, open the Recipes > UserLogin > Custom Code_PHP folder and insert the Register - Save Login in Cookie snippet.

setcookie ("UserNamePHP", $_POST['UserName'],time()+60*60*24*30);

Now let's wrap a slightly larger code block with our caching commands.

3.

Again using Find and Replace, search for the code that starts with $insertSQL = sprintf.

4.

Select all the code from the found code line up to and including the code line that begins with header(sprintf. The entire code block will look like this:

[View full width]

$insertSQL = sprintf("INSERT INTO users (UserFirstName, UserLastName, UserName, UserPassword, UserEmail) VALUES (%s, %s, %s, %s, %s)", GetSQLValueString($_POST['FirstName'], "text"), GetSQLValueString($_POST['LastName'], "text"), GetSQLValueString($_POST['UserName'], "text"), GetSQLValueString($_POST['Password'], "text"), GetSQLValueString($_POST['EmailAddress'],"text")); mysql_select_db($database_Recipes, $Recipes); $Result1 = mysql_query($insertSQL, $Recipes) or die(mysql_error()); $insertGoTo = "login.php"; setcookie ("UserNamePHP", $_POST['UserName'],time ()+60*60*24*30); if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo));


5.

Enter the following code before and after the selected code block:

From the Snippets panel, open the Recipes > UserLogin > Custom Code_PHP folder and insert the Register - Cache Activity snippet. This snippet has been set to wrap (as opposed to insert) type and is composed of two parts: a before and an after. As the name implies, a wrap type snippet surrounds the selected text with the snippet code.

Before:

 

ob_start()


After:

 

ob_end_flush()


Test your new page by entering values you expect to fail as well as those you're sure will work.




Macromedia Dreamweaver 8 Recipes
Macromedia Dreamweaver 8 Recipes
ISBN: 0321393910
EAN: 2147483647
Year: 2003
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net