Ik ben bezig om classes te leren en daarmee heb ik een registratie script gemaakt misschien heeft er iemand nog nut van:
index.php:
PHP
include "register.class.php";
include "config.php";
$class = new register();
if (!isset($_POST['submit'])) {
$class->register_create_form();
}
$class->register_check();
}
?>
Toon Meer
register.class.php:
PHP
<?php
error_reporting(E_ALL);
class register
{
function register_check()
{
$name = "";
$password = "";
$pass_match = "";
$email = "";
if ((isset($_POST['name'])) && (isset($_POST['password'])) && (isset($_POST['pass_match'])) && (isset($_POST['test']))) {
$name = safe_sql($_POST['name']);
$password = safe_sql($_POST['password']);
$pass_match = safe_sql($_POST['pass_match']);
$email = safe_sql($_POST['test']);
if ($password != $pass_match) {
$this->message("Passwords didnt match.");
}
else {
$query = mysql_query("SELECT username FROM users WHERE username='{$name}'") or die($this->message("sql error"));
$row = mysql_num_rows($query);
if ($row > 0) { $this->message("Username already taken."); }
else {
$query = mysql_query("SELECT email FROM users WHERE email='{$email}'") or die($this->message("sql error"));
$row = mysql_num_rows($query);
if ($row > 0) { $this->message("email adress already taken."); }
else {
$pass = md5($password);
$sql = "INSERT INTO users (user_id, username, password, email) VALUES (null, '{$name}', '{$pass}', '{$email}')";
mysql_query($sql) or die($this->message("sql error"));
$this->message("Registration succesfull.");
}
}
}
}
else {
$this->message("Not all fields were filled.");
}
}
function register_create_form ()
{
echo('
<form method="POST" action="'.$_SERVER['PHP_SELF'].'">
<p>Username: <input type="text" name="name" size="20"><br>
Password: <input type="text" name="password" size="20"><br>
Repeat password: <input type="text" name="pass_match" size="20"><br>
Email address: <input type="text" name="test" size="20"><br>
<input type="submit" value="Submit" name="submit"><input type="reset" value="Reset" name="rest"></p>
</form>
');
}
function message($text)
{
echo($text);
}
}
function safe_sql ($text)
{
return htmlspecialchars(mysql_real_escape_string($text));
}
?>
Toon Meer
en dan nog config.php:
PHP
$config = array();
$config['host'] = "localhost";
$config['database'] = "xxxx";
$config['username'] = "xxxx";
$config['password'] = "xxxx";
if (($config["host"]) && ($config["database"]) && ($config["username"]) && ($config["password"])) {
mysql_connect($config["host"],$config["username"], $config["password"]) or die(mysql_error());
mysql_select_db($config["database"]) or die(mysql_error());
}
else {
die("Configuration failed.");
}
Toon Meer
commetaar op het script mag geleverd worden mits ik er ook echt is aan heb