supermerc Posted December 6, 2006 Share Posted December 6, 2006 Im very new to php, and i was wondering if it was really hard to add a private messaging system to my site. And if anyone knows a good tutorial that teachs how to do it. Quote Link to comment Share on other sites More sharing options...
liam1412 Posted December 6, 2006 Share Posted December 6, 2006 I also want to knowt he answer to this question but my last post on the subject got ignored. >:( Any tutorials on this would be much appreciated from myself and supermerc Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 6, 2006 Share Posted December 6, 2006 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 sessionif (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. Quote Link to comment Share on other sites More sharing options...
liam1412 Posted December 6, 2006 Share Posted December 6, 2006 Onsidian your a legend. I can find a tutorial to make PHP do anything - Even dance the ballero or something. But what I cannot find is a PM tutorial. Hopefully I will be able to work through what you have shown and expand to comeup with the final thing. Thanks Quote Link to comment Share on other sites More sharing options...
liam1412 Posted December 6, 2006 Share Posted December 6, 2006 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 Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 6, 2006 Share Posted December 6, 2006 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 ;) Quote Link to comment Share on other sites More sharing options...
supermerc Posted December 6, 2006 Author Share Posted December 6, 2006 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. Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 6, 2006 Share Posted December 6, 2006 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! Quote Link to comment Share on other sites More sharing options...
supermerc Posted December 6, 2006 Author Share Posted December 6, 2006 I will for sure.But for now, How do i know if my members script uses cookies, and what they are? Quote Link to comment Share on other sites More sharing options...
marcus Posted December 6, 2006 Share Posted December 6, 2006 You can check if the cookies exist if you do something like:[code]<?php$cookie = setcookie("cookie","value");if($cookie == value){setcookie("cookie","value");}else {echo "Cookies are not enabled";};?>[/code] Quote Link to comment Share on other sites More sharing options...
xyn Posted December 6, 2006 Share Posted December 6, 2006 but using your code you coudl do....[code=php:0]?<php$cookie = setcookie("cookie","value");if($cookie != value){echo ("enable your cookies");exit;}?>[/code] Quote Link to comment Share on other sites More sharing options...
liam1412 Posted December 7, 2006 Share Posted December 7, 2006 I think I'll hang on for Your tutorial. Cookies seem to be a bit of an issue now anyway as alot of people have them turned off. Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 7, 2006 Share Posted December 7, 2006 [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]<?phpecho "<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. Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 12, 2006 Share Posted December 12, 2006 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. Quote Link to comment Share on other sites More sharing options...
liam1412 Posted December 12, 2006 Share Posted December 12, 2006 A gentleman, and a scholar. Legendary!!!!! :)Cheers Mate. Quote Link to comment Share on other sites More sharing options...
Accurax Posted December 12, 2006 Share Posted December 12, 2006 Excellant thankyou obsidian... ive just started doing this myself.... :)You are a star Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.