Jump to content

Personal Messaging System {Where Do I Start?}


d22552000

Recommended Posts

ok so I want ot make a VERY basic personall messaging system, and this is what I have so far

 

CREATE TABLE `pmbox` (
  `id` mediumint(32) NOT NULL auto_increment,
  `from` varchar(64) NOT NULL,
  `to` varchar(64) NOT NULL,
  `subject` varchar(255) NOT NULL default '[No Subject]',
  `message` text NOT NULL,
  `timedate` timestamp NOT NULL default CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

That is an export of my mysql table that stores the messages.

 

<form action="newpm.php" method="post">
<input type="text" name="touser" />
<input type="text" name="subject" />
<textarea name="text" cols="40" rows="8" />
</textarea>
</form>

 

Now what do I do exactly? lol....

I have made a registraition and login system.

 

The user variables are like this when they get to any files on my site:

 

$_SESSION['u'] = $user['username'];
$_SESSION['i'] = $user['useridnum'];
$_SESSION['a'] = $user['accesslevel'];

 

Hope that helps someone out.

Alright first you are going to want to make the page which submits the data, because of how you set up mysql:

 

CREATE TABLE `pmbox` (
  `id` mediumint(32) NOT NULL auto_increment,
  `from` varchar(64) NOT NULL,
  `to` varchar(64) NOT NULL,
  `subject` varchar(255) NOT NULL default '[No Subject]',
  `message` text NOT NULL,
  `timedate` timestamp NOT NULL default CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

The page should look like this.

 

<?php
$db_name = //put the name of the database here
$db_pass = //put the user who accesses the database here
$db_user = //put the password for the user here
$user_db = //put the database which stores user information here
if(mysql_connect('localhost',$db_user,$db_pass)) {
	if(mysql_select_db($db_name)) {
		function user_exists($user) {
			$user_exists_query = mysql_query("SELECT * FROM " . $user_db . " WHERE userid='" . $user . "'");
			if(mysql_num_rows($user_exists_query)=>1) {
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		if($_SESSION['u']) {
			if($_POST['submit']) {
				if($_POST['subject'] && $_POST['user'] && $_POST['message']) {
					if(user_exists($_POST['user'])==TRUE) {
						if($_POST['user'] != $_SESSION['u']) {
							$from = $_SESSION['u'];
							$to = $_POST['user'];
							$subject = mysql_real_escape_string($_POST['subject']);
							$message = mysql_real_escape_string($_POST['message']);
							$timedate = date("h:i:s A")
							if(mysql_query("INSERT INTO pmbox (from, to, subject, message, timedate) VALUES ('$from','$to','$subject','$message','$timedate')")) {
								$message = 'Message left';
							}
						}
						else {
							$error = 'You cannot comment yourself';
						}
					}
					else {
						$error = 'Invalid username';
					}
				}
				else {
					$error = 'You must fill out all of the data';
				}
			}
		}
		mysql_close();
	}
	else {
		$error = 'Failed connecting to server. Please contact the site admin';
	}
}
else {
	$error = 'Failed connecting to server. Please contact the site admin';
}
$title = 'Send a message';
?>
<html>
<head>
	<title>
		<?php print $title; ?>
	</title>
</head>
<body>
	<?php
		if($_SESSION['u']) {
			if(isset($message)) {
				print $message;
			}
			else {
				if(isset($error)) {
					print $error;
					print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>';
					print '<input type=hidden name=user value=' . $_POST['user'] . ' />';
					print 'Subject:<br /><input type=text name=subject value=' . $_POST['subject'] . ' /><br />';
					print 'Message:<br /><textarea name=message>' . $_POST['message'] . '</textarea>';
					print '</form>';
				}
				else {
					if(!$_POST['submit']) {
						print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>';
						print '<input type=hidden name=user />';
						print 'Subject:<br /><input type=text name=subject /><br />';
						print 'Message:<br /><textarea name=message></textarea><br />';
						print '<input name=submit type=submit value=Submit />';
						print '</form>';
					}
				}
			}
		}
		else {
			print 'You must be logged in to view this page';
		}
	?>
</body>
</html>

 

Now to show the user there private messages you would do something like this:

 

<?php
$db_name = //put the name of the database here
$db_pass = //put the user who accesses the database here
$db_user = //put the password for the user here
$user_db = //put the database which stores user information here
if(mysql_connect('localhost',$db_user,$db_pass)) {
	if(mysql_select_db($db_name)) {
		if($_SESSION['u']) {
			$inbox_query = mysql_query("SELECT * FROM pmbox WHERE to='" . $_SESSION['u'] . " ORDER BY id DESC');
			if(mysql_num_rows($inbox_query)=>1) {
				print '<table>';
				while($inbox_array = mysql_fetch_array($inbox_query)) {
					print '<tr>';
					print '<td valign=top> ' . $inbox_array['from'] . '<br />' . $inbox_array['timedate'] . '</td>';
					print '<td valign=top> ' . $inbox_array['subject'] . '<hr />' $inbox_array['message'] . '</td>';
					print '</tr>';
				}
				print '</table>';
			}
			else {
				print 'You have no private messages';
			}
		}
		else {
			print 'You must be logged in to view this page';
		}
	}
	else {
		$error = 'Failed connecting to server. Please contact the site admin';
	}
}
else {
	$error = 'Failed connecting to server. Please contact the site admin';
}

 

This is a VERY small inbox script. The one that i am using is actually pretty big and includes showing new messages separate from older ones, replied, and much more. but this is a pretty good one with standard error checking and security

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.