Good afternoon Friend. I recently had a similar dilemma. following your code example I solved using the cryptoLib class available at http://cryptolib.ju.je
- First we create the functions
require("path/to/cryptolib.php");
function tokenizer($id){
$token = CryptoLib::encryptData($id, "token");
return $token;
}
// Função simples para decodificar o token recebido após click no elemento
function decodeToken($token){
$decryptedString = CryptoLib::decryptData($token, "token");
return $decryptedString;
}
EXAMPLE: In your html the code can be applied as follows
<td>Dados1</td>
<td>Dados2</td>
<td><img src="e.png" onclick="editar(<? echo tokenizer($row["id"]) ?>)">
<img src="del.png"> onclick="excluir(<? echo tokenizer($row["id"]) ?>)">
</td>
</tr>
<tr>
<td>Dados3</td>
<td>Dados4</td>
<td>
<img src="e.png" onclick="editar(<? echo tokenizer($row["id"]) ?>)">
<img src="del.png"> onclick="excluir(<? echo tokenizer($row["id"]) ?>)">
</td>
</tr>
HTML OUTPUT: In your rendered html the source code will be similar to the example below:
<td>Dados1</td>
<td>Dados2</td>
<td><img src="e.png" onclick="editar("ctnbCH1FXdr41JYI9J82sXjGKbFvIUP3pshgL8KaZsYsEFqFco3NgimLy2xAj")">
<img src="del.png"> onclick="excluir("gimLy2xfUP3pshgL8KaZsYsEFqFco3NAjctnbCH1FXdr41JYI9J82sXjGKbFv")">
</td>
</tr>
<tr>
<td>Dados3</td>
<td>Dados4</td>
<td>
<img src="e.png" onclick="editar("IUPIUP3pshgL8KaZsYsEFqFco3NgimLy2xAj3pshgL8KaZsYsEFqFco3NgimLy2xAj")">
<img src="del.png"> onclick="excluir("8KaZsYsEFqFco3NgctnbCH1FXdr41JYI9J82sXjGKbFvIUP3pshgLimLy2xAj")">
</td>
</tr>
In your CRUD in PHP. Use token validation function:
$id = decodeToken($token);
The function returns the id to use in CRUD manipulation.
Completion:
When calling the function
tokenizer($row["id"]);
// O valor retornado para token
sXjGKbFvIUP3pshgLim8KaZsYsEFqFco3NgctnbCH1FXdr41JYI9J82Ly2xAj
When receiving the token, sent by javascript, calls the function
$id = decodeToken($token);
// se o valor do $row["id"] informado para a função tokenizer() for igual a "99".
// a função retornará o equivalente a:
$id = 99;
Perks:
Every time pages are rendered, the token generated for each $Row{"id"] is unique and random.
I hope this helps to clear your doubts.
Possible duplicate of What is PHP Injection? What is its difference to SQL Injection? And how to avoid it?
– Allan Andrade
Possible duplicate of How to encode a number in PHP
– Guilherme Nascimento
Vitor I think that solves your problem: http://answall.com/a/45580/3635
– Guilherme Nascimento