PHP session not persistent after server migration

Posted by Lloyd on March 27, 2011 at 8:52 am

Nerd Talk

I remember when we setup an Apache web server to move from Lighttpd, after we installed PHP we just copied over the same php.ini file to use the same settings. During testing I had a problem with sessions not persisting between pages, it was a pain to realize the problem at first. The cause of the issue is with the session.save_path, since we just copied over the old php.ini file, the folder path set to where the session should be saved is, well, not existing. I created the folder, the session folder, set write permissions and sessions are flying across pages!
You can find the session.save_path directive in /etc/php.ini and check out where sessions are saved, ex:

session.save_path = "/var/lib/php/session"

I believe it’s not the best practice to set 0777 (means everyone can read/write) permissions for the sessions folder as other users of the server may be able to hijack session files so be careful on this.

Literally, this is the song that played on my head when things started to work. It’s one of my childhood anime song. Watch and listen to this!

I’m eating bacon and rice for breakfast right now, btw. Just saying ;p

array_count_values for float values 1

Posted by Lloyd on December 11, 2010 at 2:04 pm

Nerd Talk

array_count_values function in PHP can only count integer and string values. At some point you may need to count occurrences of float values in an array but you can’t because array_count_values won’t let you. If you have an array like:

$digits = array(14.5, 5.42, 1.5, 8, 14.5);
$digits = array_count_values($digits);

This will give you a warning that says, Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values!

There’s actually a hack to get around this and achieve counting occurrences of float values. All you need to do is cast all float values in the array as string. array_count_values can also count integers so why not just cast the values as int? You don’t want to do that because casting the float values as int will change the data type to int but also remove the decimal points in your values and make it a whole number. Take this as an example:

$float = 12.8;
var_dump($float);         // float(12.8) - original float value
var_dump((int)$float);    // int(12) - becomes whole number
var_dump(intval($float)); // int(12) - becomes whole number
var_dump((string)$float); // string(4) "12.8" - string type

Based on the example above we need to cast the float values as string. So in the case of the array that we want to count the values, we need to do something like:

$digits = array(14.5, 5.42, 1.5, 8, 14.5);
$newDigits = array();
foreach($digits as $key => $value)
	// cast float as string
	if(is_float($value))
		$newDigits[$key] = (string)$value;
	// for actual int and string values
	else
		$newDigits[$key] = $value;
 
$countValues = array_count_values($newDigits);
print_r($countValues);

The above code will cast all float values in the array as string and array_count_values will work as expected. The output will be:

Array
(
    [14.5] => 2
    [5.42] => 1
    [1.5] => 1
    [8] => 1
)

Hope that helps!

PHP GET/POST variable replacement

Posted by Lloyd on September 18, 2010 at 10:55 am

Nerd Talk

Food for thought, PHP automatically converts dots (‘.’) with underscores (‘_’) on GET or POST (or REQUEST) variables.

// sample url using GET: page.php?foo.bar=value
print_r($_GET);
 
// output:
// variable "foo.bar" becomes "foo_bar"
// array([foo_bar] => value)
echo $_GET['foo_bar']; // prints "value"

I’m still here!! 2

Posted by Lloyd on August 15, 2010 at 7:30 pm

Been busy lately and no time to blog because my brain is always fried. I spent my time this weekend hanging out and meeting few people at Soshified. Other than that, I’ve been coding and thinking all day ^^


^ Back to Top