Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

session and tmp files:

0 views
Skip to first unread message

Tommy

unread,
Feb 19, 2003, 10:05:13 AM2/19/03
to
I am studying php and while studying sessions I did the following example:

<file1>

<?
class CPerson{
var $name;
var $product_ids;
}
session_start(); // we start a session
session_register("session_name"); // we give it a name
$person=new CPerson();
$person->name=$_SERVER['REMOTE_USER'];
$person->product_ids=array("1","33","45"); // id of items he intend to
buy
$_SESSION['session_name']=$person; // we store the data
?>
<HTML>
<HEAD>
<TITLE>Session1</TITLE>
</HEAD>
<BODY>
<a href="./session2.php">Go to page2</a>
</BODY>
</HTML>

</file1>

Ok, this file starts a session, and stores a (trivial) class containing the
name of the person as well as an array of products in the basket (well sort
of, I kept it simple).

<file2>

<?
class CPerson{
var $name;
var $product_ids;
}
session_start(); // we start a session
session_register("session_name"); // we give it a name
?>
<HTML>
<HEAD>
<TITLE>Session2</TITLE>
</HEAD>
<BODY>
<?
$person=$_SESSION['session_name'];
echo $person->name."<BR>"; // we output the name of the
user
// and the
product in the basket!
foreach($person->product_ids as $item){
echo $item."<BR>";
}
// the "session" remembers...
session_unregister('count');
session_destroy(); // we delete everything!
?>
</BODY>
</HTML>

</file2>

Ok, this file reads the class from the session and prints the name as well
as the products that that person has put in the basket.
It works, but I have 2 questions:

- is this a good implementation or do you have better ideas?!? I am new to
PHP and I am reading the php documentation, but I am afraid of doing things
in a "bad" way!
- as soon as I click on "Go to page2" and page 2 displays the output, the
file from the session folder (tmp folder) disappers! That is what I want of
course (in this case), but if after loading file 1 I close the browser and
then open it again on file 1, I now have 2 session files on the tmp folder.
Now, when I go to page 2 I only delete the latter one and the previous file
will sit on the tmp folder forever!!! What can I do about it?!? Is there a
way to make the session file disapper after, let's say, 30 minutes in which
the user (using that session) is inactive?!?

I thank you very much in advace,

Tommy

Steven Vasilogianis

unread,
Feb 19, 2003, 10:29:09 PM2/19/03
to
On Wed, 19 Feb 2003 15:05:13 +0000 (UTC), "Tommy" <ltk_RE...@libero.it>
wrote:

> It works, but I have 2 questions:
>
> - is this a good implementation or do you have better ideas?

Looked good to me, but you may want to consider a templating system to make
things easier on yourself. Also, as of PHP 4.1.0, it is recommended that use
the $_SESSION superglobal for modifying session variables. See
<http://www.php.net/manual/en/language.variables.predefined.php> for more on
superglobals.

> - as soon as I click on "Go to page2" and page 2 displays the output, the
> file from the session folder (tmp folder) disappers! That is what I want of
> course (in this case),

Althought that is what you want, it should not be happening. It should
be deleted, but not as soon as it displays the output, unless perhaps you have
a ridiculously small value for the session.gc_maxlifetime setting. See
<http://groups.google.com/groups?selm=b2h98f%241cpjdp%241%40ID-153790.news.df
ncis.de&oe=UTF-8&output=gplain> for an explanation.

> but if after loading file 1 I close the browser and
> then open it again on file 1, I now have 2 session files on the tmp folder.

If you were using cookies, the temporary cookie which the session created was
deleted when you closed your browser. If you were passing the session id via a
get parameter, when you closed the browser you probably did not pass the
session id back in when you re-opened the browser.

In either event, php realized there was not an active session present so
started a new one. Every new session gets its own temporary file.

> Now, when I go to page 2 I only delete the latter one and the previous file
> will sit on the tmp folder forever!!! What can I do about it?!? Is there a
> way to make the session file disapper after, let's say, 30 minutes in which
> the user (using that session) is inactive?!?

Are you sure it sits there forever? By default, they last 24 minutes. This is
just speculation, but I believe the garbage collection is not invoked unless
another request is made to the web server (or perhaps even a request which
involves sessions. I'm really not sure on this, hopefully someone else can
clarify). Try accessing the page again when you are sure
session.gc_maxlifetime's time period has been exceeded.

Also, be sure you have not inadvertently modified the session.gc_probability
value from its default of 1. On sites with many request, it may be beneficial
to modify it to some other value, however, I would advise a you be
conservative with such a modification.

You can find more information on the various session related
settings I've mentioned here:
<http://www.php.net/manual/en/ref.session.php>

HTH,
--
steven vasilogianis

Steven Vasilogianis

unread,
Feb 20, 2003, 12:02:23 AM2/20/03
to
On Wed, 19 Feb 2003 22:29:09 -0500, Steven Vasilogianis
<Ste...@operamail.com> wrote:

> Also, be sure you have not inadvertently modified the session.gc_probability
> value from its default of 1.

Oops. It looks like since session.gc_probability is an integer, 1 means 1% and
not 100% as I originally thought (I figured they would default to setting
which is conservative with regards to security rather than performance.) This
means sessions can potentially live quite long on the server.

You may need to do a lot of refreshing before garbage collection is invoked
;-). You shouldn't be worrying yourself so much with the temporary session
files anyway, though.

Tommy

unread,
Feb 20, 2003, 4:08:37 AM2/20/03
to

"Steven Vasilogianis" <Ste...@operamail.com> wrote in message
As long as there is a sort of garbage collection for the temporary files I
am happy with it!!!
I read the page but I did not understand what was garbage collection talking
about sessions! (i know it for variables).
thanks a lot,

Tommy

0 new messages