login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Ledensystemen > InlogClass V0.1 AdoDB-Engine

InlogClass V0.1 AdoDB-Engine

Auteur: ArieMedia - 15 oktober 2009 - 12:02 - Gekeurd door: Koen - Hits: 5390 - Aantal punten: 4.25 (2 stemmen)





InlogClass V0.1, AdoDB Version!
Dat betekend dus dat je voor deze versie de AdoDB-databaseclass nodig hebt,
vandaag of morgen zal ik een paar kleine veranderingen doen en zorgen dat er ook een mysqli versie beschikbaar is, indien dit script word goedgekeurd.


AdoDB Download
Downloaden
Simpel voorbeeldje van gebruik..
  1. final protected function _StartDB() {
  2. include_once _MAINPATH_ .'shell/adoDB/adodb.inc.php';
  3. $this->oDB = NewADOConnection('mysql');
  4. $this->oDB->Connect('localhost', 'xxx', 'xxx', 'xxx');
  5. }

(Dus de connectie sla ik op in een variabele van de class)

Wat doet deze class?
Deze class valideert inloggegevens, een wachtwoord moet 8 tekens lang zijn en mag geen speciale characters bevatten, een gebruikersnaam kan kleine letters, hoofdletters en nummers bevatten en heeft geen limiet.

Afhankelijke functies
We gaan er van uit dat de class is opgestart onder de naam $oClass

De tabel die we gaan aanroepen bepalen we met $oClass->Tabel = 'gebruikers';
Nu gaat de tabel de query uitvoeren op tabel 'gebruikers'.

De velden die we willen ophalen bepalen we met $oClass->Fields.
Dit moet een array zijn, ook al haal je maar 1 waarde op!
$oClass->Fields = array('id', 'userlevel');
Deze waardes komen ook in je sessie.

Het veld waar we op moeten gaan controleren heb ik "naam" en "pass" genoemd, deze zijn zelf instelbaar en zal je ook eerst moeten instellen
$oClass->NameField = 'naam'; (Gaat controleren op veldnaam "naam") -> Kan je ook bijvoorbeeld email noemen als je op email gaat controleren.
$oClass->PassFields = 'pass'; (Gaat OOK op veldnaam "pass" controleren) -> Werkt alleen voor wachtwoorden, ivm met de koppeling op het ge-encrypte wachtwoord

De inloggegevens (dus de posts die je binnen krijgt) zetten we hier in:
$oClass->Name = $_POST['naam'];
$oClass->Pass = $_POST['pass'];

Error's kunnen we ophalen met
echo $oClass->sError;
eventuele userinformatie die goed gevalideerd is kan je ophalen met
echo '<pre>'.print_r($oClass->aUser, 1).'</pre>';

Manier van gebruik:
  1. $oLogin = new inloggen();
  2. $oLogin->Name = 'Arjan';
  3. $oLogin->Pass = 'isgekker';
  4. $oLogin->Fields = array('id', 'userlevel');
  5. $oLogin->NameField = 'naam';
  6. $oLogin->PassField = 'pass';
  7. $oLogin->Tabel = 'gebruikers';
  8. if($oLogin->Login() === true) {
  9. echo 'Succesvolle login!';
  10. print_r($_SESSION);
  11. } else {
  12. echo 'Geen succesvolle login!';
  13. }
  14. echo $oLogin->sError;


Query is opgebouwd naar: SELECT id, userlevel FROM gebruikers WHERE naam= "Arjan" AND pass = "6d45bccb7346c92c2e76f0c14485d17c"

Have fun with it!

Code:
  1. <?php
  2. /**
  3.  * Loginclass
  4.  * This class is build to build a protected login
  5.  *
  6.  * @author: Arjan Veldkamp (ArieMedia.nl)
  7.  * @version: 0.1
  8.  **/
  9. class inloggen {
  10.  
  11. /**
  12. * Database class
  13. *
  14. * @param Smarty databaseclass
  15. **/
  16. private $oDB;
  17. /**
  18. * Save succesvol validated information
  19. *
  20. * @param void
  21. **/
  22. protected $aUser = array('naam' => '', 'pass' => '');
  23.  
  24. /**
  25. * Save the exception
  26. *
  27. * @param string
  28. **/
  29. protected $sError = 'Geen errors om te laten zien';
  30.  
  31. /**
  32. * Here we store the databasesettings
  33. *
  34. * @param array
  35. **/
  36.  
  37. protected $aData = array('tabel', 'fields' => array(), 'controllers' => array());
  38. /**
  39. * The constructor
  40. * Here we start the databaseconnection
  41. * If you dont need a connection use false
  42. *
  43. * @param boolean
  44. * @return void
  45. **/
  46. public function __construct($bDB=true) {
  47. /** Laad hier je AdoDB-class en stel hem in.
  48. require_once 'pad/naar/globals/globals.php';
  49. require_once _MAINPATH_ .'shell/engine.php';
  50. $oShell = new Shell();
  51. $this->oDB = $oShell->oDB;
  52. */
  53. }
  54.  
  55. /**
  56. * The setter
  57. * So we can savely set the name and password
  58. *
  59. * @param string, string
  60. * @return void
  61. **/
  62.  
  63. public function __set($sKey, $sVal) {
  64. try {
  65. switch($sKey) {
  66. case 'Name':
  67. $this->_ValidateName($sVal);
  68. break;
  69. case 'Pass':
  70. $this->_ValidatePass($sVal);
  71. break;
  72. case 'Fields':
  73. $this->_ValidateFields($sVal);
  74. break;
  75. case 'Tabel':
  76. $this->aData['tabel'] = $sVal;
  77. break;
  78. case 'NameField':
  79. $this->aData['controllers']['naam'] = $sVal;
  80. break;
  81. case 'PassField':
  82. $this->aData['controllers']['pass'] = $sVal;
  83. break;
  84. }
  85. } catch(Exception $e) {
  86. $this->sError = '<strong>FOUT: </strong><br>Bericht: '.$e->getMessage().' op regel '.$e->getLine();
  87. }
  88. }
  89.  
  90. /**
  91. * The getter
  92. * We can get the userinformation and debugging information
  93. *
  94. * @param string
  95. * @return ???
  96. **/
  97.  
  98. public function __get($sKey) {
  99. switch($sKey) {
  100. case 'aUser': return $this->aUser; break;
  101. case 'sError': return $this->sError; break;
  102. default: return false; break;
  103. }
  104. }
  105.  
  106. /**
  107. * The login
  108. * Here we make the session
  109. *
  110. * @param void
  111. * @return boolean
  112. **/
  113.  
  114. public function Login() {
  115. try {
  116. if(count($this->aData['fields']) == 0) {
  117. throw new Exception('Er zijn geen velden gezet, gebruik $oClass->Fields = array()');
  118. } else {
  119. $sFields = implode(', ', $this->aData['fields']);
  120. }
  121.  
  122. if(count($this->aData['controllers']) == 0) {
  123. throw new Exception('Er zijn geen controllers gezet, gebruik $oClass->Controllers = array()');
  124. } else {
  125. $sControl = 'WHERE '.$this->aData['controllers']['naam'] .'= "'.$this->aUser['naam'].'"
  126. AND '.$this->aData['controllers']['pass'] .' = "'.$this->aUser['pass'].'"';
  127. }
  128.  
  129. if(empty($this->aData['tabel'])) {
  130. throw new Exception('Er is geen tabel ingesteld!');
  131. } else {
  132. $sTabel = $this->aData['tabel'];
  133. }
  134.  
  135. $sql = 'SELECT '.$sFields.'
  136. FROM '.$sTabel.' '
  137. .$sControl;
  138.  
  139. if(!$this->oDB->Execute($sql)) {
  140. throw new Exception('Query is mislukt!<br>'.$sql);
  141. } else {
  142. $rs = $this->oDB->Execute($sql);
  143. if($rs->RecordCount() > 0) {
  144. $iFields = count($this->aData['fields']);
  145. for($i=0; $i<$iFields; $i++) {
  146. $_SESSION[$this->aData['fields'][$i]] = $rs->Fields($this->aData['fields'][$i]);
  147. }
  148. return true;
  149. } else return false;
  150. }
  151. }
  152. catch(Exception $e) {
  153. $this->sError = '<strong>FOUT: </strong><br>Bericht: '.$e->getMessage().' op regel '.$e->getLine();
  154. }
  155. }
  156.  
  157. /**
  158. * The namevalidator
  159. * if succesfull we will save it in $this->aUser['naam']
  160. *
  161. * @param string
  162. * @return boolean
  163. **/
  164.  
  165. final protected function _ValidateName($sName) {
  166. if(!empty($sName) && preg_match('/[a-zA-Z0-9]+/', $sName, $aMatch)) {
  167. $this->aUser['naam'] = htmlentities($aMatch[0], ENT_QUOTES);
  168. return true;
  169. } else {
  170. throw new Exception('Validatie gebruikersnaam komt niet overeen!');
  171. return false;
  172. }
  173. }
  174.  
  175. /**
  176. * The passwordvalidator
  177. * if succesfull we will save it in $this->aUser['pass']
  178. *
  179. * @param string
  180. * @return boolean
  181. **/
  182.  
  183. final protected function _ValidatePass($sPass) {
  184. if(!empty($sPass) && preg_match('/([a-zA-Z0-9]{8})/', $sPass, $aMatch)) {
  185. $this->aUser['pass'] = md5($aMatch[0]);
  186. return true;
  187. } else {
  188. throw new Exception('Validatie wachtwoord komt niet overeen!');
  189. return false;
  190. }
  191. }
  192.  
  193. /**
  194. * Fieldsvalidator
  195. *
  196. * @param array
  197. * @return void
  198. **/
  199.  
  200. final protected function _ValidateFields($aFields) {
  201. if(is_array($aFields)) {
  202. $this->aData['fields'] = $aFields;
  203. } else {
  204. throw new Exception('Tabelvelden moeten in een array staan!');
  205. }
  206. }
  207.  
  208. final protected function _ValidateControllers($aControllers) {
  209. if(is_array($aControllers)) {
  210. $this->aData['controllers'] = $aControllers;
  211. } else {
  212. throw new Exception ('Controllers moeten in een array staan!');
  213. }
  214. }
  215. }
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Geen reacties (0)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.064s