Attached is a patch that adds documentation to sess_open(), sess_close(), sess_read(), and sess_write(), explaining how to properly use sessions. It also renames these functions with an underscore prefix to mark them as "private".

Comments

jscheel’s picture

Category: feature » task

Sorry, changing category.

beeradb’s picture

I haven't had a chance to test the patch yet, but +1 for documentation, as well as the clarity of renaming the functions. My gut feeling is this should be "normal" priority rather than minor, since it introduces documentation to previously undocumented functions, and adds clarity all around.

jscheel’s picture

Priority: Minor » Normal

Ok, I am changing the priority to normal.

jscheel’s picture

Status: Needs review » Needs work

Ok, after a discussion with chx on irc, I am marking this as needs work. I need to rewrite all of this more thoroughly. Patch coming soon.

jscheel’s picture

StatusFileSize
new2.99 KB

Here's a new patch with the updated documentation. Also, at the suggestion of chx, I have removed the arguments for session_open() because they are never used. Keep in mind, this patch prefixes the functions with an underscore to reinforce the fact that they should not be used directly (private).

jscheel’s picture

Status: Needs work » Needs review
gpk’s picture

Status: Needs review » Needs work

Looks a huge improvement :)

What about the changes to bootstrap.inc and cache.inc that were in the original patch?

Some minor comments on the documentation:

First a general comment - the documentation for the first 2 functions describes what they do in general, whereas for the last 2 it describes the Drupal implmentation specifically. I've suggested specific changes for better consitency below.

1. Suggest changing

* The session save handlers:

to

* The user-level session handlers

2. I don't understand the documentation for _sess_open() - I thought that http://uk3.php.net/manual/en/function.session-save-path.php is what is used to set the save path, and anyway the save path is not used in Drupal...

3. _sess_close(): Suggest changing

* This function is used to close the current session. Because 

to

* This function will be called by PHP to close the current session. Because 

4. _sess_read(): Suggest changing

* This function retrieves

to

* This function will be called by PHP to get the current user's session data. As well as retrieving the session from the database, it also ...

5. _sess_write(): Suggest changing

* This function writes the current session to the database.

to

* This function will be called by PHP to store the current user's session.

6. Suggest changing

* This function should not be used directly.

to

* This function should not be called directly.

7. Suggest removing

 Session variables should instead be 
* accessed via the $_SESSION superglobal.

from the documentation of _sess_open() and _sess_close().

Also some of the documentation lines are longer than the 80 character limit (coding standards).

Sorry, rather more suggested changes here than I originally intended!

jscheel’s picture

@gpk: Thanks for the suggestions. I'll get a new patched rolled shortly (been super busy).

jscheel’s picture

Status: Needs work » Needs review
StatusFileSize
new11.18 KB

Ok, here is a new patch. Sorry it took so long in coming, I've been crazy busy :P

gpk’s picture

Status: Needs review » Needs work

Thanks for update, looking good :)

A few final comments, pretty minor stuff really:

- At the top, maybe replace "The user-level session save handlers:" with "The user-level session storage handlers:" (which is how PHP doc describes them)

- The list of 4 handlers omits 2 - gc and destroy_sid. Obviously the last 2 *can* be called directly.

- "and are called by PHP" -> "and are called automatically by PHP"

- The comment "This function should not be called directly." actually still looks a bit unnecessary to me in the context of _sess_open() and _sess_close() - I mean, calling these directly basically does nothing, is therefore harmless, and also there is no reason that anyone would want to in a Drupal app. Suggest removing these 2 instances.

- "This function will be called by PHP to retrieve the current user's session data from the database. It also loads the current user's appropriate roles into the user object."
--> - "This function will be called by PHP to retrieve the current user's session data, which is stored in the database. It also loads the user's roles into the user object."

- "This function will be called by PHP to store the current user's session to the database." -> "This function will be called by PHP to store the current user's session, which Drupal saves to the database."

Hope this is helpful, have not actually tested the fairly trival code changes but all looks OK.

chx’s picture

Holy batman! Very very nice work! Keep going.

jscheel’s picture

@gpk, thanks for all the suggestions! I'll work on that this afternoon. The user registration test should work for this, but I can write a new one if needed.

jscheel’s picture

Well, darn, 9 weeks and I haven't been able to touch this. We have a long weekend here in the US, so I'll try to take some time to finish this up!

jscheel’s picture

StatusFileSize
new10.79 KB

Here's the new patch. I made most of the changes gpk suggested, but I did leave in the "function should not be called directly" because I believe it adds clarification. What do you guys think?

jscheel’s picture

Status: Needs work » Needs review
dries’s picture

Status: Needs review » Reviewed & tested by the community

Excellent patch! Committed to CVS HEAD. Thanks jscheel and gpk.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Oops, meant to mark them as 'fixed'.

gpk’s picture

Fantastic, thanks Dries :) and especially jscheel for doing the bulk of the donkey-work :) :)

damien tournoud’s picture

Status: Fixed » Active

Sorry to reopen this, but why not marking sess_destroy_sid and sess_gc private? These are also session callback functions registered by session_set_save_handler(), and they should not (and are not) called directly.

gpk’s picture

Also the PHPdoc of _sess_open() and _sess_close() is not working - can't see why. And the documentation parser is stripping out an opening bracket and leaving session_set_save_handler) in a few places. See http://api.drupal.org/api/file/includes/session.inc/7. Hardly earth-shattering but a bit odd. sess_gc (or _sess_gc) is also missing documentation.

jscheel’s picture

Odd, not sure what's up with the parser. I'll look through everything again and see if I can find anything.

sess_gc() and sess_destroy_sid() shouldn't be marked private because you can theoretically use them, at least that was Clouseau's reasoning he gave when he saw that I had them marked as private. What do you guys think, should we just mark all of the session handlers private?

@gpk Thanks for all of the input!

damien tournoud’s picture

@jsheel: I would like to see sess_gc an sess_destroy_sid marked as private. Those are also callbacks of the PHP session handler and should not be used directly. Instead, you can use the public PHP function session_destroy to kill a session. You should not have to trigger a garbage collection manually.

damien tournoud’s picture

@jsheel: could you please also fix the last lines of your docbook comments? You are missing the leading space in all of them:

+*/    
+function _sess_close() {

should be:

+ */    
+function _sess_close() {
gpk’s picture

@23: Thanks Damien, I couldn't see that for the looking!

damien tournoud’s picture

Status: Active » Needs review
StatusFileSize
new6.75 KB

Here is an extension for this.

This new patch:

- privatize _sess_gc and _sess_destroy_sid
- prefix all public functions with drupal_ to clarify their use. session_regenerate is drupal_session_regenerate, session_count is drupal_session_count, session_destroy_uid is drupal_session_destroy_uid, session_save_session is now drupal_save_session
- fixed the doxygen from session.inc

catch’s picture

Makes sense to rename the functions to drupal_ - consistent with similar functions elsewhere in core. The other fixes look fine. No time to run tests now though so could do with another review.

damien tournoud’s picture

StatusFileSize
new12.57 KB

Apparently not all the chunk got in the previous patch.

dries’s picture

Status: Needs review » Needs work

Committed this to CVS HEAD. Thanks!

I'm marking this as 'code needs work' because we need to update the upgrade instructions in the handbook.

damien tournoud’s picture

This may have broken the file tests that got in this morning (and used session_save_session).

catch’s picture

Title: Clarification on session handling functions » Tests broken: Clarification on session handling functions
Category: task » bug
Priority: Normal » Critical
Status: Needs work » Needs review
StatusFileSize
new824 bytes
new1.76 KB

It did. Patch.

damien tournoud’s picture

Status: Needs review » Reviewed & tested by the community

Thanks catch.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks!

beeradb’s picture

Status: Fixed » Needs work

marking back as "code needs work" for the handbook additions Dries requested in #28

beeradb’s picture

Title: Tests broken: Clarification on session handling functions » Clarification on session handling functions

Tests no longer broken (I presume...)

gpk’s picture

Status: Needs work » Needs review
StatusFileSize
new5.4 KB

http://api.drupal.org/api/file/includes/session.inc/7 looks much more sane now, however the Description section of the PHPdoc needs fixing. Attached patch does that, and also corrects documentation of http://api.drupal.org/api/function/drupal_session_count/7 (it looks as if the patch from #293421: Documentation for sess_count is wrong did not apply properly). Also some further comments cleanup.

Is it worth improving the naming of drupal_save_session() - which despite its name does not actually save the session. How about drupal_set_session_save() (and maybe pair it with a new drupal_get_session_save()) ??

I presume that in #28 Dries was referring to the module update instructions? http://drupal.org/node/224333.

drewish’s picture

i'll chime in that drupal_save_session() is a pretty bad name for a function that doesn't save the session.

jscheel’s picture

Hey, sorry I've been awol for a while... been heads down with work :P I'm gonna throw my vote in with gpk, drupal_save_session() needs to be renamed. Awesome work on this!

dries’s picture

Status: Needs review » Needs work

This patch no longer applies because of the REQUEST_TIME changes. Will need a quick re-roll.

Let's deal with the naming of drupal_save_session() after the reroll. It might be a bad name, but I need to investigate where the function is used, and why.

gpk’s picture

@38: IIRC drupal_save_session() is not used in core (except in http://api.drupal.org/api/function/_sess_write/7 - to check whether to save session data - and in a test).

The PHPdoc of http://api.drupal.org/api/function/drupal_save_session/7 is helpful and the handbook page referenced there (http://drupal.org/node/218104) gives further info.

Ah, so that page will also need updating in due course as the function is no longer called session_save_session() ..!

redndahead’s picture

By the looks at the latest patch a lot has changed since then. Does this need to be fixed anymore?

moshe weitzman’s picture

Priority: Critical » Normal
Status: Needs work » Closed (fixed)

Much session stuff has changed. Reopen as needed.