Private Message System


First off you will need to run 1 mySQL query in phpMyAdmin and it is:
PHP Code
  1. CREATE TABLE `privates` (
  2. `pid` INT( 11 ) NOT NULL auto_increment,
  3. `to` VARCHAR( 255 ) NOT NULL,
  4. `from` VARCHAR( 255 ) NOT NULL,
  5. `date` VARCHAR( 255 ) NOT NULL,
  6. `status` CHAR( 6 ) NOT NULL default 'Unread',
  7. `subject` VARCHAR( 255 ) NOT NULL default 'Untitled Message',
  8. `content` TEXT NOT NULL,
  9. PRIMARY KEY(`pid`)
  10. );


NOTE: If you are using the Message Spyer or Mass PM you will need to change your queries and what-not.

Now, make a new file and name it: pms.php

In your new file you will want to type something up like:

PHP Code
  1. <?php
  2. session_start(); //Start session
  3. include("config.php"); //Include config file
  4. if(!$logged[id]){ //Check if user is logged in
  5. echo "<b>Error</b>: You Are Not Logged In!"; //Not logged in
  6. }else{ //Their loggedin
  7. switch($_GET[page]){ //make some links ?page=case
  8. default: //set up the default page upon going to pms.php
  9. $msgs = mysql_query("SELECT * FROM `privates` WHERE `to` = '" . $logged[username] . "' ORDER BY `pid` ASC") or die(mysql_error()); //get all the messages to the loged in user
  10. echo "<a href=\"pms.php?page=compose\">Compose Message</a>
  11. <a href=\"pms.php?page=delall\">Delete All Messages</a>
  12.  
  13. <table width=\"350\" cellpadding=\"0\" cellspacing=\"3\">
  14. <tr>
  15. <td align=\"center\" valign=\"middle\" width=\"100\">
  16. <b>Subject</b>
  17. </td>
  18. <td align=\"center\" valign=\"middle\" width=\"50\">
  19. <b>From</b>
  20. </td>
  21. <td align=\"center\" valign=\"middle\" width=\"50\">
  22. <b>Date Sent</b>
  23. </td>
  24. <td align=\"center\" valign=\"middle\" width=\"50\">
  25. <b>Status</b>
  26. </td>
  27. <td align=\"center\" valign=\"middle\" width=\"100\">
  28. <b>Delete Message</b>
  29. </td>
  30. </tr>"; //echo the start5 table and create msg link/delete all links!
  31. if(mysql_num_rows($msgs) == 0){ //check if there are messages or not
  32. echo "<tr><td width=\"300\" colspan=\"3\" align=\"center\" valign=\"middle\">You Have No New Messages!</td></tr>"; //no new messages
  33. }else{ //or if there are messages
  34. while($r = mysql_fetch_array($msgs)){ //repeat for all the messages
  35. echo "<tr><td align=\"center\" valign=\"middle\" width=\"100\">
  36. <a href=\"pms.php?page=view&id=$r[pid]\">$r[subject]</a></td>
  37. <td align=\"center\" valign=\"middle\" width=\"50\">
  38. <a href=\"members.php?user=$r[from]\">$r[from]</a>
  39. </td>
  40. <td align=\"center\" valign=\"middle\" width=\"50\">
  41. $r[date]
  42. </td>
  43. <td align=\"center\" valign=\"middle\" width=\"50\">
  44. $r[status]
  45. </td>
  46. <td align=\"center\" valign=\"middle\" width=\"100\">
  47. <a href=\"pms.php?page=delete&id=$r[pid]\">Delete</a>
  48. </td>
  49. </tr>"; //echo the messages
  50. } //end while
  51. } //end message amount check
  52. echo "</table>"; //end table
  53. break; //end the default page
  54. case 'view': //define the view page
  55. $id = (int)htmlspecialchars(strip_tags($_GET[id])); //make the ID safe
  56. if(!$id){ //if there is no ID to select
  57. echo "<a href=\"pms.php\">Go Back</a>No ID Selected!"; //echo the error
  58. }else{ //or if there is....
  59. $select = mysql_query("SELECT * FROM `privates` WHERE `pid` = '" . $id . "';"); //get the message's info
  60. $msg = mysql_fetch_array($select); //select all data
  61. if($msg[to] != $logged[username]){ //check if the user logged in is the owner of the message
  62. echo "<a href=\"pms.php\">Go Back</a>This Message Was Not Sent To You"; //if not
  63. }else{ //maybe...
  64. if(!$_POST[reply]) { //if the reply was not submitted
  65. $mark = mysql_query("UPDATE `privates` SET `status` = 'Read' WHERE `pid` = '" . $id . "'") or die(mysql_error()); //mark it as Read
  66. $message = nl2br(stripslashes($msg[content])); //make new lines to and strip the slashes
  67. $subject = stripslashes($msg[subject]); //strip the slashes
  68. echo "<a href=\"pms.php\">Go Back</a>
  69. <form method=\"post\">
  70. <dl style=\"margin: 0px;\">
  71. <dt><b>Subject</b>: $subject</dt>
  72. <dt><b>From</b>: $msg[from]</dt>
  73. <dt>$message</dt>
  74. <dt><textarea rows=\"6\" cols=\"45\" name=\"msg\"></textarea>
  75. <input type=\"submit\" name=\"reply\" value=\"Reply\"></dt>
  76. </dl>
  77. </form>"; //echo the message and reply box.
  78. }else{ //if the form was submitted
  79. $to = $msg[from]; //get who it is to
  80. $from = $logged[username]; //who its from
  81. $subject = "RE: " . $msg[subject]; //new subject
  82. $msg = addslashes($_POST[msg]); //the content
  83. $date = date("F j, Y, g:i a"); //the date sent
  84. $do = mysql_query("INSERT INTO `privates` (`to`,`from`,`date`,`subject`,`content`) VALUES ('" . $to . "','" . $from . "','" . $date . "','" . $subject . "','" . $msg . "')") or die(mysql_error()); //insert into the table!
  85. echo "Message Sent!"; //the message was sent
  86. } //end reply check
  87. } //end check posession
  88. } //end id check
  89. break;
  90. case 'compose': //create a new message
  91. if(!$_POST[send]){ //if the form was not submitted
  92. echo "<a href=\"pms.php\">Go Back</a>
  93. <form method=\"post\" action=\"\">
  94. <b>To User</b>:<Br />"; //echo some of the form and whatnot
  95. if(isset($_GET[user])){ //check if there is a user in the address bar
  96. echo "<input type=\"text\" name=\"to\" value=\"$_GET[user]\" size=\"15\">"; //if there is
  97. }else{ //or not..
  98. echo "<input type=\"text\" name=\"to\" size=\"15\">"; //echo the input box without the value of the user!
  99. } //end user check in address bar
  100. echo "<b>Subject</b>:
  101. <input type=\"text\" name=\"title\" value=\"Unitiled Message\" size=\"15\">
  102. <b>Content</b>:
  103. <textarea name=\"message\" rows=\"6\" cols=\"45\"></textarea>
  104. <input type=\"submit\" name=\"send\" value=\"Send Message\">
  105. </form>"; //echo the rest of the form
  106. }else{ //or if it was....
  107. $to = stripslashes(htmlspecialchars(strip_tags($_POST[to]))); //who its to
  108. $from = $logged[username]; //who its from
  109. $date = date("F j, Y, g:i a"); //the date sent
  110. $msg = addslashes($_POST[message]); //the message variable
  111. $subject = addslashes($_POST[title]); //the subject
  112. $do = mysql_query("INSERT INTO `privates` (`to`,`from`,`date`,`subject`,`content`) VALUES ('" . $to . "','" . $from . "','" . $date . "','" . $subject . "','" . $msg . "')") or die(mysql_error()); //insert into the table!
  113. echo "Message Sent!";
  114. } //end sent check
  115. break; //end make new msg
  116. case 'delall': //delete all page
  117. $get = mysql_query("SELECT * FROM `privates` WHERE `to` = '" . $logged[username] . "'"); //get the private messages
  118.  
  119. if(mysql_num_rows($get) == "0"){
  120. echo "You Have No Messages To Delete!";
  121. }else{
  122. $delete = mysql_query("DELETE FROM `privates` WHERE `to` = '" . $logged[username] . "'"); //delete tehm
  123. if($delete) { //check if theres a mySQL error
  124. echo "Messages Deleted"; //success
  125. }else{ //or not
  126. echo "mySQL Error Encountered!";
  127. } //end error check
  128. } //end msg check
  129. break; //end page
  130. case 'delete': //start the delete page!
  131. $id = (int)htmlspecialchars(strip_tags($_GET[id])); //make the ID safe
  132. if(!$id){ //if there is no ID to select
  133. echo "<a href=\"pms.php\">Go Back</a>No ID Selected!"; //echo the error
  134. }else{ //or if there is....
  135. $select = mysql_query("SELECT * FROM `privates` WHERE `pid` = '" . $id . "'"); //get the message's info
  136. $msg = mysql_fetch_array($select); //select all data
  137. if($msg[to] != $logged[username]){ //check if the user logged in is the owner of the message
  138. echo "<a href=\"pms.php\">Go Back</a>This Message Was Not Sent To You"; //if not
  139. }else{ //maybe...
  140. $do = mysql_query("DELETE FROM `privates` WHERE `pid` = '" . $id . "'") or die(mysql_error());
  141. echo "<a href=\"pms.php\">Go Back</a><Br />Messages Deleted!";
  142. } //end check possession
  143. } //end id check
  144. break; //end the delete page!
  145. } //end switch/get
  146. } //end login check
  147. ?>


If you encounter any errors please post them either in the comments section or on the forums!

This should work it came out clean during Debug.

PM system and User Profile Addon by shedh
For this tutorial you will need only the members.php page

on the members.php page find:
PHP Code
  1. Location: $user[location]<br>
  2. Sex: $user[sex]<br>
  3. Age: $user[age]


and replace with:
PHP Code
  1. Location: $user[location]<br>
  2. Sex: $user[sex]<br>
  3. Age: $user[age]<br>
  4. <a href='pms.php?page=compose&user=$user[username]'>Send Message</a>


Now when a user is looking at another users pofile he can quickly send a quick message, without going to another page and typing the other users username

Simple.
ShadowMage's Avatar
Author:
Views:
5,964
Rating:
Posted on Monday 11th August 2008 at 04:39 PM
zerocool
zerocool's Avatar
jambomb, use the techtuts system, it works and don't loggout. It happens the same to me with the RMB's tut, but using the TechTuts i haven't any probem
Posted on Sunday 10th August 2008 at 06:27 PM
jambomb
jambomb's Avatar
argh its annoying.. it keeps logging me out
Posted on Thursday 31st July 2008 at 03:26 PM
Dava
Dava's Avatar
that 1 on tech tuts ant much different from this 1
Posted on Thursday 31st July 2008 at 03:24 PM
jambomb
jambomb's Avatar
this is better... =]
Posted on Wednesday 11th June 2008 at 05:26 PM
Brandon
Brandon's Avatar
working PM system: https://www.techtuts.com/forums/index.php?showtopic=723
Posted on Friday 6th June 2008 at 08:56 PM
zerocool
zerocool's Avatar
When i do any actions, like compose or other, it does the acction but they log out from my session, what happen's?
the code is the same as this tutorial. Anyone have the same problem? How do you fixed it?
Posted on Tuesday 27th May 2008 at 04:18 PM
jambomb
jambomb's Avatar
when eva i send a message it says this message wasnt sent to you and wot let you view it lol!

any ideas?
Posted on Saturday 10th May 2008 at 01:41 PM
Adam981
Adam981's Avatar
wow alot of errors eh? just wondering befor i go and add this if the tut has been updated or do i need tog ot o each comments page and change what there saying?
Posted on Sunday 20th April 2008 at 05:14 PM
ShadowMage
ShadowMage's Avatar
I've added and played around it appears to be working fine.
Posted on Sunday 20th April 2008 at 01:15 PM
ShadowMage
ShadowMage's Avatar
Ill add this to my demo site of the user system when i get home later on then test with it.