Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
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 Setup

Right 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 Structure

I 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 Setup

Ok, 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 Message

Notice 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 Code

View Message Code


happy coding.


Continue to part 2...


Last Edit: Feb 25, 2009 21:46:55 GMT by Llanilek

Benjamin

Benjamin Avatar
#YOCO... You only color once.

******
Elite Mod

1,958


November 2006
Llanilek Avatar
your sql query should look something like this.


phpmyadmin ftw!!!

Nice tutorial; I'm sure it will help a lot of people in a lot of different ways (the fact that you mix sessions and MySQL).

In regards to your GET, I don't know why, but as a personal coding standard, i try to avoid using @ if possible. Instead i use something like:

$id = isset($_GET['id']) ? $_GET['id] : null;

if($id) {
// if the id exists
} else {
// fail.
}


Wonderful tut!

:: rate 5 ::
Lucifer Avatar
I'm gonna start dishing out internet beatings if people keep it up with this 4chan shit, I swear.





Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
Benjamin Avatar


phpmyadmin ftw!!!



AGREED !!! lol

Benjamin Avatar

Nice tutorial; I'm sure it will help a lot of people in a lot of different ways (the fact that you mix sessions and MySQL).


thanks... I will try to cover this a bit more in the next part.

Benjamin Avatar

In regards to your GET, I don't know why, but as a personal coding standard, i try to avoid using @ if possible. Instead i use something like:

$id = isset($_GET['id']) ? $_GET['id] : null;

if($id) {
// if the id exists
} else {
// fail.
}




i've never seen it done like that... but i'm just lazy hence the reason why i use the @ lol




thanks *takes a bow* lol

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
I hate any errors that can be possibly generated, so I kinda take Ben's approach. :P

I'd maybe throw in a brief section using INSERT INTO so they can see how to add a PM too. Great tutorial otherwise though.

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
Chris Avatar
I'd maybe throw in a brief section using INSERT INTO so they can see how to add a PM too. Great tutorial otherwise though.


that will be in part two lol as well as a few other features.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
:P Well, good to hear that.

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
wow this has had 294 clicks in 8 days from good-tutorials... best write the other part eh? lol.

ricmetal

ricmetal Avatar

*
New Member

4


February 2009
tick tock, tick tock
google aint helping - no other tuts on this :)
second part available yet?


Last Edit: Feb 22, 2009 20:16:25 GMT by ricmetal

Benjamin

Benjamin Avatar
#YOCO... You only color once.

******
Elite Mod

1,958


November 2006
ricmetal Avatar
tick tock, tick tock
google aint helping - no other tuts on this :)
second part available yet?


Not yet :(
Lucifer Avatar
I'm gonna start dishing out internet beatings if people keep it up with this 4chan shit, I swear.





Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
hey sorry about the delay in the second part i've been very busy at work i promise to get it up by the end of the week!

ricmetal

ricmetal Avatar

*
New Member

4


February 2009
"gureate!" \ ( ^ ^ ) /

Arigato


Last Edit: Feb 25, 2009 14:25:41 GMT by ricmetal

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
its done




Last Edit: Feb 25, 2009 21:47:21 GMT by Llanilek

ricmetal

ricmetal Avatar

*
New Member

4


February 2009
hey.
im implementing the pm system but the time isnt right
in the inbox i get the current time instead of the time the message was sent
help please?

alos, for the noobs out there,
change the db column's name read to something else or it wont update(mysql restricted name) --my noobness reveals. heard someone say `` in column names are to escape the mysql retricted words. i say, play it safe. i never liked those `` anyway


Last Edit: Apr 10, 2009 1:24:00 GMT by ricmetal

Benjamin

Benjamin Avatar
#YOCO... You only color once.

******
Elite Mod

1,958


November 2006
Paste the part of your code that displays the date, please. :)
Lucifer Avatar
I'm gonna start dishing out internet beatings if people keep it up with this 4chan shit, I swear.





newBookmarkLockedFalling