Stats
Total Members: 597 [241]Total Tutorials: 284Newsest User: andrerds Todays Unique Hits: 116
Main Content
Tutorial: Private Message System By ShadowMage
First off you will need to run 1 mySQL query in phpMyAdmin and it is:
CREATE TABLE `privates` (
`pid` INT( 11 ) NOT NULL auto_increment,
`to` VARCHAR( 255 ) NOT NULL,
`from` VARCHAR( 255 ) NOT NULL,
`date` VARCHAR( 255 ) NOT NULL,
`status` CHAR( 6 ) NOT NULL default 'Unread',
`subject` VARCHAR( 255 ) NOT NULL default 'Untitled Message',
`content` TEXT NOT NULL,
PRIMARY KEY(`pid`)
);
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:
<?php
session_start (); //Start session
include( "config.php" ); //Include config file
if(! $logged [ id ]){ //Check if user is logged in
echo "<b>Error</b>: You Are Not Logged In!" ; //Not logged in
}else{ //Their loggedin
switch( $_GET [ page ]){ //make some links ?page=case
default: //set up the default page upon going to pms.php
$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
echo "<a href=\"pms.php?page=compose\">Compose Message</a>
<a href=\"pms.php?page=delall\">Delete All Messages</a>
<table width=\"350\" cellpadding=\"0\" cellspacing=\"3\">
<tr>
<td align=\"center\" valign=\"middle\" width=\"100\">
<b>Subject</b>
</td>
<td align=\"center\" valign=\"middle\" width=\"50\">
<b>From</b>
</td>
<td align=\"center\" valign=\"middle\" width=\"50\">
<b>Date Sent</b>
</td>
<td align=\"center\" valign=\"middle\" width=\"50\">
<b>Status</b>
</td>
<td align=\"center\" valign=\"middle\" width=\"100\">
<b>Delete Message</b>
</td>
</tr>" ; //echo the start5 table and create msg link/delete all links!
if( mysql_num_rows ( $msgs ) == 0 ){ //check if there are messages or not
echo "<tr><td width=\"300\" colspan=\"3\" align=\"center\" valign=\"middle\">You Have No New Messages!</td></tr>" ; //no new messages
}else{ //or if there are messages
while( $r = mysql_fetch_array ( $msgs )){ //repeat for all the messages
echo "<tr><td align=\"center\" valign=\"middle\" width=\"100\">
<a href=\"pms.php?page=view&id= $r [ pid ] \"> $r [ subject ] </a></td>
<td align=\"center\" valign=\"middle\" width=\"50\">
<a href=\"members.php?user= $r [ from ] \"> $r [ from ] </a>
</td>
<td align=\"center\" valign=\"middle\" width=\"50\">
$r [ date ]
</td>
<td align=\"center\" valign=\"middle\" width=\"50\">
$r [ status ]
</td>
<td align=\"center\" valign=\"middle\" width=\"100\">
<a href=\"pms.php?page=delete&id= $r [ pid ] \">Delete</a>
</td>
</tr>" ; //echo the messages
} //end while
} //end message amount check
echo "</table>" ; //end table
break; //end the default page
case 'view' : //define the view page
$id = (int) htmlspecialchars ( strip_tags ( $_GET [ id ])); //make the ID safe
if(! $id ){ //if there is no ID to select
echo "<a href=\"pms.php\">Go Back</a>No ID Selected!" ; //echo the error
}else{ //or if there is....
$select = mysql_query ( "SELECT * FROM `privates` WHERE `pid` = '" . $id . "';" ); //get the message's info
$msg = mysql_fetch_array ( $select ); //select all data
if( $msg [ to ] != $logged [ username ]){ //check if the user logged in is the owner of the message
echo "<a href=\"pms.php\">Go Back</a>This Message Was Not Sent To You" ; //if not
}else{ //maybe...
if(! $_POST [ reply ]) { //if the reply was not submitted
$mark = mysql_query ( "UPDATE `privates` SET `status` = 'Read' WHERE `pid` = '" . $id . "'" ) or die( mysql_error ()); //mark it as Read
$message = nl2br ( stripslashes ( $msg [ content ])); //make new lines to and strip the slashes
$subject = stripslashes ( $msg [ subject ]); //strip the slashes
echo "<a href=\"pms.php\">Go Back</a>
<form method=\"post\">
<dl style=\"margin: 0px;\">
<dt><b>Subject</b>: $subject </dt>
<dt><b>From</b>: $msg [ from ] </dt>
<dt> $message </dt>
<dt><textarea rows=\"6\" cols=\"45\" name=\"msg\"></textarea>
<input type=\"submit\" name=\"reply\" value=\"Reply\"></dt>
</dl>
</form>" ; //echo the message and reply box.
}else{ //if the form was submitted
$to = $msg [ from ]; //get who it is to
$from = $logged [ username ]; //who its from
$subject = "RE: " . $msg [ subject ]; //new subject
$msg = addslashes ( $_POST [ msg ]); //the content
$date = date ( "F j, Y, g:i a" ); //the date sent
$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!
echo "Message Sent!" ; //the message was sent
} //end reply check
} //end check posession
} //end id check
break;
case 'compose' : //create a new message
if(! $_POST [ send ]){ //if the form was not submitted
echo "<a href=\"pms.php\">Go Back</a>
<form method=\"post\" action=\"\">
<b>To User</b>:<Br />" ; //echo some of the form and whatnot
if(isset( $_GET [ user ])){ //check if there is a user in the address bar
echo "<input type=\"text\" name=\"to\" value=\" $_GET [ user ] \" size=\"15\">" ; //if there is
}else{ //or not..
echo "<input type=\"text\" name=\"to\" size=\"15\">" ; //echo the input box without the value of the user!
} //end user check in address bar
echo "<b>Subject</b>:
<input type=\"text\" name=\"title\" value=\"Unitiled Message\" size=\"15\">
<b>Content</b>:
<textarea name=\"message\" rows=\"6\" cols=\"45\"></textarea>
<input type=\"submit\" name=\"send\" value=\"Send Message\">
</form>" ; //echo the rest of the form
}else{ //or if it was....
$to = stripslashes ( htmlspecialchars ( strip_tags ( $_POST [ to ]))); //who its to
$from = $logged [ username ]; //who its from
$date = date ( "F j, Y, g:i a" ); //the date sent
$msg = addslashes ( $_POST [ message ]); //the message variable
$subject = addslashes ( $_POST [ title ]); //the subject
$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!
echo "Message Sent!" ;
} //end sent check
break; //end make new msg
case 'delall' : //delete all page
$get = mysql_query ( "SELECT * FROM `privates` WHERE `to` = '" . $logged [ username ] . "'" ); //get the private messages
if( mysql_num_rows ( $get ) == "0" ){
echo "You Have No Messages To Delete!" ;
}else{
$delete = mysql_query ( "DELETE FROM `privates` WHERE `to` = '" . $logged [ username ] . "'" ); //delete tehm
if( $delete ) { //check if theres a mySQL error
echo "Messages Deleted" ; //success
}else{ //or not
echo "mySQL Error Encountered!" ;
} //end error check
} //end msg check
break; //end page
case 'delete' : //start the delete page!
$id = (int) htmlspecialchars ( strip_tags ( $_GET [ id ])); //make the ID safe
if(! $id ){ //if there is no ID to select
echo "<a href=\"pms.php\">Go Back</a>No ID Selected!" ; //echo the error
}else{ //or if there is....
$select = mysql_query ( "SELECT * FROM `privates` WHERE `pid` = '" . $id . "'" ); //get the message's info
$msg = mysql_fetch_array ( $select ); //select all data
if( $msg [ to ] != $logged [ username ]){ //check if the user logged in is the owner of the message
echo "<a href=\"pms.php\">Go Back</a>This Message Was Not Sent To You" ; //if not
}else{ //maybe...
$do = mysql_query ( "DELETE FROM `privates` WHERE `pid` = '" . $id . "'" ) or die( mysql_error ());
echo "<a href=\"pms.php\">Go Back</a><Br />Messages Deleted!" ;
} //end check possession
} //end id check
break; //end the delete page!
} //end switch/get
} //end login check
?>
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:
Location: $user[location]<br>
Sex: $user[sex]<br>
Age: $user[age]
and replace with:
Location: $user[location]<br>
Sex: $user[sex]<br>
Age: $user[age]<br>
<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.
Difficulty: Moderate
Views: 4431
Comments
Posted on Saturday 9th June 2007 at 12:54 AM
MCP
Do not forget to include
PHP:
session_start(); //allows session
include "config.php";
right under the first php tag (<?PHP)
Posted on Saturday 9th June 2007 at 01:00 AM
ShadowMage
Ah yeah forgot that x.x; i'm used to having a header.php and footer.php where in header.php it includes the config file in there then i don't have to do it again lol
Posted on Saturday 9th June 2007 at 06:16 AM
MCP
"Duplicate entry '0' for key 1"
That shows when I try to send a message. Shadow? What is that?
Posted on Saturday 9th June 2007 at 01:12 PM
ShadowMage
replace mySQL query with:
CREATE TABLE `privates` (
`pid` INT( 11 ) NOT NULL auto_increment,
`to` VARCHAR( 255 ) NOT NULL,
`from` VARCHAR( 255 ) NOT NULL,
`date` VARCHAR( 255 ) NOT NULL,
`status` CHAR( 6 ) NOT NULL default 'Unread',
`subject` VARCHAR( 255 ) NOT NULL default 'Untitled Message',
`content` TEXT NOT NULL,
PRIMARY KEY(`pid`)
);
Posted on Saturday 9th June 2007 at 01:56 PM
ShadowMage
Just a quick thing:
Where you want to display how amny new PM's you have use:
PHP:
<?php
$getnew = mysql_query ( "SELECT * FROM `privates` WHERE `status` = 'Unread' AND `to` = ' $logged [ username ] ';" );
$total = mysql_num_rows ( $getnew );
echo "<a href=\"pms.php\">Private Messages</a>( $total )" ;
?>
Posted on Saturday 9th June 2007 at 03:44 PM
Diablosblizz
I am unable to check the message:
URL:
http://hotelmario.info/members/pms.php?page=view&=r[pid]
Posted on Saturday 9th June 2007 at 03:49 PM
Diablosblizz
I cannot delete them either. :S.
Posted on Saturday 9th June 2007 at 06:50 PM
ShadowMage
FIND:
<a href=\"pms.php?page=delete&id=r[id]\">Delete</a>
REPLACE WITH:
<a href=\"pms.php?page=delete&id=$r[id]\">Delete</a>
Posted on Saturday 9th June 2007 at 08:03 PM
MCP
"No ID Selected!"
That shows when I try viewing a message.
PS: Thanks for the display code (for displaying number of new messages). It works. :)
Posted on Saturday 9th June 2007 at 09:56 PM
ShadowMage
kk :) i'll look at the above error real quick =D
Add Comment
You must be logged in to post a comment.