Mammouth du PHP |
19672 Messages
19 avr. 2007, 09:43
Ok, alors voici le code du cookie. Il s'agit à la base d'un panier persistant (pour aider à la compréhension), mais en fin de compte, on se fout du contenu :
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Cookie</title>
</head>
<body>
<script type="text/javascript">
/* <![CDATA[ */
function cookiePanier()
{
// attributs.
this.enabled = false;
// méthodes.
this.Set = setCookie;
this.Get = getCookie;
this.Del = delCookie;
this.DelAll = delAllCookies;
}
function getCookieVal(offset)
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
{
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name)
{
var arg = name +"=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i<clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
{
return getCookieVal (j);
}
i = document.cookie.indexOf(" ",i) + 1;
if (i == 0)
{
break;
}
}
return null;
}
function SetCookie (name, value)
{
// un cookie a besoin d'un nom, d'une valeur, d'un nom de domaine, d'une date d'expiration
//
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : '127.0.0.1';
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name +"="+ escape(value)+
((expires == null) ? "" : ("; expires="+ expires.toGMTString()))+
((path == null) ? "" : ("; path="+ path))+
((domain == null) ? "" : ("; domain="+ domain))+
((secure == true) ? "; secure" : "");
}
function DeleteCookie (nom)
{
var exp = new Date();
alert('Timestamp actuel : '+ exp);
exp.setTime (exp.getTime() - 10000000);
alert('Validité modifiée : '+ exp);
var cval = GetCookie (nom);
document.cookie = nom +"="+ cval +"; expires="+ exp.toGMTString();
}
function Set(nom, valeur, jours)
{
var date_exp = new Date();
date_exp.setTime(date_exp.getTime() + (jours * 24 * 3600 * 1000));
alert(valeur);
SetCookie(nom, valeur, date_exp);
}
function Get(nom)
{
alert("Cookie stocké :\nNom : cookie;\nValeur : "+ this.GetCookie(nom));
}
function Bye(nom)
{
SetCookie(nom, "Le panier est vide.");
DeleteCookie(nom);
alert("cookie supprimé.");
}
/* ]]> */
</script>
<form id="testcookie" method="post" action="./">
<fieldset>
<input type="hidden" value="a%3A1%3A%7Bi%3A0%3Ba%3A4%3A%7Bi%3A0%3Bs%3A5%3A%2200001%22%3Bi%3A1%3Bs%3A68%3A%22TAILLEFINE%7CFiz%7CBoisson%20gazeuse%20rafra%EEchissante%20aux%20agrumes%20PRIX%20CHOC%22%3Bi%3A2%3Bs%3A1%3A%221%22%3Bi%3A3%3Bs%3A4%3A%224.15%22%3B%7D%7D" name="panier" id="panier" />
<input type="button" value="Stocker un cookie" onclick="Set('paniertest', unescape(document.getElementById('panier').value), 365);" />
<input type="button" value="Voir le cookie" onclick="Get('panier');" />
<input type="button" value="Supprimer" onclick="Bye('panier');" />
</fieldset>
</form>
</body>
</html>
Le contenu est envoyé en dur. Le résultat donne ceci :
Code : Tout sélectionner
Nom : paniertest
Contenu : a%3A1%3A%7Bi%3A0%3Ba%3A4%3A%7Bi%3A0%3Bs%3A5%3A%2200001%22%3Bi%3A1%3Bs%3A68%3A%22TAILLEFINE%7CFiz%7CBoisson%20gazeuse%20rafra%EEchissante%20aux%20agrumes%20PRIX%20CHOC%22%3Bi%3A2%3Bs%3A1%3A%221%22%3Bi%3A3%3Bs%3A4%3A%224.15%22%3B%7D%7D
Hôte : 127.0.0.1
Chemin : /CodesDivers/
Envoi pour : Tout type de connexion
Expire : vendredi 18 avril 2008 09:30:00
Voici maintenant la seconde page que j'utilise pour essayer de lire le même cookie en PHP :
<?php
echo('<?xml version="1.0" encoding="iso-8859-1"?>' . "\n");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php
if(isset($_COOKIE['paniertest']))
{
?>
<pre>
<?php
var_dump($_COOKIE['paniertest']);
?>
</pre>
<?php
}
?>
</body>
</html>
Et comme résultat, j'ai une page blanche, aussi bien sous IE que sous Firefox...
Codez en pensant que celui qui maintiendra votre code est un psychopathe qui connait votre adresse 