Andmete muutmine [CS Server]

Vaata eelmist teemat Vaata järgmist teemat Go down

Tonight
Tonight

Andmete muutmine [CS Server] Vetera11
Liitus : 19/02/2012
Postitusi : 16518

PostitamineTonight on 30.08.12 19:47

Andmete muutmine [CS Server] Coolte10

Database
Kood:
CREATE TABLE `andmed` (
  `ID` int(75) NOT NULL AUTO_INCREMENT,
  `kasutajanimi` varchar(15) NOT NULL,
  `parool` varchar(45) NOT NULL,
  `email` varchar(45) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;  

class.mysql.php
Kood:
<?php
$host="";
$username="";
$password="";
$database="";

mysql_connect($host,$username,$password) or die(mysql_error());
@mysql_select_db($database) or die(mysql_error());
?>

class.register.php
Kood:
<?php

class Register
{
  private $username;
  private $password;
  private $password2;
  private $passmd5;
  private $email;
  private $email2;

  private $errors;
  private $rtoken;

  public function __construct()
  {
    $this->errors = array();

    $this->username = $this->filter($_POST['ruser']);
    $this->password = $this->filter($_POST['rpass']);
 $this->password2 = $this->filter($_POST['rpass2']);
    $this->email    = $this->filter($_POST['remail']);
 $this->email2 = $this->filter($_POST['remail2']);
   $this->rtoken    = $_POST['rtoken'];

    $this->passmd5  = md5($this->password);
  }

  public function process()
  {
    if($this->valid_rtoken() && $this->valid_data())
         $this->register();

    return count($this->errors)? 0 : 1;
  }

  public function filter($var)
  {
    return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
  }
  public function register()
  {
   mysql_query("INSERT INTO andmed(kasutajanimi,parool,email) VALUES ('{$this->username}','{$this->passmd5}','{$this->email}')");

   if(mysql_affected_rows()< 1)
     $this->errors[] = '<font color="red">Andmebaasi viga</font>';
  }

  public function user_exists()
  {
    $data = mysql_query("SELECT ID FROM andmed WHERE kasutajanimi = '{$this->username}'");

    return mysql_num_rows($data)? 1 : 0;
  }
  
  public function email_exists()
  {
    $data = mysql_query("SELECT ID FROM andmed WHERE email = '{$this->email}'");

    return mysql_num_rows($data)? 1 : 0;
  }

  public function show_errors()
  {
    echo "";

    foreach($this->errors as $key=>$value)
      echo $value."<br>";
  }

  public function valid_data()
  {
    if($this->user_exists())
      $this->errors[] = '<font color="red">Kasutajanimi juba kasutuses</font>';
    if($this->email_exists())
      $this->errors[] = '<font color="red">E-post juba kasutuses</font>';
    if(empty($this->username))
      $this->errors[] = '<font color="red">Vigane kasutajanimi</font>';
    if(empty($this->password))
      $this->errors[] = '<font color="red">Vigane parool</font>';
    if($this->password != $this->password2)
      $this->errors[] = '<font color="red">Paroolid ei ühti</font>';
    if(empty($this->email) || !eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$',$this->email))
      $this->errors[] = '<font color="red">Vigane e-post</font>';
 if($this->email != $this->email2)
      $this->errors[] = '<font color="red">E-postid ei ühti</font>';

  return count($this->errors)? 0 : 1;
  }


  public function valid_rtoken()
  {
   if(!isset($_SESSION['rtoken']) || $this->rtoken != $_SESSION['rtoken'])
     $this->errors[] = '<font color="red">Kontrollige andmeid</font>';

   return count($this->errors)? 0 : 1;
  }
}

?>

class.login.php
Kood:
<?php

class Login
{
  private $_id;
  private $_kasutajanimi;
  private $_parool;
  private $_passmd5;

  private $_errors;
  private $_access;
  private $_login;
  private $_token;

  public function __construct()
  {
    $this->_errors = array();
    $this->_login  = isset($_POST['login'])? 1 : 0;
    $this->_access = 0;
    $this->_token  = $_POST['token'];

    $this->_id       = 0;
    $this->_kasutajanimi = ($this->_login)? $this->filter($_POST['kasutajanimi']) : $_SESSION['kasutajanimi'];
    $this->_parool = ($this->_login)? $this->filter($_POST['parool']) : '';
    $this->_passmd5  = ($this->_login)? md5($this->_parool) : $_SESSION['parool'];
  }

  public function isLoggedIn()
  {
    ($this->_login)? $this->verifyPost() : $this->verifySession();
    return $this->_access;
  }

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

  public function verifyPost()
  {
    try
    {
      if(!$this->isTokenValid())
         throw new Exception('<font color="red">Vigane vorm esitatud</font>');

      if(!$this->isDataValid())
         throw new Exception('<font color="red">Valed andmed esitatud</font>');

      if(!$this->verifyDatabase())
         throw new Exception('<font color="red">Kasutajanimi ja parool ei klapi</font>');

    $this->_access = 1;
    $this->registerSession();
    }
    catch(Exception $e)
    {
      $this->_errors[] = $e->getMessage();
    }
  }

  public function verifySession()
  {
    if($this->sessionExist() && $this->verifyDatabase())
       $this->_access = 1;
  }

  public function verifyDatabase()
  {
    $data = mysql_query("SELECT id FROM andmed WHERE kasutajanimi = '{$this->_kasutajanimi}' AND parool = '{$this->_passmd5}'");
 
   if(mysql_num_rows($data))
      {
        list($this->_id) = @array_values(mysql_fetch_assoc($data));
        return true;
      }
    else
      { return false; }
  }

  public function isDataValid()
  {
    return (preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_kasutajanimi) && preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_parool))? 1 : 0;
  }

  public function isTokenValid()
  {
    return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
  }

  public function registerSession()
  {
    $_SESSION['id'] = $this->_id;
    $_SESSION['kasutajanimi'] = $this->_kasutajanimi;
    $_SESSION['parool'] = $this->_passmd5;
 mysql_query("INSERT INTO logid(kasutaja) VALUES ('{$this->_kasutajanimi}')");
  }

  public function sessionExist()
  {
    return (isset($_SESSION['kasutajanimi']) && isset($_SESSION['parool']))? 1 : 0;
  }

  public function showErrors()
  {
    echo "";

    foreach($this->_errors as $key=>$value)
      echo $value."<br>";
  }
}

?>

index.php
Kood:
<?php
session_start();
include 'class.mysql.php';
include 'class.login.php';
?>
<?php

if(isset($_POST['login'])) {

  $login = new Login();

  if($login->isLoggedIn())
     header('location: sees.php');
  else
   $login->showErrors();
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));

$login = new Login();

if($login->isLoggedIn()) {
 header('location: sees.php');
}
else
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width='300'>
 <tr><td>Kasutajanimi:</td><td><input type="text" name="kasutajanimi" /></td></tr>
 <tr><td>Parool:</td><td><input type="password" name="parool" /></td></tr>
</table>
<input type="hidden" name="token" value="<?php echo $token;?>" />
<input type="submit" name="login" value="Sisene" />
</form>
<a href="registreeru.php">Registreeru</a>

registreeru.php
Kood:
<?php
session_start();
include('class.mysql.php');
include('class.login.php');
include('class.register.php');

if(isset($_POST['register'])) {

  $register = new Register();

  if($register->process())
    echo "<font color='green'>Liitumine õnnestus</font>";
  else
    $register->show_errors();
}
$rtoken = $_SESSION['rtoken'] = md5(uniqid(mt_rand(),true));

$login = new Login();

if($login->isLoggedIn()) {
 header('location: sees.php');
}
else
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <table width='300'>
  <tr><td>Kasutajanimi:</td><td><input type="text" name="ruser"/></td></tr>
  <tr><td>Parool:</td><td><input type="password" name="rpass"/></td></tr>
  <tr><td>Parool uuesti:</td><td><input type="password" name="rpass2"/></td></tr>
  <tr><td>E-post:</td><td><input type="text" name="remail"/></td></tr>
  <tr><td>E-post uuesti:</td><td><input type="text" name="remail2"/></td></tr>
 </table>
 <input type="hidden" name="rtoken" value="<?php echo $rtoken;?>"/>
 <input type="submit" name="register" class="register" value="Registreeru"/>
</form>

sees.php
Kood:
<?php
session_start();
include('class.mysql.php');
include('class.login.php');

$login = new Login();

if($login->isLoggedIn()) {
$order = "SELECT * FROM andmed WHERE kasutajanimi ='".$_SESSION['kasutajanimi']."'";
$result = mysql_query($order);

while($andmed = mysql_fetch_row($result)){
echo "<li>Tere <b>".$_SESSION['kasutajanimi']."</b></li>";
echo "<a href='logoff.php'>Logi välja</a>";
 }
} else
 header('location: index.php');
?>

logoff.php
Kood:
<?php
session_start();

session_destroy();
header('location: index.php');
?>


Viimati muutis seda Tonight (31.12.13 12:39). Kokku muudetud 2 korda

Vaata eelmist teemat Vaata järgmist teemat Tagasi üles

Soovid reklaamidest vabaneda?

Selleks logi sisse või tee endale kasutaja.

Liitu foorumiga

Tee endale kasutaja ning osale aruteludes.


Tee uus kasutaja

Logi sisse

Omad foorumis juba kasutajat? Logi sisse.


Logi sisse

 
Permissions in this forum:
Sa ei saa vastata siinsetele teemadele