0.0.8 2013-07-23 11:30 UTC


This library emulates Apahce's mod_uid or Nginx's User ID module. For the idea of User ID, visit these links.

Target Users

People who

  • use Apache HTTP server,
  • use PHP by Apache module and
  • don't use mod_uid Apache module

Are you using Apache 1.x.x? or 2.0.x? Visit mod_uid page.

Are you using Nginx? Visit HttpUseridModule page.


Add below to your composer.json:

"require": {
    "castanet/userid": "*"

And then execute

composer install



require 'vendor/autoload.php';
$uid = new \Castanet_Userid;

You need to call \Castanet_Userid::start() as early as possible because it uses setrawcookie() function internally and setrawcookie() occurs error if you have output any string already.

Apache log

Add custome log format to httpd.conf like this:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{uid_got}n\" \"%{uid_set}n\"" combined_cookie

And set combined_cookie as current log format, then restart Apache.

uid_got and uid_set variables are set by this library.



Castanet_Userid does nothing even start() is called unless you call enable()(or setEnabled(true)).

$uid = new Castanet_Userid;
$uid->start(); // do nothing
$uid->enable()->start(); // do something

Name(key of cookie)

name is used as key of cookie. By default, uid is used:


You can modify it by setConfig() method:

$uid->setConfig('name', 'castanet');

Then key of cookie is modified:


Be careful not to use the same key already used for other purpose.


service is an arbitrary integer and defaults to IP address of server PHP is processed(in practice, calculated by ip2long()). It appears as first eight characters on log. Let it, for instance,, and logs noted by this library(as uid_got and uid_set) start with characters 0100007F.

Characters themselves mean nothing but they play a role of identity. By seeing it, you may know the first server given user accessed. So you might have set the same value for multiple server load-balanced by one reverse proxy.

You can set service like this:

$uid->setConfig('service', ip2long(''));

Cookie attributes

Cookie attributes(domain, path and expires) are able to be set by setConfig() method:

$uid->setConfig('domain', '')
    ->setConfig('path', '/sandbox');

Setting at a time

Using setConfigs() method, you can set properties above at a time:

    'name'    => 'castanet',
    'service' => ip2long(''),
    'domain'  => '',
    'path'    => '/sandbox'

Merged uid

Now Castanet Userid notes a note uid in addition to uid_set and uid_got. It is the value of either exists uid_set or uid_set and it doesn't have key name(uid=).


For PHP 4, Use Castanet_Userid4.