Kod: register.php
Ett stycke php-kod:
<?php
session_start();
if(isset($_POST['register']))
{
include_once('/class.register.php');
$register = new Register();
if($register->process())
echo "Du är nu medlem!";
else
$register->show_errors();
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<form method="POST" action="">
<table>
<tr><td>Användarnamn:</td><td><input type="text" name="user"/></td></tr>
<tr><td>Lösenord:</td><td><input type="password" name="pass"/></td></tr>
</table>
<input type="hidden" name="token" value="<?php echo $token;?>"/>
<input type="submit" name="register" value="Bli medlem!"/>
</form>
Kod: class.register.php
Ett stycke php-kod:
<?php
class Register
{
private $username;
private $password;
private $salt;
private $passmd5;
private $dynsalt;
private $passcrypted;
private $errors;
private $token;
public function __construct()
{
$this->errors = array();
$this->username = $this->$_POST['user'];
$this->username = strtolower($this->username);
$this->username = ucwords($this->username);
$this->password = $this->$_POST['pass'];
$this->token = $_POST['token'];
$this->salt = "enstorjavlahink";
$this->passmd5 = md5($this->password);
$this->dynsalt = sha1($this->username.$this->passmd5.$this->salt);
$this->passcrypted = sha1($this->dynsalt);
}
public function process()
{
if($this->valid_token() && $this->valid_data())
$this->register();
return count($this->errors)? 0 : 1;
}
public function register()
{
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("databas") or die (mysql_error());
mysql_query("INSERT INTO users(username,password) VALUES ('{$this->username}','{$this->passcrypted}')");
if(mysql_affected_rows()< 1)
$this->errors[] = 'Hmm något är fel';
}
public function user_exists()
{
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("databas") or die (mysql_error());
$data = mysql_query("SELECT ID FROM users WHERE username = '{$this->username}'");
return mysql_num_rows($data)? 1 : 0;
}
public function show_errors()
{
echo "<h3>Fel:</h3>";
foreach($this->errors as $key=>$value)
echo $value."<br>";
}
public function valid_data()
{
if($this->user_exists())
$this->errors[] = 'Användarnamnet är upptaget';
if (!preg_match('/^[a-z0-15]{3,10}$/i', $this->username))
$this->errors[] = 'Du får bara ha bokstäver och siffror i användarnamnet.<br/>Och det måste vara mellan 3 och 10 tecken.';
if(empty($this->username))
$this->errors[] = 'Ogiltigt användarnamn';
if(empty($this->password))
$this->errors[] = 'Ogiltigt lösenord';
return count($this->errors)? 0 : 1;
}
public function valid_token()
{
if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
$this->errors[] = 'Haxxor fel';
return count($this->errors)? 0 : 1;
}
}
?>
Errors:
Notice: Undefined property: Register::$nils in C:xampphtdocs estclass.register.php on line 19
Notice: Undefined property: Register::$bengt in C:xampphtdocs estclass.register.php on line 23
Och inget skrivs till databasen.
Tillägg av David 2011-03-09 22:27
Pastebin länkar:
Register.php
http://pastebin.com/C34cfimj
Class.register.php:
http://pastebin.com/E7R7FJeW
Pastebin länkar:
Register.php
http://pastebin.com/C34cfimj
Class.register.php:
http://pastebin.com/E7R7FJeW
Ingen status