Re: How do sessions work exactly?

From: Date: Fri, 28 Jul 2000 15:29:46 +0000
Subject: Re: How do sessions work exactly?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-8794@lists.php.net to get a copy of this message
> I am way lost on sessions. What I am trying to do is have a cookie > identify a message board poster. Sessions are good for that purpose :-) > But to prevent people from being able > to make their own cookies with someone else's identity contained, > I don't want to use a plain old cookie to store their identity. If I > understand corectly, a session would send a cookie that contains a > unique ID string, and that ID string would the be associated with the > variables stored on my server. Is this correct? That is exactely how sessions work. > Assuming that's how its supposed to work, how do I make it work like > that? The persons identity will already be stored in a mysql database, > can I have the session point to the database itself? Yes. If you write your own callback functions and enable them with session_set_save_handler() then you can write session functions that operate on a MySQL (ar some other) database. > What I am doing so > far is just using session_register($name, $blarg) in the registering > script. So if they register blarg is set to 1, and stored with the name > they used as the session. Then, in theory I should be able to call > those variables in other pages used to actually post messages right? Correct..., although you appear to use session_register() incorrectly. The correct way to register a variable (or object/array) is like this: $foo = 2; session_register(”foo”); > What exactly do I need to do to be able to access them from the other > pages? When you call session_start() then all the registered variables will be restored (either from file if you use the standard functions, or from the database if you write your own functions to retrieve the data), and they are then emidiately available; session_start(); echo $foo; > All I have is session_start() at the beggining of the other > pages, and that's not working. Then you are probably not registering your variables correctly. Below I have included some skeletal code for writing your own session functions. I can send you a complete working example that stores session data in a MySQL DB if you like ;-) /* called whenever a new session is initialized */ /* returns true on success and false on error */ function sess_open($save_path, $sess_name) { } /* called when closing a session */ /* returns true on success and false on error */ function sess_close() { } /* called at start of session to read stored session data */ /* returns serialized string on success and false on error */ function sess_read($sess_id) { } /* called when session data needs to be saved */ /* returns true on success and false on error */ function sess_write($sess_id, $val) { } /* called when we want to destroy all data related to a session */ /* returns true on success and false on error */ function sess_destroy($sess_id) { } /* garbage collector, cleans up stale sessions */ /* returns true on success and false on error */ function sess_gc($max_lifetime) { } /* set up callback functions */ session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc"); Best regards, Jesper Juhl juhl@eisenstein.dk

« previous php.general (#8794) next »