I decided to build it in the easiest way to use, and I published one of the two classes I made in PHPClasses.
In the UserManager class, in the login method, I made some string checks and if the user is found, I registrate a "session".
* NOTE: The other class is to manipulate (read as emulate) sessions and cookies.
I used the arguments as the same way as setcookie() function does. You registrate a "session value" by sending its name. Detailing more:
$username = "Guilherme Blanco";
$argument = "username";
So, $$argument = "Guilherme Blanco";
So, in the function I use "username" to relate the content and the name of the $username variable, BUT in PHP this has 3 problems (that I can list):
- Global access doesn't refer to the parent class variables, only the global ones and also return me an empty string (should return me a warning, that the variable isn't set). Question: Does exist any way to refer it?
- Using a simple class, and global variable, the global works properly... when sending an array as argument, in the global $$argument;, I have a warning (array to string conversion). Question: Any hack available to work around with it?
My original intention is to store the key/value as in the same way as assigned them in the script escope. For example... If I want to store in a "session" the key/value $username = "Guilherme Blanco", I'll have something like this:
$_SESS['username'] = "Guilherme Blanco";
* Note: $_SESS is an array I created.
Trying to be more specific: How can I get the key name, without send the pair (key/value)?
In PEAR, they suggest to send this: ("username" => "Guilherme Blanco"). My intention is to use only "username" and I can recover the "Guilherme Blanco" inside the function.
I'm trying to solve it in the last 3 days... I didn't find a solution yet. If anyone has another suggestion (that follow my wishes (erm, I mean... that I don't have to send the pair key/value)), I'll apreciate.
For those interested, here is the piece of code that I have the problem:
Code: Select all
function setSessionValue()
{
// This function can set an array (no serialized) as the session values
// or can set multiple values independent. All arguments should be send
// as a string value (even when they're arrays). This will in the same
// way as PHP does in cookie registration
$num_args = func_num_args();
// I have the number of arguments being send
if ($num_args == 0) {
// No value to insert, so return false
return FALSE;
} else {
// Get the previous session variables in the table
$aValues = $this->getSessionValues();
if (!$aValues) { $aValues = array(); }
// I'll insert each variable/array I find in the newSessValues
// array, then I serialize it and try to insert when I have no more
// arguments that the function sent me
$newSessValues = $aValues;
for ($i = 0; $i < $num_args; $i++) {
$arg = func_get_arg($i);
// Checking if the variable exists in the source code
// Return FALSE (bug in the source) is I don't find it
global $$arg;
if (!isset($$arg)) {
// The variable doesn't exist; so, return FALSE
return FALSE;
}
// Checking if the argument is an array or a simple variable
if (is_array($$arg)) {
// I'll merge the arrays. If arrays have the same string
// keys, then the later value for that key will overwrite
// the previous one. This also do the "update" session
$newSessValues = array_merge($newSessValues, $$arg);
} else {
// I'll add the variable in my already existent session
// values. Ths also do the "update" if I found any item
// with the same name inside my session variables
$newSessValues[$arg] = $$arg;
}
}
// Preparing the array to be inserted in the table
$newSessValues = addslashes(serialize($newSessValues));
}
// Putting in the table OLD and NEW values
$query = "UPDATE sessHandler SET sValues='".($newSessValues)."' WHERE sHashId = '".($this->_hash)."' AND sSessId = '".($this->_session)."'";
$result = ($this->_linkId != NULL) ? mysql_query($query, $this->_linkId) : mysql_query($query);
// If the insertion runned okay (I mean, happened wihout any problem),
// I should return TRUE; otherwise, return FALSE
$rows = ($this->_linkId != NULL) ? mysql_affected_rows($this->_linkId) : mysql_affected_rows();
if ($rows > 0) {
return TRUE;
} else {
return FALSE;
}
}Thanks in advance,