I have a small problem. So, I have this site where people need to write emails to each other. There are some who type their emails fast, and there are others who do that slow.
My site uses sessions, pure sessions. No cookie is set. Sessions have a tendency to expire after a certain period of time, when no movement from page to page is done.
Today I received a couple of nasty emails from those people who type their emails slow because they got my “Access Denied” message. Yes, the message is not very descriptive and I need to work on it.
My question is: how do I set a cookie… or whatever else I need to do to make sure that slow typing visitors are not pissed off at me every time they got that message?
If the solution is simple enough, I would appreciate a ready code for me to insert on the page. There was a time (3 months ago) when a ready sitepoint’s book on PHP and MySQL. Unfortunately, after that I read two more books on JavaScript and … well, our brain tends to get rid of the data which is not used frequently. So, I don’t remember much of PHP. Help.
Whilst you properly develop a solution, a quick fix might be to have a small bit of JS that uses an AJAX call to another php script to keep the current session alive.
Take wander over to the manual, there’s a wealth of information over there.
Each time you call session_start() in your PHP code, the time on the session is updated so that it won’t expire while in use.
The idea here is to create an image tag <img> on the page, dynamically, and refresh that image every 2 minutes with JavaScript.
The source of the image isn’t a static file though, it’s a PHP script. All this PHP script needs to do is access the session so that its last-access time gets updated.
As long as the user has the browser window open, this image keeps refreshing, and their session stays alive no matter how long they stay on the page. When they really do leave the page, the script isn’t running anymore so after ~24 minutes the session will get deleted like usual.
So put this on the “compose e-mail” page, or whatever pages you want to keep a session alive between refreshes on. Make “dummypage.php” the script that just touches the session. Ideally you’d also want it to return an image, which you can do by saving a 1x1 transparent or white image and sending it –
Thank you for a more deep explanation. Will do just that and let’s see if it works. Thank you to all. The sitepoint administration should do that following. Each person should have some kind of e-wallet and every time someone helps someone, we could kind of contribute with I don’t know, a couple of bucks or so. This is not much but still kind of pleasant, don’t you think? Thank you all.
I personally don’t like a solution which calls a page in the background just to keep a session lifetime… That’s not really good practice in my opinion. I don’t want to offend someone, everybody has there own opinions and views
Why not set a longer life time suggested by risoknop???
People seemed to have just ignored that one…
on this email form page, before session_start(); you could call the
session_set_cookie_params function to increase the lifetime for the session on that page… This function only has effect for one time page load. so this new lifetime only counts for that mail form page and it won’t affect other pages:
$expireTime = 60*60*24; // a whole day
session_set_cookie_params($expireTime);
session_start();
this will give them a whole day to write an email…
after they finish and a different page loads, then the 1 day gets overwritten with the default hour (3600 seconds)
The only reason I don’t like to suggest setting the session time limit to a long duration is that people do not logout and may not close their browser, so you leave a user’s session open for longer than it needs to be, presenting a security risk. Someone can just go to the website later and have full access as that user.
@Alex:
Easiest way is that you can just create a test page with everything above (no form needed) and post the link here. One of us should be able to figure out the problem in a second.
You can do it yourself, however. Two potential issues are:
The JavaScript code is not running for some reason.
The ‘dummypage.php’ file is in a different directory, so the page doesn’t exist as far as the script is concerned.
If you have Firefox, try installing HttpFox. It shows you HTTP requests that happen, and you can check to see whether the dummy page is being loaded successfully. Open the HttpFox window (after installing), hit the “Start” button, and then type in “dummy” into the filter textbox in the top toolbar. Items should appear in the list if it’s working properly, and it should show “200” under the “Result” column.
I suggest lowering the interval to test by the way, unless you want to sit there bored out of your mind.