session handling using classes n objects

This is a discussion on session handling using classes n objects within the PHP Language forums, part of the PHP Programming Forums category; hi, i have written a class for session handling, and i want to use it to keep track of the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-05-2006
viz
 
Posts: n/a
Default session handling using classes n objects

hi,

i have written a class for session handling, and i want to use it to
keep track of the user.
After authenticating the user in login page i am storing the session
info like uname etc.. in a object of session class. I am creating this
object in the login page.
Now how can i make this object persist between subsequent page
requests. and i dont want to use GET method.
Is it sensible to use hidden fields OR will i have to create a new
session object in each page???

plz help

Thanx
Josh

Reply With Quote
  #2 (permalink)  
Old 12-05-2006
Saul
 
Posts: n/a
Default Re: session handling using classes n objects


viz wrote:
> i have written a class for session handling, and i want to use it to
> keep track of the user.
> After authenticating the user in login page i am storing the session
> info like uname etc.. in a object of session class. I am creating this
> object in the login page.
> Now how can i make this object persist between subsequent page
> requests. and i dont want to use GET method.
> Is it sensible to use hidden fields OR will i have to create a new
> session object in each page???


To make an object persist between page calls you will have to save it
somewhere. Depending on the complexity you will probably use $_SESSION
variables and optionally save data to a file or database that will be
read by each page view. To access the $_SESSION variables a session ID
is held on the client computer either in a cookie or in the URL as a
$_GET variable (it's simply a session reference ID).

If you use hidden fields on forms then the data will be visible to
users using view source, and therefore can compromise security for
example someone creating a fake set of hidden variables.

If you read up on PHP sessions it will help you get where you want to
go.


Saul
www.notanant.com
Communities of websites

Reply With Quote
  #3 (permalink)  
Old 12-05-2006
viz
 
Posts: n/a
Default Re: session handling using classes n objects

well i am not using database;

and i am also not using session handling functions like
session_register and session_start Directly.
i have implemented them using a session class and i am setting and
retrieving session variables by using object of the session class.
the problem i am facing is that as long as i use the session class
object in a single page it is fine but what should i do in order to
make that object accessible in other pages so that i may check the
authenticity of user, using functions on the same object.
hop i m clear this time.

Thanx

Reply With Quote
  #4 (permalink)  
Old 12-05-2006
no@emails.thx
 
Posts: n/a
Default Re: session handling using classes n objects

On 5 Dec 2006 02:53:42 -0800, "viz" <vijayjoshi4u@gmail.com> wrote:

>well i am not using database;
>
>and i am also not using session handling functions like
>session_register and session_start Directly.
>i have implemented them using a session class and i am setting and
>retrieving session variables by using object of the session class.
>the problem i am facing is that as long as i use the session class
>object in a single page it is fine but what should i do in order to
>make that object accessible in other pages so that i may check the
>authenticity of user, using functions on the same object.
>hop i m clear this time.


This sounds like circular logic and I'm tempted to ask why you want to
complicate the use of sessions by making a class that you then have to
hold in a normal $_SESSION[] variable?

To make data 'persist' between pages choose one of the following:
1. hold it is a database (but you're not doing that)
2. hold it in cookies (but this can be switched off by the user)
3. hold it in sessions.

For something as simple as holding a userid I'd just create a
$_SESSION['userid'] variable on successful login and refer to it later
in the code by name.

I can understand creating classes for managing complex data structures
(eg. database records etc) but from what you have said so far I don't
think it warrants it in your case. :o)

Chris R.
Reply With Quote
  #5 (permalink)  
Old 12-05-2006
viz
 
Posts: n/a
Default Re: session handling using classes n objects



On Dec 5, 4:28 pm, n...@emails.thx wrote:
> On 5 Dec 2006 02:53:42 -0800, "viz" <vijayjosh...@gmail.com> wrote:
>
> >well i am not using database;

>
> >and i am also not using session handling functions like
> >session_register and session_start Directly.
> >i have implemented them using a session class and i am setting and
> >retrieving session variables by using object of the session class.
> >the problem i am facing is that as long as i use the session class
> >object in a single page it is fine but what should i do in order to
> >make that object accessible in other pages so that i may check the
> >authenticity of user, using functions on the same object.
> >hop i m clear this time.This sounds like circular logic and I'm tempted to ask why you want to

> complicate the use of sessions by making a class that you then have to
> hold in a normal $_SESSION[] variable?
>
> To make data 'persist' between pages choose one of the following:
> 1. hold it is a database (but you're not doing that)
> 2. hold it in cookies (but this can be switched off by the user)
> 3. hold it in sessions.
>
> For something as simple as holding a userid I'd just create a
> $_SESSION['userid'] variable on successful login and refer to it later
> in the code by name.
>
> I can understand creating classes for managing complex data structures
> (eg. database records etc) but from what you have said so far I don't
> think it warrants it in your case. :o)
>
> Chris R.



Thanx Chris,
that was what i wanted to know. i am new to PHP5 thats why i was
searching 4 the most feasible way.
I have one more query....if u dont mind.

when i am running my application on Firefox and if i login then the
same session is getting duplicated if i open another tab. Although if i
run the program on Firefox and IE simultaneously then 2 distinct
sessions are being created.
Is it normal??? How can it be explained??
Currently i m playing with a small application but soon i think i will
have to make use of databases for user management. Can u give some
insight into that also.

Thanx again
Josh

Reply With Quote
  #6 (permalink)  
Old 12-05-2006
no@emails.thx
 
Posts: n/a
Default Re: session handling using classes n objects

On 5 Dec 2006 03:54:10 -0800, "viz" <vijayjoshi4u@gmail.com> wrote:
>On Dec 5, 4:28 pm, n...@emails.thx wrote:
>> On 5 Dec 2006 02:53:42 -0800, "viz" <vijayjosh...@gmail.com> wrote:
>>
>> >well i am not using database;

>>
>> >and i am also not using session handling functions like
>> >session_register and session_start Directly.
>> >i have implemented them using a session class and i am setting and
>> >retrieving session variables by using object of the session class.
>> >the problem i am facing is that as long as i use the session class
>> >object in a single page it is fine but what should i do in order to
>> >make that object accessible in other pages so that i may check the
>> >authenticity of user, using functions on the same object.
>> >hop i m clear this time.This sounds like circular logic and I'm tempted to ask why you want to

>> complicate the use of sessions by making a class that you then have to
>> hold in a normal $_SESSION[] variable?
>>
>> To make data 'persist' between pages choose one of the following:
>> 1. hold it is a database (but you're not doing that)
>> 2. hold it in cookies (but this can be switched off by the user)
>> 3. hold it in sessions.
>>
>> For something as simple as holding a userid I'd just create a
>> $_SESSION['userid'] variable on successful login and refer to it later
>> in the code by name.
>>
>> I can understand creating classes for managing complex data structures
>> (eg. database records etc) but from what you have said so far I don't
>> think it warrants it in your case. :o)
>>
>> Chris R.

>
>
>Thanx Chris,
>that was what i wanted to know. i am new to PHP5 thats why i was
>searching 4 the most feasible way.
>I have one more query....if u dont mind.
>
>when i am running my application on Firefox and if i login then the
>same session is getting duplicated if i open another tab. Although if i
>run the program on Firefox and IE simultaneously then 2 distinct
>sessions are being created.
>Is it normal??? How can it be explained??
>Currently i m playing with a small application but soon i think i will
>have to make use of databases for user management. Can u give some
>insight into that also.


My understanding of sessions is that the lifetime of the session is
within the browser being run ... 2 browsers would have a session each
.... close the browser and open it again and you get another session
.... run 2 tabs in the same browser and they share the same session. (I
think)

Chris R.
Reply With Quote
  #7 (permalink)  
Old 12-05-2006
Saul
 
Posts: n/a
Default Re: session handling using classes n objects


viz wrote:
> when i am running my application on Firefox and if i login then the
> same session is getting duplicated if i open another tab. Although if i
> run the program on Firefox and IE simultaneously then 2 distinct
> sessions are being created.
> Is it normal??? How can it be explained??
> Currently i m playing with a small application but soon i think i will
> have to make use of databases for user management. Can u give some
> insight into that also.


The session variable is being held in a cookie in this case. Each of
the different browsers holds cookies separately hence two sessions with
two browsers open. If you are within a single browser the same cookie
is used, hence one cookie.


Saul
www.notanant.com
Communities of websites

Reply With Quote
  #8 (permalink)  
Old 12-05-2006
iulian.ilea
 
Posts: n/a
Default Re: session handling using classes n objects


no@emails.thx wrote:
> On 5 Dec 2006 03:54:10 -0800, "viz" <vijayjoshi4u@gmail.com> wrote:
> >On Dec 5, 4:28 pm, n...@emails.thx wrote:
> >> On 5 Dec 2006 02:53:42 -0800, "viz" <vijayjosh...@gmail.com> wrote:
> >>
> >> >well i am not using database;
> >>
> >> >and i am also not using session handling functions like
> >> >session_register and session_start Directly.
> >> >i have implemented them using a session class and i am setting and
> >> >retrieving session variables by using object of the session class.
> >> >the problem i am facing is that as long as i use the session class
> >> >object in a single page it is fine but what should i do in order to
> >> >make that object accessible in other pages so that i may check the
> >> >authenticity of user, using functions on the same object.
> >> >hop i m clear this time.This sounds like circular logic and I'm tempted to ask why you want to
> >> complicate the use of sessions by making a class that you then have to
> >> hold in a normal $_SESSION[] variable?
> >>
> >> To make data 'persist' between pages choose one of the following:
> >> 1. hold it is a database (but you're not doing that)
> >> 2. hold it in cookies (but this can be switched off by the user)
> >> 3. hold it in sessions.
> >>
> >> For something as simple as holding a userid I'd just create a
> >> $_SESSION['userid'] variable on successful login and refer to it later
> >> in the code by name.
> >>
> >> I can understand creating classes for managing complex data structures
> >> (eg. database records etc) but from what you have said so far I don't
> >> think it warrants it in your case. :o)
> >>
> >> Chris R.

> >
> >
> >Thanx Chris,
> >that was what i wanted to know. i am new to PHP5 thats why i was
> >searching 4 the most feasible way.
> >I have one more query....if u dont mind.
> >
> >when i am running my application on Firefox and if i login then the
> >same session is getting duplicated if i open another tab. Although if i
> >run the program on Firefox and IE simultaneously then 2 distinct
> >sessions are being created.
> >Is it normal??? How can it be explained??
> >Currently i m playing with a small application but soon i think i will
> >have to make use of databases for user management. Can u give some
> >insight into that also.

>
> My understanding of sessions is that the lifetime of the session is
> within the browser being run ... 2 browsers would have a session each
> ... close the browser and open it again and you get another session
> ... run 2 tabs in the same browser and they share the same session. (I
> think)
>
> Chris R.


Chris, you are right. The session is stored by window not by tab.

Reply With Quote
  #9 (permalink)  
Old 12-05-2006
no@emails.thx
 
Posts: n/a
Default Re: session handling using classes n objects

On 5 Dec 2006 06:02:10 -0800, "iulian.ilea" <iulian.ilea@gmail.com>
wrote:

>
>no@emails.thx wrote:
>> On 5 Dec 2006 03:54:10 -0800, "viz" <vijayjoshi4u@gmail.com> wrote:
>> >On Dec 5, 4:28 pm, n...@emails.thx wrote:
>> >> On 5 Dec 2006 02:53:42 -0800, "viz" <vijayjosh...@gmail.com> wrote:
>> >>
>> >> >well i am not using database;
>> >>
>> >> >and i am also not using session handling functions like
>> >> >session_register and session_start Directly.
>> >> >i have implemented them using a session class and i am setting and
>> >> >retrieving session variables by using object of the session class.
>> >> >the problem i am facing is that as long as i use the session class
>> >> >object in a single page it is fine but what should i do in order to
>> >> >make that object accessible in other pages so that i may check the
>> >> >authenticity of user, using functions on the same object.
>> >> >hop i m clear this time.This sounds like circular logic and I'm tempted to ask why you want to
>> >> complicate the use of sessions by making a class that you then have to
>> >> hold in a normal $_SESSION[] variable?
>> >>
>> >> To make data 'persist' between pages choose one of the following:
>> >> 1. hold it is a database (but you're not doing that)
>> >> 2. hold it in cookies (but this can be switched off by the user)
>> >> 3. hold it in sessions.
>> >>
>> >> For something as simple as holding a userid I'd just create a
>> >> $_SESSION['userid'] variable on successful login and refer to it later
>> >> in the code by name.
>> >>
>> >> I can understand creating classes for managing complex data structures
>> >> (eg. database records etc) but from what you have said so far I don't
>> >> think it warrants it in your case. :o)
>> >>
>> >> Chris R.
>> >
>> >
>> >Thanx Chris,
>> >that was what i wanted to know. i am new to PHP5 thats why i was
>> >searching 4 the most feasible way.
>> >I have one more query....if u dont mind.
>> >
>> >when i am running my application on Firefox and if i login then the
>> >same session is getting duplicated if i open another tab. Although if i
>> >run the program on Firefox and IE simultaneously then 2 distinct
>> >sessions are being created.
>> >Is it normal??? How can it be explained??
>> >Currently i m playing with a small application but soon i think i will
>> >have to make use of databases for user management. Can u give some
>> >insight into that also.

>>
>> My understanding of sessions is that the lifetime of the session is
>> within the browser being run ... 2 browsers would have a session each
>> ... close the browser and open it again and you get another session
>> ... run 2 tabs in the same browser and they share the same session. (I
>> think)
>>
>> Chris R.

>
>Chris, you are right. The session is stored by window not by tab.


Thanks for the confirmation :o)

Of course, I forgot to say that the life-time of the session is also
dependent on the configured lifetime of the session/cookies on the
server too.

Chris R.
Reply With Quote
  #10 (permalink)  
Old 12-07-2006
Curtis
 
Posts: n/a
Default Re: session handling using classes n objects

> Currently i m playing with a small application but soon i think i will
> have to make use of databases for user management. Can u give some
> insight into that also.


Well, how to go about properly and securely managing databases (I
assume you mean a relational database, like MySQL) can fill a book, and
I believe authors have, indeed, done so. I'm sure people here will be
more than willing to help you along the way, but I would suggest that
you first do some research online or by reading current books on the
issue. The examples on php.net's documentation uses good, secure code,
but you'll want to search out articles on sites like sitepoint, which,
as far as I know, is a place which uses good quality code. You need to
watch out for articles, which features code that doesn't escape user
input, which can lead to SQL injection (visitors crafting arbitrary SQL
queries through input, whether from the query string or POSTed from a
different script altogether). In PHP, for MySQL, you will notice the
mysql_real_escape_string function. There are similar functions for
other databases. The mysqli extension even lets you use prepared
statements, if I'm not mistaken.

So, again, you'll want to do some research on this topic, especially if
you plan on deploying your project in the public domain.

Curtis

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:48 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0