U bevindt zich hier: Forum » Tutorials » [PHP] Registratieklasse
   Actief Topic: [PHP] Registratieklasse

Vorige ( 1 ) Volgende

sebastiaan
member
avatar
# Gepost op 23-08-2010 13:03
Bewerkt door sebastiaan op 07-09-2010 11:22


Allereerst: Dit is GEEN stand-alone systeem! Je hebt het login systeem van Vlerk nodig (PHP loginsysteem OOP). Dit is een aanpassing.

Stap een: de hoofd login klasse. Sla dit bestand op als register.class.php
Plain | Plain new window | PHP code:
  1.  
  2. <?php
  3. /*
  4.  * This is a register class for the login system. This used to be a function in the
  5.  * header.php file but I've decided to move it to it's own class.
  6.  * @author Sebastiaan Franken
  7.  * @version 1.0.1
  8.  * @since 1.0
  9.  */
  10. class register
  11. {
  12.     /*
  13.      * The constructor here checks if the form is posted and then run some checks on it.
  14.      * If those checks pass the user is added to the database.
  15.      * If those checks aren't passed the user isn't added.
  16.      */
  17.     public function __construct()
  18.     {
  19.         if(isset($_POST['registreer']) && isset($_POST['actie']) && $_POST['actie'] == 'registreer_gebruiker')
  20.         {
  21.             /* Maken an error array to store errors in */
  22.             $fouten = array();
  23.            
  24.             /*
  25.              * Remove all trailing spaces from the inputs.
  26.              * @since 1.0.1
  27.              */
  28.             $gebruikersnaam = trim($_POST['gebruikersnaam']);
  29.             $wachtwoord = trim($_POST['wachtwoord']);
  30.             $controle = trim($_POST['controle']);
  31.            
  32.             /*
  33.              * Check all form fields if they're empty. If they are an error gets added
  34.              * to the error array. I'm NOT going to write comment on every field below.
  35.              */
  36.             if(empty($gebruikersnaam))         
  37.             {
  38.                 array_push($fouten, 'Er is geen gebruikersnaam ingevuld');
  39.             }
  40.             if(empty($wachtwoord))
  41.             {
  42.                 array_push($fouten, 'Er is geen wachtwoord ingevuld');
  43.             }
  44.             if($controle != $wachtwoord)
  45.             {
  46.                 array_push($fouten, 'Het wachtwoord en het controle wachtwoord is niet gelijk.');
  47.             }
  48.             if(empty($controle))
  49.             {
  50.                 array_push($fouten, 'Er is geen controle wachtwoord ingevuld');
  51.             }
  52.            
  53.             /*
  54.              * If there are errors in the array return them to the user and
  55.              * give them the option to try again
  56.              */
  57.             if(count($fouten) > 0)
  58.             {
  59.                 $count = (count($fouten) > 0) ? 'een veld' : 'een paar velden';
  60.                 $output = '<p>Helaas is er '.$count.' niet ingevuld, die wel verplicht zijn.</p>';
  61.                 $output .= '<ul>';
  62.                
  63.                 foreach($fouten as $fout)
  64.                 {
  65.                     $output .= '<li>'.$fout.'</li>';
  66.                 }
  67.                
  68.                 $output .= '</ul>';
  69.             }
  70.             else
  71.             {
  72.                 /* Secure the database input and insert into
  73.                  * the database.
  74.                  */
  75.                 $gebruikersnaam = mysql_real_escape_string($_POST['gebruikersnaam']);
  76.                 $wachtwoord = (constant('login_password_m5') === true) ? mysql_real_escape_string(md5($_POST['wachtwoord'])) : mysql_real_escape_string($_POST['wachtwoord']);
  77.                 $rang = 'gebruiker';
  78.                
  79.                 /* Run the query or die with the error that insues */
  80.                 mysql_query("INSERT INTO gebruikers(gebruikersnaam, wachtwoord, rang) VALUES('".$gebruikersnaam."', '".$wachtwoord."', '".$rang."')") or die(mysql_error());
  81.                
  82.                 /* Give a sucess message */
  83.                 $output = '<p>De gebruiker is geregistreed.</p>';
  84.             }
  85.         }
  86.         else
  87.         {
  88.             $output = new form($_SERVER['REQUEST_URI']);
  89.             $output->addLabel('Gebruikersnaam')
  90.                    ->addInput('titel', 'text')
  91.                    ->addLabel('Wachtwoord')
  92.                    ->addInput('wachtwoord', 'password')
  93.                    ->addLabel('Controle wachtwoord')
  94.                    ->addInput('controle', 'password')
  95.                    ->addButton('registreer', 'registreer', 'submit')
  96.                    ->output();
  97.         }
  98.        
  99.         return $output;
  100.     }
  101. }
  102. ?>
  103.  


Stap twee. Zoals je ziet heb ik een PHP klasse gebruikt om een form te genereren. Sla dit bestand op als formgenerator.class.php
Plain | Plain new window | PHP code:
  1.  
  2. <?php
  3. /*
  4.  * You can create HTML forms with this class
  5.  * @version 1.0.1
  6.  * @since 1.0
  7.  */
  8. class form
  9. {
  10.     /* The form method */
  11.     public $method;
  12.    
  13.     /* The action */
  14.     public $action;
  15.    
  16.     /* The form buffer */
  17.     private $buffer;
  18.    
  19.     /*
  20.      * The system constructor to add a method and action
  21.      * and start the form code.
  22.      */
  23.     public function __construct($action, $method='post')
  24.     {
  25.         $this->action = $action;
  26.         $this->method = $method;
  27.         $this->buffer = '<form method="'.$this->method.'" action="'.$this->action.'">';
  28.         return $this;
  29.     }
  30.    
  31.     /*
  32.      * With this function you can add a 'label'
  33.      * @since 1.0
  34.      * @return string, HTML
  35.      */
  36.     public function addLabel($text)
  37.     {
  38.         $this->buffer .= '<p>'.$text.'</p>';
  39.         return $this;
  40.     }
  41.    
  42.     /*
  43.      * With this function you can add a input to
  44.      * the form.
  45.      * @since 1.0
  46.      * @return string, HTML
  47.      */
  48.     public function addInput($name, $type, $value=null)
  49.     {
  50.         $this->buffer .= '<p><input type="'.$type.'" name="'.$name.'" value="'.$value.'" /></p>';
  51.         return $this;
  52.     }
  53.    
  54.     /*
  55.      * With this function you can add a textarea to the form.  
  56.      * @since 1.0
  57.      * @return string, HTML
  58.      */
  59.     public function addTextarea($name, $content=null, $rows=null, $cols=null)
  60.     {
  61.         $this->buffer .= '<p><textarea name="'.$name.'" cols="'.$cols.'" rows="'.$rows.'">'.$content.'</textarea></p>';
  62.         return $this;
  63.     }
  64.    
  65.     /*
  66.      * With this function you can add a button to the form
  67.      * @since 1.0
  68.      * @return string, HTML
  69.      */
  70.     public function addButton($type, $text, $name)
  71.     {
  72.         $this->buffer .= '<p><input type="'.$type.'" name="'.$name.'" value="'.$text.'" /></p>';
  73.         return $this;
  74.     }
  75.    
  76.     /*
  77.      * This function returns the form to the user
  78.      * @since 1.0
  79.      * @return string, HTML
  80.      */
  81.     public function output()
  82.     {
  83.         $this->buffer .= '</form>';
  84.         return $this->buffer;
  85.     }
  86. }
  87. ?>
  88.  


Stap drie
Open config.php en voeg deze twee regels toe:
Plain | Plain new window | PHP code:
  1.  
  2. include_once('formcreator.class.php');
  3. include_once('register.class.php');
  4.  


Stap vier
Open header.php en pas de init functie aan tot er dit staat (of kopieer en plak het)
Plain | Plain new window | PHP code:
  1.  
  2. public function init()
  3. {
  4.     /*
  5.      * Check if the function is called by a post and if the loginsession is false
  6.      */
  7.     if($_SERVER['REQUEST_METHOD'] == 'POST' && self::$loginsessie === false)
  8.     {
  9.         /*
  10.          * If the action field is set run a check on it to see what it is
  11.          */
  12.         if(isset($_POST['actie']))
  13.         {
  14.             switch($_POST['actie'])
  15.             {
  16.                 case 'login' : $this->controleer_gegevens(); break;
  17.                 /*case 'registreer_gebruikers' : $this->registreer_gebruikers(); break;*/
  18.                 case 'registreer_gebruikers' :
  19.                     $register = new register();
  20.                     break;
  21.             }
  22.         }
  23.     }
  24. }
  25.  


En voila. Je bent klaar. Verder zijn er geen aanpassingen nodig!

Changelog
1.0 : eerste code
1.0.1 : Code wat verbeterd en wat fouten eruit gehaald.

Mijn software heeft geen bugs. Het ontwikkelt gewoon ongedocumenteerde functies.

vlerknozem
Admin
avatar
# Gepost op 23-08-2010 13:44
Bewerkt door vlerknozem op 23-08-2010 13:44


Ziet er goed uit [[party]]

Wel dan even de registreer_gebruiker functie verwijderen uit mijn login klasse. Anders heb je onnodige code ;)

Lees de forum regels

sebastiaan
member
avatar
# Gepost op 27-08-2010 20:50


Klopt, maar tis nog een beetje werk in uitvoering :)

Mijn software heeft geen bugs. Het ontwikkelt gewoon ongedocumenteerde functies.

Vorige ( 1 ) Volgende

U moet aangemeld zijn om een reactie te kunnen plaatsen.

Indien u nog geen account heeft kunt u zich hier registreren.



© copyright 2009/2010 WebProjects 1.02 - Template van Sebastaan Franken