Completing a Noisy Image Application - Defining a basic session handling PHP class
(Page 3 of 4 )
As I explained, the only missing piece of this noisy image application is a session mechanism that allows us to save the four-digit random values to a plain variable, so they can be properly checked on the web server after a web form has been submitted.
Fortunately, the mechanism can be easily implemented by creating a simple session handling PHP class. This class must provide at least some basic functionality, including the ability to register and deregister session variables, create and destroy a particular session, and so forth.
Having explained how random values will be handled via a session handling PHP class, please pay attention to its signature, which is shown below:
class SessionHandler{
function SessionHandler(){
session_start();
}
function setVariable($value='default',$varname='challenge'){
$_SESSION[$varname]=$value;
}
function getVariable($varname='challenge'){
if(!$_SESSION[$varname]){
trigger_error('Error getting session
variable',E_USER_ERROR);
}
return $_SESSION[$varname];
}
function destroy(){
session_start();
session_unset();
session_destroy();
}
}
After examining the definition of the above "SessionHandler" class, certainly you'll have to agree with me that its intrinsic logic is very easy to grasp. As you can see, this class presents a few basic methods for registering and deregistering session variables, and destroying an existing session as well. Quite simple, right?
Obviously, in the context of the noisy image application that I'm currently developing, the previous session handling class fits perfectly. In this case it's not necessary to create a complex session-related mechanism to save the generated random strings to a plain variable.
So far, so good. At this point, I have shown you the signature of the brand new "SessionHandler()" class, which admittedly is pretty straightforward. However, I'd like to demonstrate how this class can be properly linked to the noisy image script that was listed in the previous section, in this way completing the application itself.
Thus, in the final section of this article I'm going to make available for you the full source code corresponding to this noisy image application, this time including the basic session handling class. Please click on the link below and keep reading.
Next: Completing the noisy image application >>
More DHTML Articles
More By Alejandro Gervasio