Jump to content

private messaging


supermerc

Recommended Posts

Hmm... tutorials, I'm not sure of, but I'll try to give you a basic overview to the system here.

The basic structure to a PM system is simple. Broken down to it's simplest form, you have 3 attributes of a message: 1) Sender, 2) Recipient and 3) Message. Obviously, for your users' sake, you'd probably want to add a few more features, such as Date Sent and whether it's been read, but those can be easily dealt with. Let's take that simple structure and walk through the principle behind a PM system.

First, let's set up our database structure:
[code]
CREATE TABLE myPMs (
  id INT(11) AUTO_INCREMENT PRIMARY KEY,
  to_user INT(11) REFERENCES users(id),
  from_user INT(11) REFERENCES users(id),
  message TEXT
);
[/code]

As you can see, I'm assuming you have an integer id field in a user table for reference here. If you use something else, you can modify it to fit your needs. Once you have the structure set up, you should be well on your way. A PM system assumes that you have a membership or login system of some sort. At any given time, you should be able to pull the ID number of the current user. That's how you get the FROM field for your PMs. So, you might do something like this for your PM form:
[code]
<?php
// assuming $myID holds the current user ID based on login session

if (isset($_POST['submit'])) {
  $to = $_POST['to'];
  $from = $myID;
  $message = mysql_real_escape_string($_POST['message']);
  $sql = "INSERT INTO myPMs (to_user, from_user, message) VALUES ('$to', '$from', '$message')";
  mysql_query($sql);
}

echo "<form name=\"sendPM\" action=\"\" method=\"post\">\n";
echo "<select name=\"to\">\n";
$sql = mysql_query("SELECT * FROM users WHERE id != '$myID'");
echo "<option value=\"\"></option>\n";
while ($row = mysql_fetch_assoc($sql)) {
  echo "<option value=\"$row[id]\">$row[username]</option>\n";
}
echo "</select><br />\n";
echo "<textarea name=\"message\"></textarea><br />\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Send\" />\n";
echo "</form>\n";
?>
[/code]

Obviously, you'd want to run some form of validation on your fields, but overall, this will let a user send a PM. Now, the key is to display ONLY those messages intended for a specific user. To do so, you've got to keep a couple levels of checking in mind. When they are viewing their inbox, you might use a query such as this:
[code]
SELECT * FROM myPMs WHERE to_user = '$myID';
[/code]

Then, when they select an individual message, [b]make sure that it is still associated with their ID[/b]. This will keep a user from being able to simply change the ID number of the message in the URL and view other people's messages:
[code]
SELECT * FROM myPMs WHERE id = '$messID' AND to_user = '$myID';
[/code]

When dealing with a PM system, [b]always[/b] keep a user check tacked on to the end of your WHERE clause. Same thing when looking in your own sent folder:
[code]
SELECT * FROM myPMs WHERE from_user = '$myID';
[/code]

Anyway, this gives a very basic overview for a PM system. I may actually write up a more detailed tut for this and post it on the main site sometime. Hope it gives you some direction, though.
Link to comment
Share on other sites

Just stumbled across this tutorial.  What a stupid choice of text color on the back ground but i figure if I mess about with it change to colors then redisplay I might be able to read it. 

Anyway don't know if it is any good but the link is

[url=http://www.obsidweb.net/v2/tutorials.php?id=tutorials&category=php&tutid=2&code=yes&gfx=]http://www.obsidweb.net/v2/tutorials.php?id=tutorials&category=php&tutid=2&code=yes&gfx=[/url]

Like I said I haven't read it yet so it might not be any good
Link to comment
Share on other sites

It appears to be a good [i]concept[/i], but really not well executed. It's a good read to get your mind around the idea, but there are a bunch of improvements I'd recommend before using a script like that on a production site. As mentioned in the comments, it has several major vulnerabilities ;)
Link to comment
Share on other sites

Its not an important site, its just gonna be a site with random stuff like pics and art where members can hang out and stuff. Things might not be that great, its my first website with php I usually just design site and never do anything with them but now i wanted to actually go somewhere.

Soo yeah I dont really care if its perfect its more for learning than anything else.
Link to comment
Share on other sites

Well, if you all are interested, I'm working up a tutorial on a full PM system from the ground up. After your comments this morning, I figured there should be some good ones out there, so I figured I'd write one up. So far, I've got the structure and displays all working. You can see the work in progress at http://sandbox.guahanweb.com/pm/. When I get it done, I'll post it up on the PHPFreaks main site. I should be able to have [b]something[/b] up there by this weekend. If you all aren't in a rush, you can find some detailed help there once it's finished.

Let me know if you have any questions!
Link to comment
Share on other sites

[quote author=supermerc link=topic=117562.msg480009#msg480009 date=1165439155]
I will for sure.

But for now, How do i know if my members script uses cookies, and what they are?
[/quote]

The easiest way to see if you have any cookies set from your member script is to just echo out the whole $_COOKIE array to examine. Set up a page that is [b]inside[/b] your membership section (requiring a login), and then simply place the following on the page:
[code]
<?php
echo "<pre>\n";
print_r($_COOKIE);
echo "</pre>\n";
?>
[/code]

This will do a reciprocal printout of your $_COOKIE array and let you know if you have any cookies set for the current session. From there, you can determine what they're used for.
Link to comment
Share on other sites

Well, guys, for what it's worth, I've published my Private Messaging tutorial for you all. It took a bit longer to write than I had hoped, but I hope it will help you out. You can find it on the main site as [url=http://www.phpfreaks.com/tutorials/148/0.php]Private Messaging System[/url] under the [i]Membership Systems[/i] category.

Let me know if you have any questions.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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