Hi folks,
I have two PHP questions concerning objects stored in sessions.
I'm developing an intranet for a client to be run in a Redhat 8/Apapche
2/PHP 4/MySQL environment. All pages are password protected and I get
to choose how to authenticate and layer permissions.
My first thought to this affect is to use the User table of the mysql
database to maintain site users rather than create a redundant users
table in some other database. MySQL does a great job with permissions,
and since I have root access, it seemed the natural course of action.
The user logs in by supplying name and password via an HTML form and
the authentication functions attempt a mysql_connect() with the
supplied arguments. If mysql_connect() returns greater than false, the
user clears.
Since I couldn't store the mysql_connect() in $_SESSION, I created an
object to store all of my "state" values, and it seems to work. (Though
I'm not certain why you can't store a db connection directly into
session vars, but can when they're embedded in an object.)
My (current) code for instantiating the object and storing it is as
follows:
<?php
require( "php_includ es/class_web_page. php" );
session_start() ;
if ( !$_SESSION["page"] )
{
$page = new Web_page();
$_SESSION["page"] = &$page;
}
else
{
$page = $_SESSION["page"];
}
...blah, blah, blah...
Question 1: I'm not sure that I'm ever killing this object and freeing
up its memory. When the session ends, will the object die with it?
Being a non-public application with low traffic, a small memory leak
won't show up for a long while, but I don't want to find out in a year
or so that I have to re-engineer the whole thing due to my current lack
of foresight.
Question 2: My first thought was to store a reference to the object
(&$page) in $_SESSION. (Coming from C, I figured I was storing a memory
address and that the interpreter automatically dereferenced it.) My
thinking was that storing the memory address was lighter on the
$_SESSION size. Reading up on PHP, I find that no, referencing in PHP
essentially duplicates the variable. (I'm not really passing by
reference, am I?) Am I now making a doubly wide memory allocation by
using my reference? Combine that with question 1, am I now creating a
memory leak twice as large?
Generally speaking, I'm pretty sure that I'm going about this with my
head up my a$$? If a more elegant/efficient solution should be taken,
I'd appreciate the criticism.
Thanks,
-Dan
I have two PHP questions concerning objects stored in sessions.
I'm developing an intranet for a client to be run in a Redhat 8/Apapche
2/PHP 4/MySQL environment. All pages are password protected and I get
to choose how to authenticate and layer permissions.
My first thought to this affect is to use the User table of the mysql
database to maintain site users rather than create a redundant users
table in some other database. MySQL does a great job with permissions,
and since I have root access, it seemed the natural course of action.
The user logs in by supplying name and password via an HTML form and
the authentication functions attempt a mysql_connect() with the
supplied arguments. If mysql_connect() returns greater than false, the
user clears.
Since I couldn't store the mysql_connect() in $_SESSION, I created an
object to store all of my "state" values, and it seems to work. (Though
I'm not certain why you can't store a db connection directly into
session vars, but can when they're embedded in an object.)
My (current) code for instantiating the object and storing it is as
follows:
<?php
require( "php_includ es/class_web_page. php" );
session_start() ;
if ( !$_SESSION["page"] )
{
$page = new Web_page();
$_SESSION["page"] = &$page;
}
else
{
$page = $_SESSION["page"];
}
...blah, blah, blah...
Question 1: I'm not sure that I'm ever killing this object and freeing
up its memory. When the session ends, will the object die with it?
Being a non-public application with low traffic, a small memory leak
won't show up for a long while, but I don't want to find out in a year
or so that I have to re-engineer the whole thing due to my current lack
of foresight.
Question 2: My first thought was to store a reference to the object
(&$page) in $_SESSION. (Coming from C, I figured I was storing a memory
address and that the interpreter automatically dereferenced it.) My
thinking was that storing the memory address was lighter on the
$_SESSION size. Reading up on PHP, I find that no, referencing in PHP
essentially duplicates the variable. (I'm not really passing by
reference, am I?) Am I now making a doubly wide memory allocation by
using my reference? Combine that with question 1, am I now creating a
memory leak twice as large?
Generally speaking, I'm pretty sure that I'm going about this with my
head up my a$$? If a more elegant/efficient solution should be taken,
I'd appreciate the criticism.
Thanks,
-Dan
Comment