Can you change session cookie timeout in php.ini for me? Print

  • 0

Yes, you can change the session cookie timeout in your php.ini file by updating two main directives:

🔧 Step-by-step Instructions

  1. Locate your php.ini file:

    • On most systems, it's in /etc/php/[version]/apache2/php.ini or /etc/php.ini.

    • Run php --ini in the terminal to find the correct file if you're unsure.

  2. Open php.ini in a text editor:

    bash
     
    sudo nano /etc/php.ini

    (Replace the path with the actual one from your system if different.)

  3. Edit the session timeout settings:

    Add or modify the following lines:

    ini
     
    session.gc_maxlifetime = 3600 session.cookie_lifetime = 3600
    • session.gc_maxlifetime: This is the number of seconds after which data will be seen as 'garbage' and cleaned up by PHP's garbage collection.

    • session.cookie_lifetime: This sets how long the cookie should live on the client side (in seconds). If set to 0, the cookie expires when the browser closes.

    Example: 3600 seconds = 1 hour.

  4. Save and close the file.

  5. Restart your web server:

    • For Apache:

      bash
       
      sudo systemctl restart apache2
    • For Nginx with PHP-FPM:

      bash
       
      sudo systemctl restart php-fpm

 

Tip for Application-Level Override

If you don't have access to php.ini, you can add this to your script:

php
ini_set('session.gc_maxlifetime', 3600); ini_set('session.cookie_lifetime', 3600); session_start();

 


Was this answer helpful?

« Back