Revision [6297]

This is an old revision of RegisterAction made by IanAndolina on 2005-02-24 23:13:27.

 

Register Action


See also:
Documentation: RegisterActionInfo.
This is the development page for the Register action.
 


I've started working on a new version of an action for user registration. The motivation behind this is to replace the current usersetting action with three distinct actions:



The code


Save the following as ./actions/register.php and use it as {{register}}.

  1. <?php
  2. /**
  3.  * Display a form for user registration.
  4.  *
  5.  * This action allows new users to register an account, if user registration is enabled.
  6.  * All the required fields are validated before the new user is created.
  7.  *
  8.  * @package     Actions
  9.  * @name        Register
  10.  *
  11.  * @author      {@link http://wikka.jsnx.com/DarTar Dario Taraborelli}
  12.  * @version     0.1
  13.  * @since       Wikka 1.1.X.X
  14.  *
  15.  * @todo
  16.  *          - (optionally) drop WikiName restriction on usernames;
  17.  *          - use core functions to validate fields;
  18.  *          - add done/failed icons for each field after form is posted;
  19.  *          - use central error handler for printing error messages;
  20.  *          - define strategy to link hardcoded login/logout page;
  21.  *          - define welcome page where new users must be redirected;
  22.  */
  23.  
  24. print $this->Format('===== Registration page =====');
  25.  
  26. if ($this->GetConfigValue("allow_new_users") == "0") {
  27.     // user registration is disabled
  28.     print $this->Format('//User registration is disabled on this wiki//');
  29. } else {
  30.     if ($user = $this->GetUser()){
  31.         // user is logged in
  32.    
  33.         // is this the first time the user is logged in?
  34.         if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'register')) {
  35.             print $this->Format('--- **Registration successful!** --- You are currently logged in as '.$this->GetUserName());
  36.         } else {
  37.    
  38.         print $this->Format('--- You are currently logged in as '.$this->GetUserName());
  39.         }
  40.     } else {
  41.         // user is not logged in
  42.         print "<script type=\"text/javascript\"><!-- \nfunction hov(loc,cls){ \n    if(loc.className) loc.className=cls;\n}\n //-->\n</script>\n";
  43.    
  44.         // is user trying to register?
  45.         if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'register')) {
  46.  
  47.             // get POST values
  48.             if ($_POST['name']) $name = trim($_POST['name']);
  49.             if ($_POST['email']) $email = trim($_POST['email']);
  50.             if ($_POST['password']) $password = $_POST['password'];
  51.             if ($_POST['confpassword']) $confpassword = $_POST['confpassword'];
  52.    
  53.             // validate fields
  54.             // note: all these validation checks should use core functions to preserve consistency
  55.  
  56.             if ($this->LoadUser($name)) $error = 'Sorry, this username already exists. Please choose a different name.';
  57.             elseif ($this->ExistsPage($name)) $error = 'Sorry, this username is reserved for a page. Please choose a different name.';
  58.             elseif (!$this->IsWikiName($name)) $error = 'Please fill in a valid username (formatted as a ##""WikiName""##).';
  59.             elseif (!isset($email)) $error = 'Please specify an email address.';
  60.             elseif (!preg_match("/^.+?\@.+?\..+$/", $email)) $error = 'That does not quite look like an email address.';
  61.             elseif (!isset($password)) $error = 'Please choose your password.';
  62.             elseif (strlen($password) < 5) $error = 'Sorry, password too short.';
  63.             elseif (preg_match("/ /", $password)) $error = 'Sorry, spaces are not allowed in passwords.';
  64.             elseif (!isset($confpassword)) $error = 'You need to confirm your password.';
  65.             elseif ($confpassword != $password) $error = 'Sorry, passwords do not match.';
  66.             else {
  67.                 // all required fields are valid and non-empty
  68.  
  69.                 // create user
  70.                 $this->Query("insert into ".$this->config["table_prefix"]."users set ".
  71.                     "signuptime = now(), ".
  72.                     "name = '".mysql_real_escape_string($name)."', ".
  73.                     "email = '".mysql_real_escape_string($email)."', ".
  74.                     "password = md5('".mysql_real_escape_string($_POST["password"])."')");
  75.  
  76.                 // log in
  77.                 $this->SetUser($this->LoadUser($name));
  78.    
  79.                 // forward
  80.                 $this->Redirect($this->href());
  81.             }
  82.         }
  83.        
  84.         $intro = $this->Format(' --- If you are a **new user** you can register an account using this form (if you already have an account, please go to the [[UserSettings login page]]). --- --- To register, the following fields are required:
  85. ~-your **username** (it must be formatted like a ##""WikiName""##, for example: ##""JuliusCaesar""##);
  86. ~-a **valid email address** (this will only be used to retrieve your password in case you lose it);
  87. ~-a **valid password** (min. 5 characters, no space allowed).
  88. --- ---');
  89.  
  90.         // build registration form
  91.         $form = $this->FormOpen();
  92.         $form .= '<input type="hidden" name="action" value="register" />';
  93.         $form .= '  <table>';
  94.    
  95.         if (isset($error)) {
  96.             $form .= '<tr><td></td><td><span class="error">'.$this->Format($error).'</span></td></tr>';
  97.         }
  98.         $form .= '      <tr>';
  99.         $form .= '          <td align="right">Your username:</td>';
  100.         $form .= '          <td><input name="name" size="40" value="';
  101.         $form .= (isset($name))? $name : '';
  102.         $form .= '" title="Choose a valid username (formatted as a WikiName)" /></td>';
  103.         $form .= '      </tr>';
  104.         $form .= '      <tr>';
  105.         $form .= '          <td align="right">Your email address:</td>';
  106.         $form .= '          <td><input name="email" size="40" value="';
  107.         $form .= (isset($email))? $email : '';
  108.         $form .= '" title="Fill in a valid email address"/></td>';
  109.         $form .= '      </tr>';
  110.         $form .= '      <tr>';
  111.         $form .= '          <td align="right">Your password:</td>';
  112.         $form .= '          <td><input type="password" name="password" size="40" title="Choose a valid password (min. 5 chars, no space)" /></td>';
  113.         $form .= '      </tr>';
  114.         $form .= '      <tr>';
  115.         $form .= '          <td align="right">Confirm password:</td>';
  116.         $form .= '          <td><input type="password" name="confpassword" size="40" title="Type again your password for confirmation" /></td>';
  117.         $form .= '      </tr>';
  118.         $form .= '      <tr>';
  119.         $form .= '          <td align="right"></td>';
  120.         $form .= '          <td><input type="submit" value="Register" size="40" title="Click to register" /></td>';
  121.         $form .= '      </tr>';
  122.         $form .= '  </table>';
  123.         $form .= $this->FormClose();
  124.  
  125.         // output intro and form
  126.         print $intro.$form;
  127.     }
  128. }
  129. ?>


Try to use SemanticMarkup if this is going to be rewritten anyway, instead of:
$form .= '<tr>';
$form .= '	<td align="right">Confirm password:</td>';
$form .= '	<td><input type="password" name="confpassword" size="40" title="Type again your password for confirmation" /></td>';
$form .= '</tr>';

use:
$form .= '<label>Confirm password:<input type="password" name="confpassword" size="40" title="Type again your password for confirmation" /></label>';


It is more elegant, semantically clean and frees some bytes to run free in forests! --IanAndolina




CategoryDevelopment
There are 4 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki