PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
error_reporting(E_ALL);
class Register
{
private $_register;
private $_errors;
private $_username;
private $_password;
private $_password2;
private $_passmd5;
private $_mail;
private $_token;
public function __construct()
{
$this->_register = isset($_POST['register'])? 1 : 0;
if(isset($_POST['register']))
{
$this->_username = $this->filter($_POST['username']);
$this->_password = $this->filter($_POST['password']);
$this->_password2 = $this->filter($_POST['password2']);
$this->_mail = $this->filter($_POST['mail']);
$this->_token = $_POST['token'];
$this->_passmd5 = md5($this->_password);
$this->_ip = $_SERVER['REMOTE_ADDR'];
$ths->_data = date('Y-m-D');
}
$this->_errors = array();
}
public function filter($var)
{
return preg_replace('/[^a-zA-Z0-9]/','',$var);
}
public function insert()
{
mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
mysql_select_db("romsilva") or die(mysql_error());
$query = mysql_query("INSERT INTO users VALUES('', '0', '0', '0', '0', '0')") or die(mysql_error());
}
public function register()
{
if($this->messages())
{
$this->insert();
}
else
$this->showerrors();
}
public function passleng()
{
return(strlen($this->_password) > 6 && strlen($this->_password) < 25)? 1 : 0;
}
public function messages()
{
try{
if(!$this->allfields())
throw new Exception("Te rog completeaza toate campurile!");
if($this->istokenvalid())
throw new Exception("Formular nevalidat! Te rog completeaza din nou formularul fara a da refresh paginii!");
if(!$this->database())
throw new Exception("Utilizatorul exista deja!");
if(!$this->chkpass())
throw new Exception("Cele doua parole nu se potrivesc");
if(!$this->passleng())
throw new Exception("Parola trebuie sa fie intre 6 si 25 de caractere!");
echo "Succes!";
}
catch(Exception $e)
{
$this->_errors[] = $e->getMessage();
}
}
public function chkpass()
{
return($this->_password==$this->_password2)? 1 : 0;
}
public function allfields()
{
return($this->_username&&$this->_password&&$this->_password2&&$this->_mail)? 1 : 0;
}
public function database()
{
mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
mysql_select_db("romsilva") or die(mysql_error());
$data = mysql_query("SELECT * FROM users WHERE username = '{$this->_username}' ");
if(mysql_num_rows($data)==0)
{
return true;
}
else
return false;
}
public function istokenvalid()
{
return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
}
public function showerrors()
{
foreach($this->_errors as $key=>$value)
{
echo $value."<br>";
}
}
}
?>
<?php
function trySomething($i)
{
if ($i != 0)
{
throw new Exception('i is not 0');
}
else
{
echo $i;
}
}
//
$x = 1;
try
{
trySomething($x);
}
catch (Exception $e)
{
echo $e->getMessage();
}
?>
In the function showerrors() try testing the $this->_errors variable to see if it is an array, this will help with troubleshooting where the problem is.
<?php
error_reporting(E_ALL);
class Register {
private $_register;
private $_errors;
private $_username;
private $_password;
private $_password2;
private $_passmd5;
private $_mail;
private $_token;
private $_insertcaptcha;
private $_cap;
public function __construct()
{
$this->_register = isset($_POST['register'])? 1 : 0;
if (isset($_POST['register'])) {
$this->_username = $this->filter($_POST['username']);
$this->_password = $this->filter($_POST['password']);
$this->_password2 = $this->filter($_POST['password2']);
$this->_mail = $this->filter($_POST['mail']);
$this->_token = $_POST['token'];
$this->_passmd5 = md5($this->_password);
$this->_ip = $_SERVER['REMOTE_ADDR'];
$this->_data = date('Y-m-D');
$this->_insertcaptcha = $_POST['insertcaptcha'];
$this->_cap = $_POST['cap'];
}
$this->_errors = array();
}
public function filter($var)
{
return preg_replace('/[^a-zA-Z0-9]/', '', $var);
}
public function insert()
{
mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
mysql_select_db("romsilva") or die(mysql_error());
$query = mysql_query("INSERT INTO users (id, username, password, admin, lastip, lastlogin) VALUES('', '0', '0', '0', '0', '0')") or die(mysql_error());
}
public function register()
{
if ($this->messages()) {
$this->insert();
} else
$this->showerrors();
}
public function passleng()
{
return(strlen($this->_password) > 6 && strlen($this->_password) < 25)? 1 : 0;
}
public function messages()
{
try {
if (!$this->allfields())
throw new Exception("Te rog completeaza toate campurile!");
if ($this->istokenvalid())
throw new Exception("Formular nevalidat! Te rog completeaza din nou formularul fara a da refresh paginii!");
if (!$this->database())
throw new Exception("Utilizatorul exista deja!");
if (!$this->chkpass())
throw new Exception("Cele doua parole nu se potrivesc");
if (!$this->passleng())
throw new Exception("Parola trebuie sa fie intre 6 si 25 de caractere!");
if (!$this->captcha())
throw new Exception("Cod captcha incorect!");
echo "Succes!";
}
catch(Exception $e) {
$this->_errors[] = $e->getMessage();
}
}
public function chkpass()
{
return($this->_password == $this->_password2)? 1 : 0;
}
public function captcha()
{
return($this->_cap == $this->_insertcaptcha)? 1 : 0;
}
public function allfields()
{
return($this->_username && $this->_password && $this->_password2 && $this->_mail)? 1 : 0;
}
public function database()
{
mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
mysql_select_db("romsilva") or die(mysql_error());
$data = mysql_query("SELECT * FROM users WHERE username = '{$this->_username}' ");
if (mysql_num_rows($data) == 0) {
return true;
} else
return false;
}
public function istokenvalid()
{
return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
}
public function showerrors()
{
foreach($this->_errors as $key => $value) {
echo $value . "<br>";
}
}
}
?>
What you should do is run through all functions being called and either echo something random (so you know that the code's going to right way), or output various data that you need at points to check if it's actually there.
However, take another look at your messages function. It does not return a value, thus it will always be false, so rather than calling the insert function it'll call the showerrors function instead. You'll have to either return true or false depending on the conditions the validation is in.
Also, the way your current exceptions are set up won't work, PHP will leave the try catch block once the an exception has been thrown, you'll have to either try catch them individually or reconsider whether you want to use exceptions or just normal string type error messages.
public function messages()
{
try {
if (!$this->allfields())
throw new Exception("Te rog completeaza toate campurile!");
if ($this->istokenvalid())
throw new Exception("Formular nevalidat! Te rog completeaza din nou formularul fara a da refresh paginii!");
if (!$this->database())
throw new Exception("Utilizatorul exista deja!");
if (!$this->chkpass())
throw new Exception("Cele doua parole nu se potrivesc");
if (!$this->passleng())
throw new Exception("Parola trebuie sa fie intre 6 si 25 de caractere!");
if (!$this->captcha())
throw new Exception("Cod captcha incorect!");
mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
mysql_select_db("romsilva") or die(mysql_error());
$query = mysql_query("INSERT INTO users (id, username, password, admin, lastip, lastlogin) VALUES('', '0', '0', '0', '0', '0')") or die(mysql_error());
echo "Succes!";
}
catch(Exception $e) {
$this->_errors[] = $e->getMessage();
}
}