Revision [6650]

This is an old revision of RegisterAction made by DarTar on 2005-03-10 18:10:39.

 

Register Action

Last edited by DarTar:
Slightly improved version
Thu, 10 Mar 2005 18:10 UTC [diff]


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:


[2005-02-25] action uploaded on this site as a beta feature: RegisterActionTest (you'll need to logout to test it)

The action

Current version: 0.3

Done:

To do:

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.3
  13.  * @since       Wikka 1.1.X.X
  14.  * @output      form for user registration
  15.  *
  16.  * @todo
  17.  *          - CSS to style form;
  18.  *          - (optionally) drop WikiName restriction on usernames;
  19.  *          - use core functions to validate fields;
  20.  *          - use central error handler for printing error messages;
  21.  *          - decide best strategy to link hardcoded login/logout page;
  22.  *          - define welcome page where new users must be redirected;
  23.  *          - (optionally) add option for email-confirmation of registered users.
  24.  */
  25.  
  26. // constants
  27. define('MIN_PASSW_LENGTH', '5');
  28. define('DEFAULT_REDIRECT_TO', 'WelcomeUser');
  29.  
  30. print $this->Format('===== Registration page =====');
  31.         print '(from: '.$HTTP_REFERER;
  32.  
  33. if ($this->GetConfigValue('allow_new_users') == '0')
  34. {
  35.     // user registration is disabled
  36.     print $this->Format('//User registration is disabled on this wiki//');
  37. } else
  38. {
  39.     if ($user = $this->GetUser())
  40.     {
  41.  
  42.         // user is logged in
  43.  
  44.         // initializing variables
  45.         $name = '';
  46.         $email = '';
  47.         $password = '';
  48.         $confpassword = '';
  49.         $error = '';
  50.    
  51.         // is this the first time the user logs in?
  52.         if ((isset($_GET['reg'])) && ($_GET['reg'] == '1'))
  53.         {
  54.  
  55.             switch ($this->GetConfigValue('allow_new_users'))
  56.             {
  57.                 default:
  58.                 case 0:
  59.                 // print first login welcome screen
  60.                 print $this->Format('--- **Registration successful!** --- --- You are currently logged in as '.$this->GetUserName());
  61.                 break;
  62.    
  63.                 case 1:
  64.                 // redirect to welcome page
  65.                 $this->Redirect($this->href('', DEFAULT_REDIRECT_TO));
  66.                 break;
  67.    
  68.                 case 2:
  69.                 // redirect to referrer page
  70.                 $this->Redirect($this->href('', DEFAULT_REDIRECT_TO));
  71.                 break;
  72.             }
  73.  
  74.         } else
  75.         {
  76.             // user is already logged in: print user information
  77.             print $this->Format('--- You are currently logged in as '.$this->GetUserName());
  78.         }
  79.  
  80.     } else
  81.     {
  82.  
  83.         // user is not logged in
  84.    
  85.         // is user trying to register?
  86.         if ($_POST)
  87.         {
  88.  
  89.  
  90.             // get POST values
  91.             if (isset($_POST['name'])) $name = trim($_POST['name']);
  92.             if (isset($_POST['email'])) $email = trim($_POST['email']);
  93.             if (isset($_POST['password'])) $password = $_POST['password'];
  94.             if (isset($_POST['confpassword'])) $confpassword = $_POST['confpassword'];
  95.    
  96.             // validate fields
  97.             // note: all these validation checks should use core functions to preserve consistency
  98.  
  99.             if ($this->LoadUser($name))
  100.             {
  101.                 $error = 'Sorry, this username already exists. Please choose a different name.';
  102.                 $validname = $this->Action('failed');
  103.             } elseif ($this->ExistsPage($name))
  104.             {
  105.                 $error = 'Sorry, this username is reserved for a page. Please choose a different name.';
  106.                 $validname = $this->Action('failed');
  107.             } elseif (!$this->IsWikiName($name))
  108.             {
  109.                 $error = 'Please fill in a valid username (formatted as a ##""WikiName""##).';
  110.                 $validname = $this->Action('failed');
  111.             } elseif (!$email)  
  112.             {
  113.                 $error = 'Please specify an email address.';
  114.                 $validname = $this->Action('done');
  115.                 $validemail = $this->Action('failed');
  116.             } elseif (!preg_match("/^.+?\@.+?\..+$/", $email))
  117.             {
  118.                 $error = 'That does not quite look like an email address.';
  119.                 $validname = $this->Action('done');
  120.                 $validemail = $this->Action('failed');
  121.             } elseif (!$password)
  122.             {
  123.                 $error = 'Please choose your password.';
  124.                 $validname = $this->Action('done');
  125.                 $validemail = $this->Action('done');
  126.                 $validpassword = $this->Action('failed');
  127.             } elseif (strlen($password) < MIN_PASSW_LENGTH)
  128.             {
  129.                 $error = 'Sorry, password too short (min. '.MIN_PASSW_LENGTH.' chars).';
  130.                 $validname = $this->Action('done');
  131.                 $validemail = $this->Action('done');
  132.                 $validpassword = $this->Action('failed');
  133.             } elseif (preg_match("/ /", $password)) {
  134.                 $error = 'Sorry, spaces are not allowed in passwords.';
  135.                 $validname = $this->Action('done');
  136.                 $validemail = $this->Action('done');
  137.                 $validpassword = $this->Action('failed');
  138.             } elseif (!$confpassword)
  139.             {
  140.                 $error = 'You need to confirm your password.';
  141.                 $validname = $this->Action('done');
  142.                 $validemail = $this->Action('done');
  143.                 $validpassword = $this->Action('failed');
  144.                 $validconfpassword = $this->Action('failed');
  145.             } elseif ($confpassword != $password)
  146.             {
  147.                 $error = 'Sorry, passwords do not match.';
  148.                 $validname = $this->Action('done');
  149.                 $validemail = $this->Action('done');
  150.                 $validpassword = $this->Action('failed');
  151.                 $validconfpassword = $this->Action('failed');
  152.             } else
  153.             {
  154.                 // all required fields are valid and non-empty
  155.  
  156.                 // create user
  157.                 $this->Query("insert into ".$this->config["table_prefix"]."users set ".
  158.                     "signuptime = now(), ".
  159.                     "name = '".mysql_real_escape_string($name)."', ".
  160.                     "email = '".mysql_real_escape_string($email)."', ".
  161.                     "password = md5('".mysql_real_escape_string($password)."')");
  162.  
  163.                 // log in
  164.                 $this->SetUser($this->LoadUser($name));
  165.    
  166.                 // forward
  167.                 $this->Redirect($this->href('','','reg=1'));
  168.             }
  169.         }
  170.  
  171.  
  172.        
  173.         $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:
  174. ~-your **username** (it must be formatted like a ##""WikiName""##, for example: ##""JuliusCaesar""##);
  175. ~-a **valid email address** (this will only be used to retrieve your password in case you lose it);
  176. ~-a **valid password** (min. '.MIN_PASSW_LENGTH.' characters, no space allowed).
  177. --- ---');
  178.  
  179.         // build registration form
  180.         $form  = $this->FormOpen();
  181.         $form .= '  <table summary="Form to provide registration data: username, email and password">';
  182.         $form .= '  <caption>Registration form</caption>';
  183.         $form .= '  <tbody>';
  184.    
  185.         if (isset($error))
  186.         {
  187.             $form .= '<tr><td colspan="3" align="center"><span class="error">'.$this->Format($error).'</span></td></tr>';
  188.         }
  189.         $form .= '      <tr>';
  190.         $form .= '          <th align="right" scope="row"><label for="name">Your username:</label></th>';
  191.         $form .= '          <td><input name="name" id="name" size="40" value="'.$name.'" title="Choose a valid username (formatted as a WikiName)" /></td>';
  192.         $form .= '          <td>'.$validname.'</td>';
  193.         $form .= '      </tr>';
  194.         $form .= '      <tr>';
  195.         $form .= '          <th align="right" scope="row"><label for="email">Your email address:</label></th>';
  196.         $form .= '          <td><input name="email" id="email" size="40" value="'.$email.'" title="Fill in a valid email address"/></td>';
  197.         $form .= '          <td align="left">'.$validemail.'</td>';
  198.         $form .= '      </tr>';
  199.         $form .= '      <tr>';
  200.         $form .= '          <th align="right" scope="row"><label for="password">Your password:</label></th>';
  201.         $form .= '          <td><input type="password" name="password" id="password" size="40" title="Choose a valid password (min. '.MIN_PASSW_LENGTH.' chars, no space)" /></td>';
  202.         $form .= '          <td align="left">'.$validpassword.'</td>';
  203.         $form .= '      </tr>';
  204.         $form .= '      <tr>';
  205.         $form .= '          <th align="right" scope="row"><label for="confpassword">Confirm password:</label></th>';
  206.         $form .= '          <td><input type="password" name="confpassword" id="confpassword" size="40" title="Type again your password for confirmation" /></td>';  
  207.         $form .= '          <td align="left">'.$validconfpassword.'</td>';
  208.         $form .= '      </tr>';
  209.         $form .= '      <tr>';
  210.         $form .= '          <td></td>';
  211.         $form .= '          <td><input type="submit" value="Register" title="Register" /></td>';  
  212.         $form .= '      </tr>';
  213.         $form .= '  </tbody>';
  214.         $form .= '  </table>';
  215.         $form .= $this->FormClose();
  216.  
  217.         // output intro and form
  218.         print $intro.$form;
  219.     }
  220. }
  221. ?>






Discussion





Much better... a few more comments:
  1. The variables are still not being initialized. If a user does not provide a value when submitting the form, the variable won't be set - and then you're trying to use the unset variable(s) as parameters to functions and values for form fields. Try not excluding E_NOTICE in php's error reporting and submit an empty form - and see what you get...
  1. What's the mysterious JavaScript for? Do we even need it?
  1. I don't think the submit button can do anything with a size attribute (missed that the first time)
--JavaWoman


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