Cookies or sessions

Dear all,

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.

Alex.

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. :wink:

Quick fix:
[highlight=‘javascript’]
var img = new Image();
img.src = “dummypage.php?”;
with (img.style) {
position = “absolute”;
top = “-100px”;
left = “-100px”;
}
document.body.appendChild(img);
setInterval(function() {
img.src = img.src.replace(/\?./, “?” + (new Date()).getTime());
}, 1000 * 60 * 2 /
2 minutes */);

Interesting, I like that.

Wow, thanks a bunch. Where do I put that? I know, I know, I watch too much. Seriously, where? On what page?

What page should I use instead of so called “dummypage.php”? Please, bear with my stupidity.

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 –

<?php

session_start();

header("Content-type: image/png");
readfile("transparent.png");

?>

Nice, simple solution sk89q! :tup:

Well, can’t you just set sessions not to expire that soon? Set them to be alive for few hours at least.

Take a look here: http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes

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? :slight_smile: Thank you all.

Okay, just to make sure that I did everything right:

  1. On my “compose-email.php” page I inserted the given JavaScript code.

var img = new Image();
img.src = "dummypage.php?";
with (img.style) {
    position = "absolute";
    top = "-100px";
    left = "-100px";
}
document.body.appendChild(img);
setInterval(function() {
    img.src = img.src.replace(/\\?.*/, "?" + (new Date()).getTime());
}, 1000 * 60 * 2 /* 2 minutes */);

  1. I created a “dummypage.php” script and inserted this into it:

<?php 

session_start(); 

header("Content-type: image/png"); 
readfile("transparent.png"); 

?>

  1. And I created a simple image, called “transparent.png” and put it into the same directory as my “dummypage.php”.

Is that really all that I need? I will make a test now. Let’s see how long the session will be kept alive.

Please correct me if I made something silly.

Thank you, you people rock!

Nope, it did not work for me. My send-email form is built with iframes.

Try putting the script on the main page, and on the page loaded in the iframe.

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:

  1. The JavaScript code is not running for some reason.
  2. 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.

I do get the alert message every 30 seconds. Does that mean everything should work?

Try HttpFox, as I outlined in my updated post.

Will do that immediately and let you know the results.

Here is what I done, just to make sure I followed your steps precisely:

  1. Installed HTTP Fox.
  2. Went to my site.
  3. Visited “compose-email.php” page.
  4. Opened HTTP Fox

In the top part of the HTTP Fox window, among other pages I found a request to “dummypage.php?”

When I highlighted it, right column at the bottom of HTTP Fox has this:

(Status-Line) HTTP/1.1 200 OK
Date Thu, 25 Mar 2010 05:49:23 GMT
Server Apache
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma no-cache
Keep-Alive timeout=15, max=96
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type image/png

Does that mean that everything is good? or should by good?

Just so you know, image was created and placed in the same folder as my dummypage.php. Image of .png format.

Hurray! It worked for me! Thanks to all and everyone. You must be tired of hearing me thanking you but thank you anyways! :lol: