login.php
Code: Zaznacz cały
<?php
session_start();
if(!isset($_POST['login']))
{
include_once("class.login.php");
$login = new Login();
if($login->process())
header (" adf.php");
else
$login->show_errors();
}
?>
<form method= "POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Login:<input type="text" name="uusername"/>
Haslo:<input type="password" name="ppassword"/>
<input type="submit" name="login" value="Zaloguj"/>
</form>
Code: Zaznacz cały
<?php
class Login
{
private $username;
private $password;
private $errors;
public function __construct()
{
$this->errors = array();
$this->username = $this->filter($_POST['uusername']);
$this->password = $this->filter($_POST['ppassword']);
}
public function process()
{
if($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 login()
{
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("media") or die (mysql_error());
mysql_query("SELECT * FROM users(username, password) WHERE ('{$this->username}','{$this->password}')");
if(mysql_affected_rows()<1)
$this->errors[]= 'Nie ma takiego usera';
}
public function show_errors()
{
echo "<h3>Errors</h3>";
foreach($this->errors as $key=>$value)
echo $value."<br>";
}
public function valid_data()
{
if(empty($this->username))
$this->errors[] = 'Invalid Username';
if(empty($this->password))
$this->errors[] = 'Invalid Password';
return count($this->errors)? 0 : 1;
}
}
?>
Pozdrawiam