Private Messaging Outbox on Your Website
- Private Messaging on Your Website
- Private Message Login
- Private Message Logout
- Private Message Sending Form
- Send Private Message
- Private Message Inbox — Received Messages
- Private Message Outbox — Sent Messages
- Check Private Message Session ID
- Delete Received Private Message
- Delete Sent Private Message

This page is a tutorial on putting a private messaging (PM) outbox on your website. It uses sessions, display security, PHP, MySQL, and a menu system. This outbox page has no Delete feature. For that, go to Delete Sent Private Message. The screenshot above is merely the regular inbox. Picture the outbox page as the same except there are no buttons. Note that with our PM system, you don't need to click somewhere in the outbox just to see the message. All messages are displayed along with who they are to, their subject, and their date—right there in the outbox. Feel free to delete any or all messages if your box gets too full.
Let's check out the code. It starts with a PHP include that insists on adding the php file checkid.php to this page's content. All that's in this file is:
session_start();
if(!isset($_SESSION['sessionid'])){
session_unset();
session_destroy();
header('location: message-login.php');
}else{
// session logged
}
This just does what it ought to: start a session (it started in the login page, actually, but we can say we restarted it even though we merely continued it and restarted the session timeout clock which is set for 24 to 48 minutes, depending on the PHP installation directives). We then check the session variable "sessionid" which was saved at login. If it's not set, the user is sent to the login page after unsetting and destroying the session. If it is set, the user gets to continue the script since the else conditional leads only to a comment that does nothing, but says it all: "session logged." So the user is okay and good to go.
Next we get ready to access the MySQL database by using the include with config.php in it. Then we get the session variable "username" into the PHP variable $fromuser. This is an OUTBOX. Do the math.
Next we get all the outgoing messages sent from $fromuser as long as their outdel flags are not set and we list the results with the most recent first.
We'll skip over the browser sniffing that empowers a CSS style adjustment for cosmetic purposes and a textCounter() function that really only is needed in the Private Message Sending Form page.
We'll skip over the CSS style section and menu and get to the table caption where we snuck in the user's user name via a quick PHP insert. HTML doesn't mind this type of mixture, nor does JavaScript, although the latter is a bit fussy when you try to load JavaScript variables from PHP variables. See our JSON page for more details, since JSON cures JavaScript's reluctance problems in this area.
Recall that we stuck the results of grabbing all your outgoing messages into a PHP resource result variable $sql. Now we use the mysql_fetch_array() function to get the record contents—of all records with your user name in the fromuser field—into arrays that we now loop through a row at a time to display on the screen. We put the messages' dates into $sent. For security, we use the htmlentities() function for screen display after using the stripslashes() function because all data got stored in the db with use of the mysql_real_escape_string() function which most people use for security.
Finally, we close MySQL.
Name this file message-outbox.php
<?php
include_once"checkid.php";
//message-outbox.php
include('config.php');
$fromuser = $_SESSION['username'];
$sql = mysql_query("SELECT * FROM privatemessages WHERE fromuser = '$fromuser' AND outdel = '0' ORDER BY
datesent DESC");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>Private Message Outbox — Sent Messages</TITLE>
<meta name="description" content="Private Message Outbox — Sent Messages">
<meta name="keywords" content="Private Message Outbox,Sent Messages,Private Messaging,Private Message,php,javascript, dhtml, DHTML">
<script language="javascript">
mactest=(navigator.userAgent.indexOf("Mac")!=-1) //My browser sniffers
Netscape=(navigator.appName.indexOf("Netscape") != -1)
msafari=(navigator.userAgent.indexOf("Safari")!= -1)
wsafari=0; if(!mactest&&msafari){wsafari=1;msafari=0}
is_opera = 0; if(window.opera){is_opera=1}
is_ie_mac = 0; is_ie=0;if(document.all){is_ie=1}
function fixwidth(){if(Netscape||is_opera){e=document.getElementById('box');e.style.width='826px';e=document.getElementById('menu');e.style.width='116px';}}
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit){field.value = field.value.substring(0, maxlimit);}
else{countfield.value = maxlimit - field.value.length;}}
</script>
<STYLE TYPE="text/css">
BODY {margin-left:0; margin-right:0; margin-top:0;text-align:left;background-color:#ccc}
p, li {font:13px Verdana; color:black;text-align:left}
h1 {font:bold 28px Verdana; color:black;text-align:center}
h2 {font:bold 24px Verdana;text-align:center}
h3 {font:bold 15px Verdana;}
#box {background-color:#eee;position:absolute;top:50px;left:150px;
width:850px;padding:10px;border:2px solid blue}
#table1 {width:100%;border:1px solid blue;text-align:center}
#menu {background-color:#eee;position:absolute;top:50px;left:0px;
width:130px;padding:5px;border:2px solid blue}
</STYLE>
</head>
<body onload="fixwidth()">
<h1>Private Message Outbox — Sent Messages</h1>
<div id='menu'>
<a HREF="send-message-form.php">Send Message</a><BR><BR>
<a HREF="message-inbox.php">Message Inbox</a><BR><BR>
<a HREF="message-outbox.php">Message Outbox</a><BR><BR>
<a HREF="message-delete-received.php">Delete Inbox<BR>Message</a><BR><BR>
<a HREF="message-delete-sent.php">Delete Outbox<BR>Message</a><BR><BR>
<a HREF="message-login.php">Login</a><BR><BR>
<a HREF="message-logout.php">Logout</a>
</div>
<div id='box'>
<table id='table1' border='1'>
<caption><b>Messages from: <?php echo $fromuser; ?></b></caption>
<tr><th>To</th><th>Subject</th><th>Date</th>
<th>Message</th></tr>
<?php
while($rows=mysql_fetch_array($sql)){
$sent=stripslashes($rows['datesent']);
echo "<tr><td>".htmlentities(stripslashes($rows['touser']), ENT_QUOTES)."</td>";
echo "<td>".htmlentities(stripslashes($rows['subject']), ENT_QUOTES)."</td>";
echo "<td>".date('Y/m/d',$sent)."</td>";
echo "<td>".htmlentities(stripslashes($rows['message']), ENT_QUOTES)."</td></tr>";}
mysql_close();
?>
</table>
</body>
</html>