Tutorial - Guide PHP


Increase the time of a session in PHP

Tracking user activities

There are many situations when you need to keep track of user interactions with your website. Tracking user activities is helpful in many ways, such as determining a user's preferences or detecting fraud. The most common way to track users is by using cookies. However, cookies are limited in scope and can't be easily deleted by the end user. The alternative is to use PHP sessions, which provide a much more flexible way of tracking user interactions.

Understanding PHP Sessions

A PHP session provides a way to save data across multiple requests. Each request has its own connection with the server, so the data stored in the session cannot be corrupted by a crashed connection. This makes sessions very reliable when storing critical application data. Each request is treated as a new session when interacting with a session, so no data is lost when closing active sessions. Sessions are especially useful for online gaming where data loss can lead to lost games and frustrated players.
It's also easy to determine how long a given session should last. Sessions are non-persistent by default, which means they don't persist after the web server has responded to the request that initiated the session. However, it's easy to include persistent attributes within your code to make sessions persistent by default. This ensures that any data stored in a session will be available across future requests without requiring modification of server code. It's also possible to set expiration dates for individual items within a session; this allows you to automatically delete old data when necessary.

Improving the performance of a PHP Session

To improve performance, it's a good idea to allow long PHP sessions to timeout after a short period of inactivity. This prevents unused sessions from preventing subsequent actions from occurring on the server. It's also useful to limit how many sessions you open at once. Opening too many sessions causes the server to become overloaded and slow down performance for all users. Limiting how many sessions your server can handle effectively limits resource utilization, offering better response times for all users.

Sessions are an easy way to keep track of user data without resorting to cookies or limiting server resources. Setting appropriate duration parameters for your sessions prevents old information from slowing down future requests and keeps your application responsive enough for all users. Limiting the number of open sessions reduces server load and makes your website more accessible as well. Session-based programming is easy when using PHP!

Increasing the time of a PHP Session

A simple method to increase our time of a session started on any PHP page without changing the configuration of our server is by using the PHP "ini_set" function.

The PHP ini_set function helps us to set the values of some internal configuration options in PHP during the time the script is running. At the end of our PHP script this configuration option will be invalid.

To increase the time of a session started in PHP we will use the ini_set function as follows:

ini_set("session.cookie_lifetime","7200");
ini_set("session.gc_maxlifetime","7200");
session_start();

This must be placed at the beginning of the PHP files we want to configure.

Increase the time of a PHP session on a shared hosting CPanel

We must create a directory in the root directory of our shared hosting: ".folder_sessions", that is, before the public_html directory, which is where we have hosted all our shared domains.

Then, in the PHP files where you use the sessions add the following:

# Session timeout 86400 = 24 hours
ini_set('session.save_path', '/home/server/.folder_sessions');
ini_set('session.gc_maxlifetime', 86400);
ini_set('session.cookie_lifetime', 86400);
# session cache_expire time       
ini_set('session.cache_expire', 960);
ini_set('session.name', 'my_domain');
session_start();

Increasing PHP session time with an .htaccess file

The .htaccess file is generally included in the root directory of our domain, always in Apache servers, and serves to configure our domain as to restrict access to directories or files that are included in the server, improve the security of a web page, create fictitious directories, redirect pages, or in this case, increase the session time of a domain.

php_value session.save_path /home/server/.folder_sessions
php_value session.gc_maxlifetime 86400
php_value session.cookie_lifetime 86400
php_value session.cache_expire 86400
php_value session.name my_domain

 

Canonical URL: Increase the time of a session in PHP