Jump to content
gorski

Prevenirea injectiilor SQL folosind PDO Prepared Statements

Recommended Posts

1. Ce este PDO?

PDO, un acronim pentru PHP Data Objects, este o extensie PHP care poate fi folosita ca un nivel de abstractizare pentru conexiunea dintre programele PHP si diverse baze de date. PDO defineste o interfata simpla si consistenta intre diverse baze de date. Pentru fiecare din driverele specifice implementate in PDO se pot folosi functiile specifice. PDO ofera un nivel de abstractizare pentru accesarea datelor, ceea ce inseamna ca indiferent la ce baza de date suntem conectati, se folosesc aceleasi functii pentru a trimite interogari si pentru a primi date. PDO nu ofera un nivel complet de abstractizare, nici transformare mapare automata in obiecte, nu rescrie SQL si nici nu emuleaza caracteristici absente in unele baze de date.

2. Conexiunea la baza de date

$conexiune = new PDO("mysql:host=$db_host;$dbname=$db_name", $db_user, $db_pass);

Unde:

mysql - driver-ul folosit pentru a ne conecta la baza de date

$db_host = 'localhost';

$db_name = 'numele bazei de date';

$db_user = 'userul cu care va conectati la server-ul MySQL';

$db_pass = 'parola cu care va conectati la server-ul MySQL';

Modul default pentru erori este PDO::ERRMODE_SILENT. Ca sa-l schimbam si sa si afisam erorile(in caz ca sunt), punem totul intr-o structura try/catch.

try {
$conexiune = new PDO("mysql:host=$db_host;$dbname=$db_name", $db_user, $db_pass);
$conexiune->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOExpcetion $e) {
echo: 'Eroare: ' . $e->getMessage();
}

Pentru mai multe detalii, intrati aici: PHP: Errors and error handling - Manual

3. Extragerea datelor

Sunt 2 moduri prin care putem extrage date din baza de date si anume:

a) query

B) execute

a) QUERY(nerecomandata)

$username = $_POST['username'];
try {
$conexiune = new PDO("mysql:host=$db_host;$dbname=$db_name", $db_user, $db_pass);
$conexiune->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = $conexiune->query('SELECT * FROM tabela WHERE username = ' . $conexiune->quote($username));
foreach($data as $row) {
print_r($row);
}
} catch(PDOExpcetion $e) {
echo: 'Eroare: ' . $e->getMessage();
}

B) EXECUTE(recomandata)

$id = 13;
try {
$conexiune = new PDO("mysql:host=$db_host;$dbname=$db_name", $db_user, $db_pass);
$conexiune->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conexiune->prepare('SELECT * FROM tabela WHERE id = :id');
$statement->bindParam(':id', $id, PDO::PARAM_INT);
$statement->execute();
while($row = $statement->fetch()) {
print_r($row);
}
} catch(PDOExpcetion $e) {
echo: 'Eroare: ' . $e->getMessage();
}

4. Cateva exemple

a) Executii multiple

try {
$conexiune = new PDO("mysql:host=$db_host;$dbname=$db_name", $db_user, $db_pass);
$conexiune->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conexiune->prepare('INSERT INTO tabela VALUES(:username)');
$statement->bindParam(':username', $username,);
$username = "gogu";
$statement->execute();
$username = "goaga";
$statement->execute();
} catch(PDOExpcetion $e) {
echo: 'Eroare: ' . $e->getMessage();
}

B) INSERT

try {
$conexiune = new PDO("mysql:host=$db_host;$dbname=$db_name", $db_user, $db_pass);
$conexiune->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conexiune->prepare('INSERT INTO tabela VALUES(:username)');
$statement->execute(array(
':username' => 'gogu'
));
} catch(PDOExpcetion $e) {
echo: 'Eroare: ' . $e->getMessage();
}

c) UPDATE

$id = 13;
$username = "gogu";
try {
$conexiune = new PDO("mysql:host=$db_host;$dbname=$db_name", $db_user, $db_pass);
$conexiune->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conexiune->prepare('UPDATE tabela SET username = :username WHERE id = :id');
$stmt->execute(array(
':id' => $id,
':username' => $username
));
} catch(PDOException $e) {
echo: 'Eroare: ' . $e->getMessage();
}

d) DELETE

$id = 13;
try {
$conexiune = new PDO("mysql:host=$db_host;$dbname=$db_name", $db_user, $db_pass);
$conexiune->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conexiune->prepare('DELETE FROM tabela WHERE id = :id');
$statement->bindParam(':id', $id);
$statement->execute();
} catch(PDOException $e) {
echo: 'Eroare: ' . $e->getMessage();
}

Link-uri utile:

PHP: PDO - Manual

php-pdo-wrapper-class - Minimal extension for PHP's PDO class designed for ease-of-use and reducing development time/effort when accessing a database. - Google Project Hosting

by HackYard

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...