The Length of PHP Session ID


You may wonder how many characters php session id consists of. I also needed to know the size of the session_id() in order to decide on the field size of my mysql table column. (I was planning to store session id in the database for a reason).

The answer is strlen(session_id()) => 26 according to my local php server. But;

in fact, it depends on the two php.ini settings:

  • hash_function
  • hash_bits_per_character
php.ini session.hash_function session.hash_bits_per_character

session.hash_function & session.hash_bits_per_character

With playing with these settings, I obtained the following chart:

session_id()

session_id()

For those who wonder how I obtained this chart, here is the php source code and demo page:

[php]
“;
$result .= “
session_id() – PHP Version ” . phpversion() . “

“;
$result .= “
 session.hash_function(0)session.hash_function(1)

“;
for($bit = 4; $bit <= 6; $bit++) { $result .= "$bit

“;

for($func = 0; $func < 2; $func++) { $result .= "” . id($func, $bit) . “

“;
}
$result .= “

“;
}
$result .= “

“;

echo $result;

function id($func, $bit) {
ini_set(“session.hash_function”, $func);
ini_set(“session.hash_bits_per_character”, $bit);
session_regenerate_id();
return session_id() . “(” . strlen(session_id()) . “)”;
}
?>
[/php]

Conclusion

As a result, I decided to set the length of my mysql table column to 40, which will store php session id.

  1. #1 by Omar on 21 May 2011 - 09:45

    Thanks, very helpful!

(will not be published)

Please leave these two fields as-is: