2

I want to delete session files automatically in PHP.

I came to know that I have to change the following configurations in PHP.ini

  • session.gc_probability
  • session.gc_divisor
  • session.gc_maxlifetime

But I am not sure that what value to change in these properties.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Renganathan M G
  • 5,039
  • 2
  • 31
  • 35
  • 1
    If you keep the PHP save session path to default, PHP should automatically clear them up. If you change the path, you have to clean it yourself. – Clarkey Aug 26 '14 at 08:52
  • What do you mean automatically? After a certain time period or when the user logs out? – Chris Aug 08 '15 at 15:12
  • [This question](https://stackoverflow.com/questions/654310/cleanup-php-session-files) has useful information about the automatic session file cleanup. – T30 May 25 '17 at 10:00

2 Answers2

1

Probably this example will help you

 <?php 
 // you have to open the session to be able to modify or remove it 
 session_start(); 

 // to change a variable, just overwrite it 
 $_SESSION['variable_name']='variable_value'; 

 //you can remove a single variable in the session 
 unset($_SESSION['variable_name']); 

 // or this would remove all the variables in the session, but not the session itself 
 session_unset(); 

 // this would destroy the session variables 
 session_destroy(); 
 ?> 
Valeriy Gorbatikov
  • 3,459
  • 1
  • 15
  • 9
  • 1
    Usually for removing the session files, there is a CRON that deletes them. Do you have full access to the server? – Chris Aug 08 '15 at 15:13
-1

Using session_destroy(); function, created session files deleted from automatically.

Rakesh Singh
  • 1,250
  • 9
  • 8