PHP » Cookies and Sessions » session_start()

boolean session_start()

Starts a session.

This function assigns a session ID to the current session and prepares storage for session variables. If the browser passes a session ID as a cookie or otherwise, session_start() will resume that session. Otherwise it will create a new session ID. The session ID is usually stored as a cookie in the user's browser, but can also automatically be sent with all local URLs. When a session has been started, session variables can be registered and accessed throughout the remainder of the session.

There are many options that can modify the behavior of the session functions in the configuration file php.ini. A few important ones are listed here:

session.save_handler: How the session information will be stored: files, shared memory, or user defined handlers. The latter can be used for database storage, for example.
session.gc_maxlifetime: The max time of inactivity before a session expires and is deleted.
session.use_cookies: Whether cookies should be used to pass the session ID.
session.auto_start: Whether a session should automatically be started at the initial request from a browser.

Read more about how you can access variables through the predefined autoglobals in the section about Predefined Variables.



print "My session ID is: " . session_id();

My session ID is: 38d945017d073b001e14a8864d6564e6

This code shows how a session is started, and what a session ID can look like.

See Also: