Need help with destroying sessions

Hello Experts.
This question relates to a question is posed many months ago regarding switch between 2 languages on my website. The system functions very well, however, I am having trouble destroying the session when the user leaves the site.

Below is the code I have in the head of my pages to set the default language and to reset the language when a  link is clicked. I have also included the code used for the language selection links.

The problem is that as it is... I am often left with sometimes over 100 session files in the root directory of my website, (1 for every visit). It is usuall a randomly named file with no extension.

e.g. sess_a6cc781948ef79c0303caf413a6d6a38

I have tried several option from the PHP manual to try to prevent the remnant session files form building up in my root directory, but nothing seems to work. The only way I can find that prevents these files from being saved is the use the simple session_destroy() function at the end of the script in the head of the page, but this cause the default language to be reset every time the user navigates to another page, instead of retaining their chosen language.

How can I keep the language setting intact until the user leaves, and them delete the the session file?

Any ideas?

<?php session_start();

  $_SESSION['language'] = $_GET['lang'];  

 $language = $_SESSION['language'];
  //sets the default language to english
 $_SESSION['language'] = 'en';
  $language = $_SESSION['language'];


<a href="<?php echo $_SERVER['PHP_SELF'] ?>?lang=en" class="small">English</a> | <a href="<?php echo $_SERVER['PHP_SELF'] ?>?lang=vn" class="small">Ti¿ng Vi¿t</a>

Open in new window

Who is Participating?
Lukasz ChmielewskiCommented:
Linux hosting ? crontab with a php file deleting the folder contents

Windows ? Use batch script orvbs to delete files
Lukasz ChmielewskiCommented:

I believe there is no need to do that - you may as well ignore the files or move the session dir to another folder (by default it resides in /tmp). These are just text files and you do not have to worry about them as much as you do.
You could try doing this with a cookie. That way there is no need to store a file on the server. The data being stored is not sensitive, so there should not be a security concern.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Mark BradyPrincipal Data EngineerCommented:
Another easy way is to store the language setting for each user in mysql. that way when they login you can quickly access their language preference and set it that way. On the other hand, if you don't want to destroy a whole session, just unset the session variables that you are setting (not the language one).

Look at any forms you have and anything posted via a form which get stored in $_SESSION['name']; for each posted element. For eg if you have posted the following:

$first_name = $_POST['first']; // or $_GET or $_REQUEST
$last_name = $_POST['last'];

When you have finished using the data unset them like this.

unset($_SESSION['last']; // etc.....

Hope this gives you another idea to think about. Put simply, if you have your users login, then store their language setting in the database and retrieve it when they login.
Personally I use cookies.. But some sessions contain far too much data for cookies.

So given that I would use a cronjob to clear out old sessions every day

find /tmp -atime +2 -name 'sess_*' -exec rm -f {} \;

The above code will clear any old (48 hours) sessions out of the temp folder

-atime means last accessed time
+2 means greater than 2 days, by default the time modifiers use days
-name means macth this name
'sess_*'  means a wildcard - YOU MUST use single quotes otherwise the shell will parse it
-exec means execute the following command
{} is the filename and path of any matching files
\; means the end of the command

SaigonJeffAuthor Commented:
Roads Roads

Your suggestion works great as a means of gathering all those temp files using the session_save_path(); function... Great tip, but is there a way to have the temp directory emptied on a daily or weekly basis so I don't have to go in a do it manually?
SaigonJeffAuthor Commented:
Thanks for the great help! Really helped.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.