The
session_start(
) function is used to create a new
session. A session is unique to the interaction between a browser and a web
database application. If you use your browser to access several sites at once,
you'll have several unrelated sessions. Similarly, if several users access your
application each has their own session. However, if you access an application
using two browsers (or two browser windows) at the same time, in most cases the
browsers will share the same session; this can lead to unpredictable
behavior—that's the reason why many web sites warn against it.
The
session identifier
generated by PHP is a random string of 32 hexadecimal digits, such as
fcc17f071bca9bf7f85ca281094390b4. When a new session is started, PHP
creates a session file, using the session identifier, prefixed with
sess_, for the filename. For example, the filename associated with our
example session ID on a Unix system is
/tmp/sess_fcc17f071bca9bf7f85ca281094390b4.
The
session_start(
) function is also used to find an existing session. If a call is made to
session_start( ), and a session has previously been started, PHP attempts
to find the session file and initialize the session variables. PHP does this
automatically by looking for the session cookie in the browser request whenever
you call
session_start( ). You don't need to do anything different when
starting a new session or restoring an existing one. Even if the identified
session file can't be found,
session_start( ) simply creates a new
session file.