User Registration Hacks


In this first section, you look at a few hacks to the core user registration system. Management of the registration process is an important part of your site; it's in many ways the first real interaction a user might have with your website. First impressions are always powerful, so you want to be certain the system works smoothly for your users.

The X-User module provides a number of dynamic registration features, but this section looks at hacks you can apply to your default PostNuke install.

Requiring Optional Registration Fields

PostNuke has a number of premade fields built in to the system that you can toggle on and off together using the Show Optional Fields entry in the User Registration Configuration page. You can turn these extra fields on, but you can't control which are required. It's relatively easy to use the existing server-side validation system to add some of these other optional fields to the required list.

You need two files open for editing to complete this hack:

 /modules/NS-NewUser/user.php /modules/NS-NewUser/lang/eng/global.php 

The global.php file contains the text variable definitions for the English language. You need to add some variables to display messages for users. But first, you can begin with edits to the user.php file. In your editor, scroll down to about line 492. You should see the function optionalitems().

Take a look through this function to see how it writes out the input fields for the form. Any of these fields can be made required, but for this example, you specifically look at the Real Name field. It's at roughly line 525:

 echo "<input type=\"text\" name=\"name\" value=\"" . pnVarPrepForDisplay(pnUserGetVar('name')) . "\" size=\"30\" maxlength=\"60\" />"; 

What's important here is the name attribute. It tells you how to reference the data entered into the field. In this case, the attribute happens to have the value "name."

With that knowledge in hand, scroll back up to about line 304:

 function newuser_user_finishnewuser($var) {     $dbconn =& pnDBGetConn(true);     $pntable =& pnDBGetTables();     list($name,         $agreetoterms,         $email, 

This is the start of the function that completes the new user submission process. Notice that the list function is taking the form data and converting it into PHP variables. The "name" data is from here on out referred to as $name.

Tip

Add comments to all your changes to make it clear how the hack works.


A little farther down to line 368 reveals the userCheck function call:

 $stop = userCheck($uname, $email, $agreetoterms); 

This is the first change you need to make. You now know the variable that needs checking is $name, but it needs to be sent to the userCheck function for validation. Change the call to look like this:

 $stop = userCheck($uname, $email, $agreetoterms, $name); 

Now much farther up in the code near line 182, you should find the userCheck function itself:

 function userCheck($uname, $email, $agreetoterms) {     $dbconn =& pnDBGetConn(true);     $pntable =& pnDBGetTables(); 

Similar to the previous change, add the $name variable to the function inputs:

 function userCheck($uname, $email, $agreetoterms, $name) 

Look through the function contents. You can see multiple if blocks where the other variables are checked. You need to add the validation code after all that, so scroll down a bit farther to about line 254. You know you are at the end of the checks when you see the return($stop); line. Just before that, add in this code:

 // Code below added for required Real Name field validation if ((!$name) || ($name=="") || (!preg_match("/^[a-zA-Z \.\'\-]+$/", $name))) {    $stop = "<div style=\"text-align:center\"><span class=\"pn-title\">".         _REALNAMEINVALID."</div></span><br />"; } 

That code checks to see if the field is blank or if it contains any characters other than letters, spaces, hyphens, apostrophes, or periods. This allows for most common contracted or combined names, as well as initial use. More specific checks can be made, and the check should be customized to the field you want to validate.

Tip

For more information on pattern matching and validation, head over to www.php.net and do a function search on preg_match and ereg.


Now, you might have also noticed you just used a previously undefined variable: _REALNAMEINVALID. This is the message displayed to the user when the name that's entered doesn't pass the validation. Switch over to the global.php file to add in the text definition. In keeping with the alphabetical order, add this line after _PRIVACYPOLICY:

 define('_REALNAMEINVALID','The real name you have entered contains characters that are not allowed. Please try again.'); 

Take a look at the registration form in Figure 23.1; you can browse to this form by registrating as a new user. The Your Name field might be required, but there is nothing to inform a new user of that fact. And the Optional Items heading is now clearly misleading.

Figure 23.1. Don't keep required fields a secret.


In the global.php file, add in two additional variables:

 define('_ADDITIONALITEMS','Additional Items'); define('_REQUIREDITEM','(required)'); 

Go back to the user.php file and scroll down to the optionalitems function, now at about line 496. Look for the form input that collects the name data. It should be at line 529. Add the required variable after the input like this:

 case "_UREALNAME":     echo "<input type=\"text\" name=\"name\" value=\"" . pnVarPrepForDisplay(pnUser GetVar('name')) . "\" size=\"30\" maxlength=\"60\" /> "._REQUIREDITEM;     break; 

Now scroll back up to line 171 to take care of the heading. The completed code should be similar to this:

 echo "<tr>"     ."<td>&nbsp;</td>"     // _OPTIONALITEMS changed to _ADDITIONALITEMS for new required fields     ."<td><span class=\"pn-normal\"><strong>" . _ADDITIONALITEMS . "</strong></span></td>"     ."</tr>\n" . "";     // Display optional items to register optionalitems(); 

Now, you have the finished form shown in Figure 23.2.

Figure 23.2. Requiring specific additional items.


Prompting Users to Remember Their Password

When new users first receive their passwords in their email, the passwords are randomly generated. Occasionally, users delete that email without changing the password to something they can remember. This requires the user to come back to the site and request a password reset, which results in an identity check email, a form submit with the confirmation code, and eventually an email sent to the user with yet another randomly generated password.

That is quite a bit of effort for what should be a simple problem. The real issue is whether that's too much trouble for some of your users. You might lose registered users just because they can't remember their password.

You can prevent the problem from happening with a simple prompt in the registration email telling users to save the email with their random password, or, better yet, to go to their account settings and change the password to something easier to remember.

This fix requires only a change or two to this file:

 /modules/NS-NewUser/lang/eng/global.php 

Add the prompt to the email message by editing one of the existing variables to include the wording you have in mind. For example, change this line:

 define('_FOLLOWINGMEM','The information stored about you is as follows:'); 

To read as this:

 define('_FOLLOWINGMEM','Your initial password has been randomly generated. You can change the random password to something more memorable by editing your account settings. The login information for your account is:'); 

You can also change the registration message that appears on the screen after the form is submitted. The original message:

 define('_YOUAREREGISTERED','You are now registered. You should receive your password at the e-mail address you provided.'); 

can be expanded to say:

 define('_YOUAREREGISTERED','You are now registered. You should receive your password at the email account you provided. Please remember to write down your user name and password and put them someplace safe. You can change the random password to something more memorable by editing your account settings.'); 

Editing the language files to customize messages for your site helps your users stay connected. Update the phrasing of these variables to appeal to your user base.

Formatting the New User Email

Many site administrators have expressed an interest in being able to edit the email message sent to newly registered users. The real trick is that the email content is contained in both language variables and the programming source. Open up your text editor and load up these files:

 /modules/NS-NewUser/user.php /modules/NS-NewUser/lang/eng/global.php 

The first file performs the registration functions and the second defines a list of global text variables. In the user.php file, scroll down to about line 461 and find this code:

 $message = "" . _WELCOMETO . " $sitename ($siteurl)!\n\n" . _YOUUSEDEMAIL . " ($email) " . _TOREGISTER . " $sitename. " . _FOLLOWINGMEM . "\n\n" . _UNICKNAME . " $uname\n" . _UPASSWORD . " $makepass"; $subject = "" . _USERPASS4 . " $uname" . _USERPASS42 . ""; 

The first line builds the message body and the second creates the subject line for the email. Editing these lines alters the formatting of the message and how it calls the variables. The text used in the message is changed by editing the global.php file. The formatting options possible are relatively unlimited, but just for example, the following is an alternative phrasing.

Tip

Add text entries to the different global.php files in each language your site supports to stay consistent.


Using these language variables:

 define('_WELCOMETOTHE','Welcome to the'); define('_COMMUNITY','community!'); define('_RECEIVEDREGISTRATION','We have received your registration of the account'); define('_CREATEDRANDOM','and have created an initial random password for you.'); define('_ACCOUNTPASSIS','Your account password is'); define('_CHANGERANDOM','You can change the random password to something more memorable by editing your account settings.'); define('_THANKSREGISTER','Thanks for registering!'); 

and this message code:

 $message = ""._WELCOMETOTHE." $sitename "._COMMUNITY."\n\n"._RECEIVEDREGISTRATION." $uname "._CREATEDRANDOM."\n"._ACCOUNTPASSIS." $makepass.\n"._CHANGERANDOM."\n\n"._ THANKSREGISTER."\n"; 

you get this email message:

 Welcome to the mysite.com community! We have received your registration of the account myaccount and have created an initial random password for you. Your account password is mypass You can change the random password to something more memorable by editing your account settings. Thanks for registering! 



    PostNuke Content Management
    PostNuke Content Management
    ISBN: 0672326868
    EAN: 2147483647
    Year: 2003
    Pages: 207
    Authors: Kevin Hatch

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