par
Michel6359 » 13 janv. 2013, 19:51
RE
Bon j'ai essayé la solution proposé par
Perine , mais cela ne fonctionne, jai essayé avec htmlentities , rien à faire , je suis usé avec ce problème .
Code : Tout sélectionner
<?php
header("content-type: application/json; charset=utf-8");
// Version avec PDO
$conn = new PDO('mysql:host=localhost;dbname=***', '**', '***');
function cleanup_magic_quotes() {
if (!get_magic_quotes_gpc()) {
return;
}
array_walk_recursive($_GET, 'cleanup_magic_quotes_array');
array_walk_recursive($_POST, 'cleanup_magic_quotes_array');
array_walk_recursive($_REQUEST, 'cleanup_magic_quotes_array');
array_walk_recursive($_COOKIE, 'cleanup_magic_quotes_array');
}
function cleanup_magic_quotes_array(&$value) {
$value = stripslashes($value);
}
cleanup_magic_quotes();
$currentTimestamp = time();
$lastTimestamp = (isset($_GET['lastTimestamp'])) ? $_GET['lastTimestamp'] : ($currentTimestamp-1);
if (!empty(htmlentities($_POST['pseudo'])) && !empty(htmlentities($_POST['message'])))
{
/* Attention jamais faire un htmlspecialchars() sur une variable avant mise en bd cela donne un gros risque de coruption de valeur
$message = htmlspecialchars ($_POST['message']);
$pseudo = htmlspecialchars($_POST['pseudo']); */
//mysql_query("SET NAMES UTF8");
$sth = $conn->prepare("INSERT INTO minichat(pseudo,message,timestamp) VALUES(:pseudo, :message, :timestamp)");
/* pour sécuriser une variable en pdo cette méthode est meilleur
$sth->bindParam(":pseudo", $pseudo);
$sth->bindParam(":message", $message);
$sth->bindParam(":timestamp", $currentTimestamp); */
htmlentities($_POST['pseudo'], ENT_QUOTES, "UTF-8");
htmlentities($_POST['message'], ENT_QUOTES, "UTF-8");
//htmlentities ($_POST['message'], ENT_QUOTES);
//$sth->bindValue('pseudo', (htmlentities($_POST['pseudo'])),PDO::PARAM_STR);
//$sth->bindValue('message',$_POST['message'],PDO::PARAM_STR);PDO::PARAM_INT
//$sth->bindValue('message',(htmlentities($_POST['message'])),PDO::PARAM_STR);
$sth->bindValue('timestamp',$currentTimestamp,PDO::PARAM_INT);
$sth->execute();
}
/* $sth = $conn->prepare("SELECT * FROM minichat WHERE timestamp > :lastTimestamp ORDER BY id DESC LIMIT 0,10");
cette ligne ne peut pas fonctionner, car on ne mélange jamais du pdo avec du mysql c'est soit l'un soit l'autre
$conn-> mysql_query("SET NAMES UTF8"); */
//$sth = $conn->query("SET NAMES UTF8");
$sth = $conn->prepare("SELECT * FROM minichat WHERE timestamp > :lastTimestamp ORDER BY id DESC LIMIT 0,10");
$sth->bindParam(":lastTimestamp", $lastTimestamp);
$sth->execute();
$result_utf8 = $sth->fetchAll(PDO::FETCH_ASSOC);
if(count($result_utf8) > 0)
ini_set("zlib.output_compression", "On");
echo json_encode (htmlspecialchars(stripslashes($result_utf8)));
?>
RE
Bon j'ai essayé la solution proposé par [b]Perine [/b], mais cela ne fonctionne, jai essayé avec htmlentities , rien à faire , je suis usé avec ce problème . 8-|
[code]
<?php
header("content-type: application/json; charset=utf-8");
// Version avec PDO
$conn = new PDO('mysql:host=localhost;dbname=***', '**', '***');
function cleanup_magic_quotes() {
if (!get_magic_quotes_gpc()) {
return;
}
array_walk_recursive($_GET, 'cleanup_magic_quotes_array');
array_walk_recursive($_POST, 'cleanup_magic_quotes_array');
array_walk_recursive($_REQUEST, 'cleanup_magic_quotes_array');
array_walk_recursive($_COOKIE, 'cleanup_magic_quotes_array');
}
function cleanup_magic_quotes_array(&$value) {
$value = stripslashes($value);
}
cleanup_magic_quotes();
$currentTimestamp = time();
$lastTimestamp = (isset($_GET['lastTimestamp'])) ? $_GET['lastTimestamp'] : ($currentTimestamp-1);
if (!empty(htmlentities($_POST['pseudo'])) && !empty(htmlentities($_POST['message'])))
{
/* Attention jamais faire un htmlspecialchars() sur une variable avant mise en bd cela donne un gros risque de coruption de valeur
$message = htmlspecialchars ($_POST['message']);
$pseudo = htmlspecialchars($_POST['pseudo']); */
//mysql_query("SET NAMES UTF8");
$sth = $conn->prepare("INSERT INTO minichat(pseudo,message,timestamp) VALUES(:pseudo, :message, :timestamp)");
/* pour sécuriser une variable en pdo cette méthode est meilleur
$sth->bindParam(":pseudo", $pseudo);
$sth->bindParam(":message", $message);
$sth->bindParam(":timestamp", $currentTimestamp); */
htmlentities($_POST['pseudo'], ENT_QUOTES, "UTF-8");
htmlentities($_POST['message'], ENT_QUOTES, "UTF-8");
//htmlentities ($_POST['message'], ENT_QUOTES);
//$sth->bindValue('pseudo', (htmlentities($_POST['pseudo'])),PDO::PARAM_STR);
//$sth->bindValue('message',$_POST['message'],PDO::PARAM_STR);PDO::PARAM_INT
//$sth->bindValue('message',(htmlentities($_POST['message'])),PDO::PARAM_STR);
$sth->bindValue('timestamp',$currentTimestamp,PDO::PARAM_INT);
$sth->execute();
}
/* $sth = $conn->prepare("SELECT * FROM minichat WHERE timestamp > :lastTimestamp ORDER BY id DESC LIMIT 0,10");
cette ligne ne peut pas fonctionner, car on ne mélange jamais du pdo avec du mysql c'est soit l'un soit l'autre
$conn-> mysql_query("SET NAMES UTF8"); */
//$sth = $conn->query("SET NAMES UTF8");
$sth = $conn->prepare("SELECT * FROM minichat WHERE timestamp > :lastTimestamp ORDER BY id DESC LIMIT 0,10");
$sth->bindParam(":lastTimestamp", $lastTimestamp);
$sth->execute();
$result_utf8 = $sth->fetchAll(PDO::FETCH_ASSOC);
if(count($result_utf8) > 0)
ini_set("zlib.output_compression", "On");
echo json_encode (htmlspecialchars(stripslashes($result_utf8)));
?>
[/code]