JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed parse errors is session.php, added test/session_test.php
authorJason Woofenden <jason183@herkamire.com>
Tue, 3 Apr 2007 03:37:31 +0000 (23:37 -0400)
committerJason Woofenden <jason183@herkamire.com>
Tue, 3 Apr 2007 03:37:31 +0000 (23:37 -0400)
session.php
test/session_test.html [new file with mode: 0644]
test/session_test.php [new file with mode: 0644]
test/session_test_db_connect.php [new file with mode: 0644]

index 295c421..c3be9e9 100644 (file)
@@ -20,9 +20,9 @@
 
 # you'll need this file that calles db_connect()
 if(!isset($GLOBALS['wfpl_db_handle'])) {
-       if(file_exists('db_connect.php') {
+       if(file_exists('db_connect.php')) {
                require_once('db_connect.php');
-       } elseif(file_exists('code/db_connect.php') {
+       } elseif(file_exists('code/db_connect.php')) {
                require_once('code/db_connect.php');
        } else {
                die("session.php requires a file db_connect.php or that you call db_connect() first. See code/wfpl/db.php for more information.");
@@ -149,12 +149,12 @@ function init_session() {
 
 # save a variable into the session
 function session_set($name, $value) {
-       session_unset($name);
+       session_clear($name);
        db_insert('wfpl_session_data', 'session_id,name,value', $GLOBALS['session_id'], $name, $value);
 }
 
 # remove variable from the session
-function session_unset($name) {
+function session_clear($name) {
        db_delete('wfpl_session_data', 'where session_id=%i && name=%"', $GLOBALS['session_id'], $name);
 }
 
diff --git a/test/session_test.html b/test/session_test.html
new file mode 100644 (file)
index 0000000..5361385
--- /dev/null
@@ -0,0 +1,33 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+  <title>testing code/wfpl/session.php</title>
+</head>
+
+<body>
+
+<h1>testing code/wfpl/session.php</h1>
+
+<!--~block start~-->
+<div style="border: 2px solid black; padding: 10px; margin-top: -2px">
+  <h2>~message.html~</h2>
+  
+  <p>
+  <table cellpadding="5" cellspacing="0" border="1" summary="">
+    <tr><td colspan="4" style="font-weight: bold">wfpl_sessions table</td></tr>
+    <tr><td>id</td><td>session_key</td><td>length</td><td>expires</td></tr><!--~wfpl_sessions_row start~-->
+    <tr><td>~id.html~</td><td>~session_key.html~</td><td>~length.html~</td><td>~expires.html~</td></tr><!--~end~-->
+  </table>
+  </p>
+  <p>
+  <table cellpadding="5" cellspacing="0" border="1" summary="">
+    <tr><td colspan="4" style="font-weight: bold">wfpl_session_data table</td></tr>
+    <tr><td>id</td><td>session_id</td><td>name</td><td>value</td></tr><!--~wfpl_session_data_row start~-->
+    <tr><td>~id.html~</td><td>~session_id.html~</td><td>~name.html~</td><td>~value.html~</td></tr><!--~end~-->
+  </table>
+  </p>
+</div>
+<!--~end~-->
+</body>
+</html>
diff --git a/test/session_test.php b/test/session_test.php
new file mode 100644 (file)
index 0000000..8defd9e
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+
+# Copyright 2007 Jason Woofenden   PUBLIC DOMAIN
+
+
+# To run this file:
+#
+# 1) make a link to it from your website directory which has the code/ directory in it
+#
+# 2) edit code/wfpl/test/session_test_db_connect.php to connect to your TESTING database
+#
+# 3) add the wfpl_session tables to your TESTING database:
+#
+# create table wfpl_sessions (id int unique auto_increment, session_key varchar(16), length int, expires int);
+# create table wfpl_session_data (id int unique auto_increment, session_id int, name varchar(100), value text);
+
+##############
+# WARNING: this code deletes all wfpl sessions. Do not use on a live site's database
+##############
+
+require_once('code/wfpl/test/session_test_db_connect.php');
+require_once('code/wfpl/session.php');
+
+function session_dump($message) {
+       $ses = db_get_rows('wfpl_sessions', 'id,session_key,length,expires');
+       if($ses) foreach($ses as $row) {
+               list($id, $session_key, $length, $expires) = $row;
+               tem_set('id', $id);
+               tem_set('session_key', $session_key);
+               tem_set('length', $length);
+               tem_set('expires', $expires);
+               tem_sub('wfpl_sessions_row');
+       }
+
+       $data = db_get_rows('wfpl_session_data', 'id,session_id,name,value');
+       if($data) foreach($data as $row) {
+               list($id, $session_id, $name, $value) = $row;
+               tem_set('id', $id);
+               tem_set('session_id', $session_id);
+               tem_set('name', $name);
+               tem_set('value', $value);
+               tem_sub('wfpl_session_data_row');
+       }
+
+       tem_set('message', $message);
+       tem_sub('block');
+}
+
+
+function session_test() {
+       tem_load('code/wfpl/test/session_test.html');
+
+       db_delete('wfpl_sessions');
+       db_delete('wfpl_session_data');
+       session_dump('Clean slate');
+
+       session_new();
+       session_dump('new session');
+
+       session_set('username', 'jason');
+       session_dump('username jason');
+
+       session_set('username', 'phil');
+       session_dump('overwrote username as phil');
+
+       $old = $GLOBALS['session_id'];
+
+       session_new();
+       session_dump('new session');
+
+       session_set('username', 'jason');
+       session_set('bamph', 'foo');
+       session_dump('set username=jason and bamph=foo in new session');
+
+       session_clear('username');
+       session_dump('cleared username in new session');
+
+       _kill_session($old);
+       session_dump('killed old session');
+
+       kill_session();
+       session_dump('kill_session()');
+
+       tem_output();
+}
+
+?>
diff --git a/test/session_test_db_connect.php b/test/session_test_db_connect.php
new file mode 100644 (file)
index 0000000..71090a2
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+
+require_once('code/wfpl/db.php');
+
+db_connect('DATABASE_NAME', 'USERNAME', 'PASSWORD');
+
+?>