Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Homemade session system??

session

  • Please log in to reply
6 replies to this topic

#1 bbqroast

bbqroast

    Codecall Addict

  • Senior Member
  • PipPipPipPipPipPip
  • 564 posts
  • Location:/etc/passwd

Posted 02 June 2011 - 06:50 PM

Hey just wondering could you make your own session like system?
My way of going around it would be to have the following functions:
start()
end()
set(var,val)
retrieve(var)
destroy(var)
revive(IP)

start()
Creates a cookie with a random ID on the clients computer, it then also adds this ID to a MySQL table called "Sessions" with the random ID, a Timestamp, an IP and a boolean "active" which is set to true.
end()
Takes the session getting the ID from the cookie and sets its active value to false it also destroys the cookie.
set(var,val)
Taking the ID from the cookie the function sees if an active session is running if so it looks through another table called "SessionVars" for a active match to var under the name column and a match with the ID from the table and ID from the cookie, if so it then sets the value column to val, if no match was present it creates a new enty with a boolean called active set to true it also adds the cookie ID to a column called "cookieID".
retrieve(var)
Looks through "SessionVars" for a active match with the same cookieID as the ID in the cookie of var to the name column then returns the value column.
destroy(var)
Looks through "SessionVars" under the name column for var then sets a boolean "active" to false.
revive(IP)
Looks through the "Sessions" table for an IP matching IP then outputs the results along with all the ID matches from "SessionVars".
I'd like some feedback.
Written from my bed, yes i'm that lazy- mind you though my neck REALLY hurts right now!
  • 0
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 02 June 2011 - 08:06 PM

Of course you can implement your own session handling, and it could be a good learning experience for database work.

In a practical sense, session_set_save_handler() can allow you to modify most of that functionality (creating functions for db work, then feeding those function names to the save_handler function) and can be a practical alternative than rewriting all of it.

You will be able to create functions for the following:

  • session open
  • session close
  • session reading (database)
  • session writing (database)
  • session destruction
  • session garbage collection (when the garbage collector comes around to call this one)
I find your task a bit more appealing however.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 Upstream

Upstream

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 98 posts
  • Location:Netherlands
  • Programming Language:C, C++, PHP, (Visual) Basic, JavaScript, Perl, Bash, Others
  • Learning:Others

Posted 03 June 2011 - 12:24 AM

Nice idea! But I always use sessions and info stored in a database for checking of the values given bij the browser to ensure that the session is valid. How would you go about this?
  • 0
"The question of whether a computer can think is no more interesting than the question of whether a submarine can swim." (Edsger Dijkstra)

#4 bbqroast

bbqroast

    Codecall Addict

  • Senior Member
  • PipPipPipPipPipPip
  • 564 posts
  • Location:/etc/passwd

Posted 03 June 2011 - 01:56 AM

Erm what does bij mean?
  • 0
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#5 Upstream

Upstream

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 98 posts
  • Location:Netherlands
  • Programming Language:C, C++, PHP, (Visual) Basic, JavaScript, Perl, Bash, Others
  • Learning:Others

Posted 03 June 2011 - 07:34 AM

Sorry, it´s Dutch....
  • 0
"The question of whether a computer can think is no more interesting than the question of whether a submarine can swim." (Edsger Dijkstra)

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 04 June 2011 - 04:15 PM

The easiest way to create something you try describe is probably to create your own class for session handling as a wrapper to the existing functions.

Class Session {
public function __construct() {
session_start();
}
}


And, to make your session Class work with the database, do as Alexander wrote about setting handler etc...
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#7 bbqroast

bbqroast

    Codecall Addict

  • Senior Member
  • PipPipPipPipPipPip
  • 564 posts
  • Location:/etc/passwd

Posted 17 July 2011 - 12:30 AM

Yeah, its more theory than anything.
I haven't actual done any work with PHP classes, I never really needed them I guess.
  • 0
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).





Also tagged with one or more of these keywords: session

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download