Simple PHP Private Messaging system.
Hello!
Here is quite a longish tutorial to help anyone who's looking to add a PM feature to their user authenticated site.
Note you will need:
- a SQL database, preferably MySQL as this is what i'll be using in the tutorial.
- knowledge of sessions/user authentication
- a little bit of free time
- Your favorite text editor (notepad ftw!)
- apache compiled with mod_rewrite (optional)
Database SetupRight first of all, we need to set up the database.
This can be done in many ways depending on what you want from your pm system but here are the basics.
- ID
- touser
- fromuser
- subject
- message
- read
- deleted
- datesent
i'll explain what each column head is for now.
ID - is mainly for indexing, but is also what we will use to call the pm information from the database.
touser - this is the username that the pm is going to.
you could also set this to touserid if you want to match it up with the user id in the user database, or if you want to add both you can do.fromuser - this is the username that the pm is from.
again you could use this as an id or both.subject - the subject of the pm will be placed in view for the user to see in the inbox
message - the body of the message will be saved here.
read - this will be a flag to refer to that checks if the user has read the message.
deleted - this will be another flag that checks if the user has deleted the message. this can be missed out and use a delete sql query but its more advisable to store the pm's just incase you need to refer to it at a later date.
datesent - this will be a timestamp just for reference.
Database StructureI mainly use text fields in this database but just to make things a little more professional we're gonna use integer, varchar, text and enum fields.
ID = int(12)
touser = varchar(40)
fromuser = varchar(40)
subject = varchar(150)
message = text()
read = enum('0','1')
deleted = enum('0','1')
datesent = text()
setting the primary key to ID (you can set this as auto_increment too).
your sql query should look something like this.
CREATE TABLE `pms` (
`id` INT( 12 ) NOT NULL AUTO_INCREMENT ,
`touser` VARCHAR( 40 ) NOT NULL ,
`fromuser` VARCHAR( 40 ) NOT NULL ,
`subject` VARCHAR( 150 ) NOT NULL ,
`message` TEXT NOT NULL ,
`read` ENUM( '0', '1' ) NOT NULL DEFAULT '0',
`deleted` ENUM( '0', '1' ) NOT NULL DEFAULT '0',
`datesent` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
)
Inbox SetupOk, firstly you need to make sure that you have your sessions set up here. i'm just going to use some basic session variables in this example, but you can make it alot more advanced for security etc.
we need to see if the session is registered firstly, we do this by checking if a session is registered.
<?php
session_start();
if(session_is_registered("SESSION_NAME")) {
if the session is indeed registered we will check the database to retrieve any messages for that user in the database.
$touser = $_SESSION['username'];
$sql = mysql_query("SELECT * FROM `pms` WHERE `touser` = '$touser' AND `deleted` = '0' ORDER BY `datesent` DESC");
now we have got that information we will loop it for each row and output it to the screen.
i'm using a table to output the information but you can use anything you like.
?>
<table width='95%'>
<tr><th>From</th><th>Subject</th><th>Date</th></tr>
<?php
while($r = mysql_fetch_object($sql)) {
echo "<tr><td>$r->fromuser</td><td><a href='view.php?id=$r->id'>$r->subject</a></td><td>$r->datesent</td></tr>";
}
?>
</table>
Now you should have a nice little inbox! you can obviously add css and javascript or what ever you fancy to make it all pretty but thats the basics of your inbox.
View MessageNotice I added a link to the "subject" part of the while loop. This comes into play here. This is also where you can add rewritten urls to protect the script a little more but i'll show you that later, for now we'll stick with what we have got.
firstly again security we must see if the user is logged in but i'm not going to repeat that again here (see finished script link at the bottom of this tutorial)
we must see if the id of the post is set, if it is grab the information from the database providing that person has the right permissions to view that message.
$id = @$_GET['id'];
if(!isset($id)) {
header('location: inbox.php');
}
elseif(isset($id)) {
$grab_pm = mysql_query("SELECT * FROM `pms` WHERE `touser` = '$touser' AND `id` = '$id'");
while($r= mysql_fetch_object($grab_pm)) {
echo "<h2>$r->subject</h2>";
echo "<p>$r->message</p>";
echo "<p>From: $r->fromuser On: $r->datesent</p>";
}
}
and thats it for the first part of the tutorial...
this is usually in two files, inbox.php and view.php but you can have it any way you like... play around with it and see what you can come up with... I'd like to see some examples.
in part two i'll be covering how to create and delete pm's also replying to received emails. And i'll try an add in an outbox also which will require some database modifications but ... well its all good..
hope you like this part of the tutorial any critisism/comments are welcome and if you get stuck on anything just ask i'll do my best to respond.
as promised i've added the link to the full code, bear in mind this has a few more security things added in that just makes it more functional but if you have any questions on those just reply and i'll try and answer them.
Inbox CodeView Message Codehappy coding.
Continue to part 2...
Last Edit: Feb 25, 2009 21:46:55 GMT by Llanilek