Created
July 31, 2011 21:03
-
-
Save maximevalette/1117211 to your computer and use it in GitHub Desktop.
Gestion des options utilisateurs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function clearOption($uniqid,$name) { | |
/* Suppression de la valeur en session ainsi que MySQL */ | |
unset($_SESSION['options'][$name]); | |
mysql_query("DELETE FROM users_options WHERE uniqid = '".mysql_real_escape_string($uniqid)."' AND name = '".mysql_real_escape_string($name)."' LIMIT 1"); | |
return true; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getOption($uniqid,$name,$default=false) { | |
$row = new stdClass(); | |
if ($_SESSION['options'][$name] && $uniqid == $_COOKIE['uniqid']) { | |
/* Si la valeur existe en session, on l'utilise */ | |
$row->value = $_SESSION['options'][$name]; | |
} else { | |
/* Sinon on récupère via MySQL et on met en session */ | |
$result = mysql_query("SELECT value FROM users_options WHERE uniqid = '".mysql_real_escape_string($uniqid)."' AND name = '".mysql_real_escape_string($name)."' LIMIT 1"); | |
$row = mysql_fetch_object($result); | |
$_SESSION['options'][$name] = $row->value; | |
} | |
/* Si malgré tout il n'y a rien, on retourne la valeur par défaut */ | |
if (!isset($row->value)) return $default; | |
/* Retour de la valeur */ | |
return $row->value; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getOptions($uniqid) { | |
/* Si les options ne sont pas déjà en session, on les met */ | |
if (!is_array($_SESSION['options'])) { | |
$result = mysql_query("SELECT name,value FROM users_options WHERE uniqid = '".mysql_real_escape_string($uniqid)."'"); | |
while ($row = mysql_fetch_object($result)) { | |
$_SESSION['options'][$row->name] = $row->value; | |
} | |
} | |
return $_SESSION['options']; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Session] | |
session.save_handler = memcache | |
session.save_path = "tcp://127.0.0.1:9000?persistent=1&weight=1&timeout=1&retry_interval=15" | |
session.use_cookies = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function setOption($uniqid,$name,$value) { | |
/* Mise à jour de la session ainsi que de MySQL */ | |
$_SESSION['options'][$name] = $value; | |
mysql_query("REPLACE INTO users_options (uniqid,name,value) VALUES ('".mysql_real_escape_string($uniqid)."','".mysql_real_escape_string($name)."','".mysql_real_escape_string($value)."')"); | |
/* On retourne */ | |
return $value; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `users_options` ( | |
`uniqid` int(11) NOT NULL, | |
`name` varchar(24) NOT NULL, | |
`value` varchar(32) NOT NULL, | |
UNIQUE KEY `uniqid` (`uniqid`,`name`) | |
) ENGINE=MyISAM DEFAULT CHARSET=utf8; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment