Skip to content

Instantly share code, notes, and snippets.

@ktomk
Created October 7, 2011 15:02
Show Gist options
  • Save ktomk/1270461 to your computer and use it in GitHub Desktop.
Save ktomk/1270461 to your computer and use it in GitHub Desktop.
Validate a PHP session id string
<?php
/**
* validate string session id
*
* @see http://www.devnetwork.net/viewtopic.php?f=34&t=88685#p520259
*
* @param string $sessionId
* @return bool
*/
public function isValidId($sessionId)
{
$strId = (string) $sessionId;
if ($strId !== $sessionId) return FALSE;
// session.hash_bits_per_character: '4' (0-9, a-f), '5' (0-9, a-v), and '6' (0-9, a-z, A-Z, "-", ",")
// session.hash_function: '0' means MD5 (128 bits) and '1' means SHA-1 (160 bits).
// len: 22 (128bits, 6 bits/char), 40 (160bits, 4 bits/char)
return (bool) preg_match('/^[0-9a-zA-Z,-]{22,40}$/', $strId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment