int cgi_session_var_exists(const char *name)
{
if (!List_item(name, sess_list_start)) {
session_lasterror = SESS_VAR_NOT_REGISTERED;
return 0;
}
return 1;
}
// Unregister a session variable ( destroying its value )
int cgi_session_unregister_var(char *name)
{
if (!sess_initialized) {
session_lasterror = SESS_NOT_INITIALIZED;
// Get the session ID
sid = cgi_cookie_value(SESSION_COOKIE_NAME);
// If there isn't a session ID, we need to create one
if (sid == NULL) {
if (sess_create_file()) {
cgi_add_cookie(SESSION_COOKIE_NAME, sess_id, 0, 0, 0, 0);
sess_initialized = 1;
return 1;
}
return 1;
}
return 0;
}
// Make sure the file exists
else {
save_path_len = strlen(SESSION_SAVE_PATH) + strlen(SESSION_FILE_PREFIX);
sess_fname = (char *)malloc(save_path_len + SESS_ID_LEN + 1);
if (!sess_fname)
libcgi_error(E_MEMORY, "File %s, line %s", __FILE__, __LINE__);
int sess_initialized = 0;; // true if the session has been initialized, otherwise false
int sess_finitialized = 0; // true if the session file has been created, otherwirte false
int session_lasterror = 0;
// This variables are used to control the linked list of all
// session objects. Most of time you don't need to use them
// directly
formvars *sess_list_start = NULL;
formvars *sess_list_last = NULL;
// Generate a session "unique" id
void sess_generate_id()
{
static char table[] = "123456789abcdefghijlmnopqrstuvxzwyABCDEFGHIJLMOPQRSTUVXZYW";
unsigned int len = strlen(table);
register int i;
// Changes file permission to 0600
chmod(sess_fname, S_IRUSR|S_IWUSR);
fclose(sess_file);
return 1;
}
// Destroy the session, including all data
// After session_destroy() was called, is not more
// possible to use session functions before an another
// call to session_start()
int cgi_session_destroy()
{
// Remember: unlink() returns 0 if success :)
if (!unlink(sess_fname)) {
sess_finitialized = 0;
List_free(&sess_list_start);
if (data != NULL) {
fprintf(sess_file, "%s=%s", data->name, data->value);
data = data->next;
if (data) {
while (data) {
fprintf(sess_file, ";%s=%s", data->name, data->value);
data = data->next;
}
}
}
fclose(sess_file);
return 1;
}
// Register a variable into current opened session
// Note that we are opening and closing the session file
// every time this function is called... ( I/O ** 1000000 :-/ )
int cgi_session_register_var(const char *name, const char *value)
{
formvars *data;
if (!sess_initialized) {
session_lasterror = SESS_NOT_INITIALIZED;