Prima di tutto ti ringrazio per la risposta Poi passando al dunque:
- Si uso l'id dell'user nelle query ma per prova ho rimosso quella parte e ho provato lo script solo su una riga del db.
- Ho cercato di seguire quello che mi hai detto ma non mi sono ben chiare alcune cose quindi sono andato ad intuito:
il file changepassword.class.php l'ho cambiato così (per comodità ho assegnato io manualmente l'id):
changepassword.class.php
Codice PHP:
<?php
error_reporting(E_ALL);
require('administrator/db_config.php');
class ModifyPassword
{
private $old_pass = '';
private $new_pass1 = '';
private $new_pass2 = '';
private $userid = '';
public function __construct($p1,$p2,$p3,$id)
{
$this->old_pass = $p1;
$this->new_pass1 = $p2;
$this->new_pass2 = $p3;
$this->userid = $id;
}
public function ChangePassword()
{
$this->ErrorReport();
}
protected function IsEmptyField()
{
if($this->old_pass == '' OR $this->new_pass1 == '' OR $new_pass2 == '')
{
return TRUE;
}
else
{
return FALSE;
}
}
protected function VerifyPassword()
{
if($this->new_pass1 == $this->new_pass2)
{
return TRUE;
}
else
{
return FALSE;
}
}
protected function VerifyLengthPassword()
{
if(strlen($this->new_pass2) >= 8 && strlen($this->new_pass2) <= 15)
{
return TRUE;
}
else
{
return FALSE;
}
}
protected function VerifyAlphanumericPassword()
{
if(preg_match('/^[a-zA-Z0-9]+$/', $this->new_pass2))
{
return TRUE;
}
else
{
return FALSE;
}
}
protected function PasswordExists()
{
$sql = "SELECT password FROM users WHERE password='$this->old_pass' AND id='$this->userid'";
$res = mysql_query($sql);
if($row = mysql_fetch_array($res))
{
return TRUE;
}
else
{
return FALSE;
}
}
public function ErrorResult($num)
{
header("Location: change_password.php?alert=" . $num);
die;
}
protected function ErrorReport()
{
if($this->IsEmptyField())
{
$this->ErrorResult(1);
}
if(!$this->VerifyPassword())
{
$this->ErrorResult(2);
}
if(!$this->VerifyLengthPassword())
{
$this->ErrorResult(3);
}
if(!$this->VerifyAlphanumericPassword())
{
$this->ErrorResult(4);
}
if(!$this->PasswordExists())
{
$this->ErrorResult(5);
}
$this->ChangePasswordUser();
}
protected function ChangePasswordUser()
{
$sql = "UPDATE users SET password = '$this->new_pass1' WHERE password='$this->old_pass' AND id='$this->userid'";
$res = mysql_query($sql);
mysql_close();
if($res === TRUE)
{
die ('Password aggiornata con successo!');
}
else
{
die ("Errore con l'\aggiornamento: " . mysql_error());
}
}
}
?>
e il file modify_password.php così (presumo che i controlli sui campi non vadano qui )
modify_password.php
Codice PHP:
<?php
include 'changepassword.class.php';
$obj = new ModifyPassword($pass1,$pass2,$pass3,$userid);
$obj->ChangePassword();
$pass1 = trim(mysql_real_escape_string($_POST['oldpassword']));
$pass2 = trim(mysql_real_escape_string($_POST['password']));
$pass3 = trim(mysql_real_escape_string($_POST['password2']));
$userid = 6;
?>
Inoltre non sono sicuro ma questo:
Codice PHP:
public function __construct($p1,$p2,$p3,$id)
{
$this->old_pass = $p1;
$this->new_pass1 = $p2;
$this->new_pass2 = $p3;
$this->userid = $id;
}
non dovrebbe essere così:
Codice PHP:
public function __construct($pass1,$pass2,$pass3,$userid)
{
$this->old_pass = $pass1;
$this->new_pass1 = $pass2;
$this->new_pass2 = $pass3;
$this->userid = $userid;
}
può essere anche che mi sbagli io vista l'ora