Jump to content

Timp rămas până în 2025

La mulți ani tuturor!

[PHP-OOP] Register


Guest Theramore
 Share

Recommended Posts

Guest Theramore

Un mic script pt inregistrare, securizat cat de cat, Object Oriented, facut de mine daca sunt intrebari nu ezitati:)

 

in config.php:

 

<?php
$cfg['host'] = "localhost";
$cfg['db_user'] = "root";
$cfg['db_pass'] = "";
$cfg['db'] = "db";

mysql_connect($cfg['host'], $cfg['db_user'], $cfg['db_pass']) or die(mysql_error());
mysql_select_db($cfg['db']) or die(mysql_error());
?>

 

 

register.php

 

<?php
include("class.register.php");
$register = new Register();


$captcha = mt_rand(1000,9999);
?>
<center>
<fieldset script_blocat'width: 350px;'>
<legend>Register</legend>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='POST'>
<table>
	<tr>
		<td>
			Full name:
		</td>
		<td>
			<script_blocat='text' name='name' class="input-wait ui-widget ui-widget-content ui-corner-all defaultText" script_blocat"padding: 3px; width: 180px;">
		</td>
	</tr>
			<tr>
		<td>
			E-Mail:
		</td>
		<td>
			<script_blocat='text' name='email' class="input-wait ui-widget ui-widget-content ui-corner-all defaultText" script_blocat"padding: 3px; width: 180px;">
		</td>
	</tr>
	<tr>
		<td>
			Username:
		</td>
		<td>
			<script_blocat='text' name='username' class="input-wait ui-widget ui-widget-content ui-corner-all defaultText" script_blocat"padding: 3px; width: 180px;">
		</td>
	</tr>
	<tr>
		<td>
			Password:
		</td>
		<td>
			<script_blocat='password' name='password' class="input-wait ui-widget ui-widget-content ui-corner-all defaultText" script_blocat"padding: 3px; width: 180px;">
		</td>
	</tr>
			<tr>
		<td>
			Retype-Password:
		</td>
		<td>
			<script_blocat='password' name='password2' class="input-wait ui-widget ui-widget-content ui-corner-all defaultText" script_blocat"padding: 3px; width: 180px;">
		</td>
	</tr>
	<tr>
		<td>
			Country:
		</td>
		<td>
			<script_blocat='text' name='country' class="input-wait ui-widget ui-widget-content ui-corner-all defaultText" script_blocat"padding: 3px; width: 180px;">
		</td>
	</tr>
	<tr>
		<td>
			City:
		</td>
		<td>
			<script_blocat='text' name='city' class="input-wait ui-widget ui-widget-content ui-corner-all defaultText" script_blocat"padding: 3px; width: 180px;">
		</td>
	</tr>
	<tr>
		<td>
			Birthday:
		</td>
		<td>
			<select name='d'>
			<option value=''>Day</option>
			<?php
			$i = 1;
			while($i <= 31){
				echo "<option value='".$i."'>".$i."</option>";
				$i++;
			}
			?>
			</select>
			<select name='m'>
			<option value=''>Month</option>
			<?php
			$i = 1;
			while($i <= 12){
				echo "<option value='".$i."'>".$i."</option>";
				$i++;
			}
			?>
			</select>
			<select name='y'>
			<option value=''>Year</option>
			<?php
			$i = 1960;
			$year = date('Y');
			while($year >= $i){
				echo "<option value='".$year."'>".$year."</option>";
				$year = $year - 1;
			}
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td>
			Captcha(<?php echo $captcha; ?>):
		</td>
		<td>
			<script_blocat='text' name='insertcap' class="input-wait ui-widget ui-widget-content ui-corner-all defaultText" script_blocat"padding: 3px; width: 180px;">
			<script_blocat='hidden' name='hiddencap' value='<?php echo $captcha; ?>'>
		</td>
	</tr>
</table>
<script_blocat='submit' name='submit' class="input-wait ui-widget ui-widget-content ui-corner-all defaultText" script_blocat"padding: 3px; width: 180px;" value='Register'>
</form>
</fieldset>
</center>

<?php
if(isset($_POST['submit'])){

if($register->messages()){


}else{
	$register->showerrors();
}

}

?>

 

 

class.register.php:

 

<?php

error_reporting(E_ALL);
include("config.php");

class Register {
   private $username;
   private $password;
   private $password2;
   private $passmd5;
   private $bd;
   private $city;
   private $country;
   private $name;
   private $email;
   private $errors;
   private $insertcap;
   private $hiddencap;

   public function __construct()
   {
       $this->submit = isset($_POST['submit'])? 1 : 0;

       if ($this->submit) {
           $this->username = $this->filter($_POST['username']);
           $this->password = $this->filter($_POST['password']);
           $this->password2 = $this->filter($_POST['password2']);
           $this->passmd5 = md5($this->password);
           $this->email = $this->filter($_POST['email']);
           $this->city = $this->filter($_POST['city']);
           $this->country = $this->filter($_POST['country']);
           $this->bd = $_POST['d'] . "-" . $_POST['m'] . "-" . $_POST['y'];
           $this->name = $this->filter($_POST['name']);
           $this->insertcap = $this->filter($_POST['insertcap']);
           $this->hiddencap = $this->filter($_POST['hiddencap']);
       }
       $this->errors = array();
   }

   public function messages()
   {
       echo $this->dataLength();
       try {
           if (!$this->allfields())
               throw new Exception("<div class='ui-widget'><div class='ui-state-error ui-corner-all' script_blocat'padding: 0 .7em;'><p><span class='ui-icon ui-icon-alert' script_blocat'float: left; margin-right: .3em;'></span><strong>Error:</strong> Please fill in all fields!</p></div></div>");
           if (!$this->passwordMatch())
               throw new Exception("<div class='ui-widget'><div class='ui-state-error ui-corner-all' script_blocat'padding: 0 .7em;'><p><span class='ui-icon ui-icon-alert' script_blocat'float: left; margin-right: .3em;'></span><strong>Error:</strong> The passwords do not match!</p></div></div>");

           if (!$this->userCheck())
               throw new Exception("<div class='ui-widget'><div class='ui-state-error ui-corner-all' script_blocat'padding: 0 .7em;'><p><span class='ui-icon ui-icon-alert' script_blocat'float: left; margin-right: .3em;'></span><strong>Error:</strong> That user is already in use!</p></div></div>");

           if (!$this->dataLength())
               throw new Exception("<div class='ui-widget'><div class='ui-state-error ui-corner-all' script_blocat'padding: 0 .7em;'><p><span class='ui-icon ui-icon-alert' script_blocat'float: left; margin-right: .3em;'></span><strong>Error:</strong> The username must be between 5 and 22 characters and must contain only numbers, letters and _ !</p></div></div>");

           if (!$this->captchamatch())
               throw new Exception("<div class='ui-widget'><div class='ui-state-error ui-corner-all' script_blocat'padding: 0 .7em;'><p><span class='ui-icon ui-icon-alert' script_blocat'float: left; margin-right: .3em;'></span><strong>Error:</strong> Invalid captcha code!</p></div></div>");

		//Aici vin liniile care insereaza datele in baza de date!

           echo "success!";
       }

       catch(Exception $e) {
           $this->errors[] = $e->getMessage();
       }
   }

   public function filter($var)
   {
       return preg_replace('/[^a-zA-Z0-9-.-@-_]/', ' ', $var);
   }

   public function userCheck()
   {
       $query = mysql_query("SELECT * FROM users WHERE username = '{$this->username}' ");
       $r = mysql_num_rows($query);
       return ($r == 0)? 1 : 0;
   }

   public function dataLength()
   {
       return preg_match('/^([a-zA-Z0-9._]){5,12}+$/', $this->username)? 1 : 0;
   }

   public function passwordMatch()
   {
       return($this->password == $this->password2)? 1 : 0;
   }

   public function captchamatch()
   {
       return($this->insertcap == $this->hiddencap)? 1 : 0;
   }

   public function allfields()
   {
       return(!empty($this->username) && !empty($this->password) && !empty($this->password2) && !empty($this->email) && !empty($this->bd) && !empty($this->city) && !empty($this->country) && !empty($this->name) && !empty($this->insertcap))? 1 : 0;
   }

   public function success()
   {

   }

   public function showerrors()
   {
       foreach($this->errors as $k => $v) {
           echo "<br /><center><fieldset script_blocat'width: 350px;'><legend>Error</legend>" . $v . "</fieldset></center>";
       }
   }
}

?>

Link to comment
Share on other sites

Guest Theramore

dap imi place sa postez tutoriale:P o sa mai pun :)

 

Te-ai inregistrat doar ca sa postezi asta? Interesant.

 

Nivel tutorial: Incepator Mediu Avansat

Link to comment
Share on other sites

Guest Theramore

Mai nu stiu daca or mai exista la fel insa ala e scris de mine pentru un site la care lucrez si l am pus si aici :) cum am zis daca e vreo nelamurire in cod intreba ma si o sa incerc sa iti explic cat de bine pot :)

 

puteti vizualiza site-ul meu //editat si un model cu acel script //editat nu este complet functional, nu am pus query-ul care trimite datele in db insa poti verifica cateva din functii, acolo incerc sa fac un site de socializare am ceva de lucru :D:)

 

Am si un login facut in acelasi mod (OOP) dupa ce il termin vi-l dau si voua :P

 

Ma gandesc daca sa il imbin si cu java sau nu sa il fac mai dinamic, sa nu mai stea utilizatorul sa reincarce pagina la fiecare submit e stresant :)

 

Da, arata bine tutorialul, bravo ! Sper sa nu fie copiat de pe alte forumuri .

 

//Edited by SkiizO: Daca mai faci reclama primesti warn up, citeste regulamentul sectiunii.

Edited by Big Al
Link to comment
Share on other sites

Guest Theramore

am pus un link catre exemplu macar pe ala nu il poti lasa? ti se pare a fii reclama?

asta nu suport eu la forumurile astea...RECLAMA , am facut reclama...wow:O :|

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.