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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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.
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

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?
Lukasz ChmielewskiCommented:
Linux hosting ? crontab with a php file deleting the folder contents

Windows ? Use batch script orvbs to delete files

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SaigonJeffAuthor Commented:
Thanks for the great help! Really helped.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.