PHP Cookies across multiple sub domains

Here’s how to make the browser store cookies across multiple sub domains.

The example I have used is for my site. The cookie given to
the browser will be used for any * address. The PHP code
creates a new session with a random session id, that session id is
stored in the cookie, and the cookie is sent to the browser. Next time
the browser sends a requests, it will send the cookie that contains the
session id.

Here’s the PHP code:

        // new session id
        session_id( md5 ( uniqid ( microtime () ) ) );
        session_name ( SESSION_NAME );
        // send cookie - change to your domain name
        setcookie( SESSION_NAME, session_id(), 0, '/', '');
        session_id( $_COOKIE[SESSION_NAME] );
        session_name( SESSION_NAME );

Leave a Reply

Your email address will not be published.