PDA

View Full Version : Building a Forum.....



costas
01-12-2007, 12:52 PM
Hi to all,
I'm building a forum and I want to be able to see who's online! If anyone knows anything I'd appreciated!!

Thanks in advance!!

djr33
01-12-2007, 02:48 PM
Sessions. Not just PHP sessions, but stored in a database with matching info (good for security too, to compare).
Then display all of the names of people who have active sessions.
You could also use time in either deleting sessions that have been inactive for some amount of time, such as 15 minutes.

costas
01-13-2007, 12:39 PM
I found another way to see who's online! But how can I store sessions in db? Just for the knowledge and why not including it in my forum if it's better! Also, I want to have a permission system that would be different for each group in each forum! (Note that the forums are dynamically set by the admin) Any help would be appreciated!!

Thanks for answering!

tech_support
01-14-2007, 01:53 AM
But how can I store sessions in db?



<?php
$GLOBALS['sess_server'] = 'localhost';
$GLOBALS['sess_db'] = 'sessions';
$GLOBALS['sess_username'] = 'user';
$GLOBALS['sess_password'] = 'pass';

function sess_open() {
$GLOBALS['sess_mysqli'] = mysqli_connect(
$GLOBALS['sess_server'],
$GLOBALS['sess_username'],
$GLOBALS['sess_password']
);
mysqli_select_db($GLOBALS['sess_mysqli'], $GLOBALS['sess_db']);
}

function sess_close() {
mysqli_close($GLOBALS['sess_mysqli']);
}

function sess_read($id) {
$result = mysqli_query(
$GLOBALS['sess_mysqli'],
sprintf('SELECT data FROM sessiondata WHERE id = \'&#37;s\'',
mysqli_real_escape_string($GLOBALS['sess_mysqli'], $id))
);
if ($row = mysqli_fetch_object($result)) {
$ret = $row->data;
mysqli_query(
$GLOBALS['sess_mysqli'],
sprintf('UPDATE sessiondata SET access=\'%s\' WHERE id=\'%s\'',
date('YmdHis'),
mysqli_real_escape_string($GLOBALS['sess_mysqli'], $id))
);
} else {
$ret = '';
}
return $ret;
}

function sess_write($id, $data) {
mysqli_query(
$GLOBALS['sess_mysqli'],
sprintf('UPDATE sessiondata SET data=\'%s\', access=\'%s\' WHERE id=\'%s\'',
mysqli_real_escape_string($GLOBALS['sess_mysqli'], $data),
date('YmdHis'),
mysqli_real_escape_string($GLOBALS['sess_mysqli'], $id))
);
if (mysqli_affected_rows($GLOBALS['sess_mysqli']) < 1) {
mysqli_query(
$GLOBALS['sess_mysqli'],
sprintf('INSERT INTO sessiondata (data, access, id) VALUES (\'%s\', \'%s\', \'%s\')',
mysqli_real_escape_string($GLOBALS['sess_mysqli'], $data),
date('YmdHis'),
mysqli_real_escape_string($GLOBALS['sess_mysqli'], $id))
);
}
return true;
}

function sess_destroy($id) {
mysqli_query(
$GLOBALS['sess_mysqli'],
sprintf('DELETE FROM sessiondata WHERE id=\'%s\'',
mysqli_real_escape_string($GLOBALS['sess_mysqli'], $id))
);
return true;
}

function sess_gc($timeout) {
$timestamp = date('YmdHis', time() - $timeout);
mysqli_query(
$GLOBALS['sess_mysqli'],
sprintf('DELETE FROM sessiondata WHERE access < \'%s\'',
$timestamp)
);
}

session_set_save_handler(
'sess_open', 'sess_close', 'sess_read',
'sess_write', 'sess_destroy', 'sess_gc');
?>

djr33
01-15-2007, 11:25 PM
I don't know why you're using $GLOBALS... just use $_SESSION, if that is indeed your goal...