Forum Moderators: coopster

Message Too Old, No Replies

Best way to do session handling in a site template with function libs

         

Fuzzplug Jones

11:35 pm on Aug 25, 2006 (gmt 0)

10+ Year Member



Hello,

I have a fairly extensive set of PHP functions I have written and re-use, even complex stuff like full user account registration and handling, and functions to post messages that can be turned into blogs, forums, news posts, or whatever based on a little code here and there. It's basically a programmer's content management system that lets me deploy customized sites that don't look templated (like most of the CMSs) and at the same time allow me to not re-invent the wheel each time.

The only real pain I have with this is sessions. Here's an example.

At the top of every page I have an include which puts in a file called include_functions.php. This file is the base of my library. It sets up basic things and calls extra files that have specific functionality in them (one for user management, one for blog/forum messaging, and so on). It's at the top of the page because otherwise PHP sessions don't work (the dreaded "headers already sent" message). This is fine except all form and some URL querystring handling is done in these functions and will output before any HTML that defines the style of the site.

For example: Say you post a message. My code has a form with a "post" button. The website looks as it should (style, sidebars, static content etc). Then you press post. Because of sessions, the function libraries have to be the first line of the script, and so any error from the post or even the message that says "post accepted, redirecting" is output to the top of the HTML page (most functions "die" after they've done their thing, especially in the case of errors, so it's just a few lines of black text on a white page in Times font).

I'd like to keep the functionality the way it is, include a library at the top of the page and call functions as I need them. However, it's also very handy that the functions check for post things and process them, freeing me from having to put that logic in the custom site coding (in other words, all i have to do to make a blog is basically call the function to output the form, and it handles everything else).

Now comes the time when I need to fix this in order to feel comfortable with adding more functions to this library, and I wonder what your thoughts are for doing it "the right way." The things I've considered are to make a "session" function set that I can call at the top to just start the session and do systemwide constants (pulled from config files like site name, admin email etc), and then i'm free to move the include_functions (which exposes all functions and does automatic form handling and all that) down into the main content div or wherever it's supposed to go. Is that the way to do it, or is there a better way?

I've been programming php since about '98 so feel free to treat me like a non-noob. However, this just comes down to basic decision making, so while I could do it the aforementioned way, I'd like advice from people who know in case there's a better way.

Anxiously awaiting your expertise...

jatar_k

5:17 pm on Aug 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> It's at the top of the page because otherwise PHP sessions don't work

I would leave all the includes at the top of the page

>> However, it's also very handy that the functions check for post things and process them

libraries should never have output though just functions, called as needed

>> make a "session" function set that I can call at the top to just start the session and do systemwide constants

that's what I would do, but

>> and then i'm free to move the include_functions ... down into the main content div or wherever it's supposed to go

I think the include should stay at the top. You should then have a function call "wherever it's supposed to go" that calls the necessary pieces to setup the page

all that said, libs shouldn't have output. Really libs shouldn't do anything until explicitly called. A universal site specific function called global_page_setup or something that would know what functions are needed for a particular site would work. This means libs stay as they are supposed to be, a collection of functions to be used and reused, and keeps the control of what they do up to the site itself. You could have a control file that is site specific as well, just as a conf file would be site specific.

I also assume this large set of functions is split up into logical libs, not throwing a million functions into one file which you drag around to every page.

Fuzzplug Jones

6:24 pm on Aug 27, 2006 (gmt 0)

10+ Year Member



Thanks Jatar,

After I posted it I thought about that - that a library shouldn't ever return something without being called.

I'm starting to think that that's where I need to start, so that if I call a function called bb() (which is my main bulletin-board/blog/news/forum function), *it* should check before it runs if there's POST data to contend with. That functionality right now is a part of the library that always runs (for simplicity's sake while I was building it).

Thanks for confirming my feelings on this.

jatar_k

7:32 pm on Aug 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



my pleasure

one last thought, I wasn't sure if you were doing this or not.

I would group all of the like functions into specific libraries

cms_functions.lib.php
form_functions.lib.php
session_functions.lib.php
etc

or whatever your naming convention is.

Then use two site specific files on each site

config.php - holds all passwords etc
site_includes.php - each site will only need certain libraries so you can add all the relevant ones in this file. It is easy to add and remove includes for specific libs and keeps you from including 5k lines of code to be parsed for every page.