Studio Zero
« [PHP] Simple Private Messaging System »

Welcome Guest. Please Login or Register.
Sept 3, 2010, 4:30am




Studio Zero :: Web Development :: Coders' Hangout :: Coding Tutorials :: [PHP] Simple Private Messaging System
   [Search This Thread][Send Topic To Friend] [Print]
 AuthorTopic: [PHP] Simple Private Messaging System (Read 5,589 times)
Llanilek
Dedicated Studio Member
****
[p:168]
member is offline

[avatar]

Former Elite Mod

[yim] [msn] [aim]
[homepage]

Joined: Oct 2005
Gender: Male
Posts: 921
Location: Y Drenewydd, North Wales, UK
Karma: 9
 [PHP] Simple Private Messaging System
« Thread Started on Jan 17, 2009, 9:55pm »

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.

Code:
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.

Code:
<?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.

Code:
$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.

Code:
?>

<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.

Code:
$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, 9:46pm by Llanilek »Link to Post - Back to Top  IP: Logged

[image]
Matt Murdock
Elite Mod
******
Member of Fredy's "I'm Whipped" Club
member is offline

[avatar]



Joined: Nov 2006
Gender: Male
Posts: 1,678
Location: Illinois
Karma: 18
 Re: [PHP] Simple Private Messaging System
« Reply #1 on Jan 18, 2009, 2:26am »


Jan 17, 2009, 9:55pm, Llanilek wrote:
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:

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

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


Wonderful tut!

[image]
Link to Post - Back to Top  IP: Logged


Nov 10, 2009, 8:42pm, lucifer wrote:
I'm gonna start dishing out internet beatings if people keep it up with this 4chan shit, I swear.


[image]
Llanilek
Dedicated Studio Member
****
[p:168]
member is offline

[avatar]

Former Elite Mod

[yim] [msn] [aim]
[homepage]

Joined: Oct 2005
Gender: Male
Posts: 921
Location: Y Drenewydd, North Wales, UK
Karma: 9
 Re: [PHP] Simple Private Messaging System
« Reply #2 on Jan 18, 2009, 10:54am »


Jan 18, 2009, 2:26am, Matt Murdock wrote:


phpmyadmin ftw!!!



AGREED !!! lol


Jan 18, 2009, 2:26am, Matt Murdock wrote:

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.


Jan 18, 2009, 2:26am, Matt Murdock wrote:

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:

Code:
$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



Jan 18, 2009, 2:26am, Matt Murdock wrote:

Wonderful tut!

[image]


thanks *takes a bow* lol
Link to Post - Back to Top  IP: Logged

[image]
Bruce Banner
Head Coder
******
Click Click Boom
member is offline

[avatar]



Joined: Nov 2006
Gender: Male
Posts: 17,332
Location: Nebraska
Karma: 311
 Re: [PHP] Simple Private Messaging System
« Reply #3 on Jan 19, 2009, 3:05am »

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.
Link to Post - Back to Top  IP: Logged

[image]
[image]

Coming soon(er or later) to SZ:
- [user] and [pmuser] UBBC tags
- Houdini
- CIv4
- Project Apocalypse
- Minimizer
Llanilek
Dedicated Studio Member
****
[p:168]
member is offline

[avatar]

Former Elite Mod

[yim] [msn] [aim]
[homepage]

Joined: Oct 2005
Gender: Male
Posts: 921
Location: Y Drenewydd, North Wales, UK
Karma: 9
 Re: [PHP] Simple Private Messaging System
« Reply #4 on Jan 19, 2009, 9:13am »


Jan 19, 2009, 3:05am, Bruce Banner wrote:
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.
Link to Post - Back to Top  IP: Logged

[image]
Bruce Banner
Head Coder
******
Click Click Boom
member is offline

[avatar]



Joined: Nov 2006
Gender: Male
Posts: 17,332
Location: Nebraska
Karma: 311
 Re: [PHP] Simple Private Messaging System
« Reply #5 on Jan 20, 2009, 3:20am »

:P Well, good to hear that.
Link to Post - Back to Top  IP: Logged

[image]
[image]

Coming soon(er or later) to SZ:
- [user] and [pmuser] UBBC tags
- Houdini
- CIv4
- Project Apocalypse
- Minimizer
Llanilek
Dedicated Studio Member
****
[p:168]
member is offline

[avatar]

Former Elite Mod

[yim] [msn] [aim]
[homepage]

Joined: Oct 2005
Gender: Male
Posts: 921
Location: Y Drenewydd, North Wales, UK
Karma: 9
 Re: [PHP] Simple Private Messaging System
« Reply #6 on Jan 27, 2009, 2:44pm »

wow this has had 294 clicks in 8 days from good-tutorials... best write the other part eh? lol.
Link to Post - Back to Top  IP: Logged

[image]
ricmetal
New Member
*
member is offline





Joined: Feb 2009
Gender: Male
Posts: 4
Karma: 0
 Re: [PHP] Simple Private Messaging System
« Reply #7 on Feb 22, 2009, 8:14pm »

tick tock, tick tock
google aint helping - no other tuts on this :)
second part available yet?
« Last Edit: Feb 22, 2009, 8:16pm by ricmetal »Link to Post - Back to Top  IP: Logged
Matt Murdock
Elite Mod
******
Member of Fredy's "I'm Whipped" Club
member is offline

[avatar]



Joined: Nov 2006
Gender: Male
Posts: 1,678
Location: Illinois
Karma: 18
 Re: [PHP] Simple Private Messaging System
« Reply #8 on Feb 22, 2009, 8:57pm »


Feb 22, 2009, 8:14pm, ricmetal wrote:
tick tock, tick tock
google aint helping - no other tuts on this :)
second part available yet?


Not yet :(
Link to Post - Back to Top  IP: Logged


Nov 10, 2009, 8:42pm, lucifer wrote:
I'm gonna start dishing out internet beatings if people keep it up with this 4chan shit, I swear.


[image]
Llanilek
Dedicated Studio Member
****
[p:168]
member is offline

[avatar]

Former Elite Mod

[yim] [msn] [aim]
[homepage]

Joined: Oct 2005
Gender: Male
Posts: 921
Location: Y Drenewydd, North Wales, UK
Karma: 9
 Re: [PHP] Simple Private Messaging System
« Reply #9 on Feb 25, 2009, 9:14am »

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!
Link to Post - Back to Top  IP: Logged

[image]
ricmetal
New Member
*
member is offline





Joined: Feb 2009
Gender: Male
Posts: 4
Karma: 0
 Re: [PHP] Simple Private Messaging System
« Reply #10 on Feb 25, 2009, 2:22pm »

"gureate!" \ ( ^ ^ ) /

Arigato
« Last Edit: Feb 25, 2009, 2:25pm by ricmetal »Link to Post - Back to Top  IP: Logged
Llanilek
Dedicated Studio Member
****
[p:168]
member is offline

[avatar]

Former Elite Mod

[yim] [msn] [aim]
[homepage]

Joined: Oct 2005
Gender: Male
Posts: 921
Location: Y Drenewydd, North Wales, UK
Karma: 9
 Re: [PHP] Simple Private Messaging System
« Reply #11 on Feb 25, 2009, 9:44pm »

its done

« Last Edit: Feb 25, 2009, 9:47pm by Llanilek »Link to Post - Back to Top  IP: Logged

[image]
ricmetal
New Member
*
member is offline





Joined: Feb 2009
Gender: Male
Posts: 4
Karma: 0
 Re: [PHP] Simple Private Messaging System
« Reply #12 on Apr 8, 2009, 11:52pm »

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:24am by ricmetal »Link to Post - Back to Top  IP: Logged
Matt Murdock
Elite Mod
******
Member of Fredy's "I'm Whipped" Club
member is offline

[avatar]



Joined: Nov 2006
Gender: Male
Posts: 1,678
Location: Illinois
Karma: 18
 Re: [PHP] Simple Private Messaging System
« Reply #13 on Apr 9, 2009, 2:43am »

Paste the part of your code that displays the date, please. :)
Link to Post - Back to Top  IP: Logged


Nov 10, 2009, 8:42pm, lucifer wrote:
I'm gonna start dishing out internet beatings if people keep it up with this 4chan shit, I swear.


[image]
   [Search This Thread][Send Topic To Friend] [Print]

Click Here To Make This Board Ad-Free


This Board Hosted For FREE By ProBoards
Get Your Own Free Message Boards & Free Forums!
Terms of Service | Privacy Policy | Report Abuse | Mobile