2

If anyone can help me, it will be of great help to me!

I'm starting in PHP, I made an application that worked fine while it was running on my computer, but when I put it into production on Hostgator, after some time (20 to 30 minutes) it seems that it loses the session, causing a problem in some functions that I run by javascript.

When I refresh the page it goes back to the login page (index.php) with error 2 and I need to login again.

Can anyone tell me how I should handle this error? I need not to lose the session so fast and when I do, be able to identify it before executing the functions in Javascript to redirect to the login page, but when I use console.log() before executing the function, appears as if it has not lost the session. So I don't know how to deal with it.

  • We aren't sure how you are implementing the sessions without your code. Can you post some basic related code so that we can help you out? – Praveen Kumar Purushothaman Mar 14 '22 at 16:50
  • 20-30 minutes is the normal timeout for Sessions. You can [read here](https://stackoverflow.com/questions/8311320/how-to-change-the-session-timeout-in-php) how to change this if you're sure it's the right way for your app. If you need something that lasts longer than 20-30 minutes, maybe you should switch to cookies. – Andrea Olivato Mar 14 '22 at 16:51
  • Would I be right in guessing you are making AJAX calls from a page but the page itself is not being refreshed very often? – RiggsFolly Mar 14 '22 at 16:53
  • If you are ^^^ then make if you are calling scripts on your server by AJAX, make sure these scripts all start a session. This will reset the counter for the timeout – RiggsFolly Mar 14 '22 at 17:11

1 Answers1

2

If you use PHP's default session handling, the only way to reliably change the session duration in all platforms is to change php.ini. Check the settings with a phpinfo();

You can also set in your .htaccess file:

php_value session.gc_maxlifetime 64000
php_value session.cookie_lifetime 64000
php_value session.cache_expire 64000
php_value session.name yourdomain.com

If you use a framework like Laravel, CakePHP, Yii you can configure the session to be stored in the database, Redis, etc. and you have a better control.

Like RiggsFolly said in the comment, many shared hosting packages have limits. Hostgator for example: https://www.hostgator.com/help/article/php-settings-that-cannot-be-changed - but the session gc_maxlifetime can be changed from MultiPHP INI Editor, from the default 1440 sec (24 minutes) to a higher value.

Valeriu Ciuca
  • 2,084
  • 11
  • 14
  • Which if you are using a shared hosting package, you never actually have access to – RiggsFolly Mar 14 '22 at 16:57
  • Perfect Valeriu Ciuca! This helped me solve one of the problems and extend the time that really was in 1440 seconds. Thank you so much! The second problem I needed to solve is to check via javascript if the session was not destroyed? In case it wasn't destroyed I would like to execute the ajax call, if it was destroyed I would like to redirect to index. Is there any way to do this? – Bruno Barbosa Mar 14 '22 at 22:46
  • I am glad it helped. Please upvote the answer. Thx! You can check the PHP session cookie if it exists using javascript. The cookie is usually called: PHPSESSID - A solution for reading the cookie by name with javascript is here: https://stackoverflow.com/a/25490531/4527645 – Valeriu Ciuca Mar 14 '22 at 22:51