Help - Search - Members - Calendar
Full Version: Pm System W/ User Integration (t-pt:2)
Zymic Webmaster Forums > Zymic Free Web Hosting > Tutorials
uncled1023
Hello again... from popular demand, i am now going to show you how to enable a personal message system for your already budding website!! this will be one of the longer of the tutorial series. So here goes!!!

1)ok, first off, we will need to make a NEW file and call is db.php
2)include the following code in it.(remember to use the same information from the db_connect.php so you can work off the same database.
CODE
<?

    $dbhost = 'localhost';

    $dbusername = 'username';
  
    $dbpasswd = 'password';
  
    $database_name = 'databasename';
    

    $connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
    or die ("Couldn't connect to server.");
    
    
    $db = mysql_select_db("$database_name", $connection)
    or die("Couldn't select database.");
    ?>


3)Now save it and open your phpadmin
4)under the same username/password as your user database, create a new database with the following query.
CODE
CREATE TABLE `messages` (
`id` int(11) NOT NULL auto_increment,
`reciever` varchar(25) NOT NULL default '',
`sender` varchar(25) NOT NULL default '',
`subject` text NOT NULL,
`message` longtext NOT NULL,
`recieved` enum('1','0') default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM;


now go to your users table and add a field under username and call it pm_count with a default value of 0.

5)ok, now we are going to create a new file and call it inbox.php
6)put the following code into it:
CODE
<?
     require('db_connect.php');    // database connect script.

     session_start();
    $user = $_SESSION['username'];
    
    include 'db.php';
    if(!$user)
        {
        echo "<br><p>You are not logged in. Please go and log in.</p><br>";
        }
        
    else
        {
      
        $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
        $row = mysql_fetch_array ($sql);
        $pm_count = $row['pm_count'];
        
        $percent = $pm_count/'50';
        $percent = $percent * '100';
        ?>
<html>
<head>
<title>Inbox</title>
</head>
<body>
<center><b><p><? echo "$pm_count"." of 50 Total  |  "."$percent"."% full"; ?></p></b>
        </center>
        <br>
        <?
  
        $query = "SELECT id, sender, subject, message FROM messages WHERE reciever='$user'";
        $sqlinbox = mysql_query($query);
        
        
        if(!$sqlinbox)
            {
            ?>
            <p><? print '$query: '.$query.mysql_error();?></p>
            <?
            }
        
      
          
        elseif (!mysql_num_rows($sqlinbox) )
            {
            ?>
            <center><p><b>You have no messages to display</b></p></center>
            <?
            }
        
        

        else
            {
            
            ?>
            <center>
            <form name="send" method="post" action="deletemsg.php">
            <table width="80%">
              <tr>
                  <td width="75%" valign="top"><p><b><u>Subject</u></b></p></td>
                  <td width="120px" valign="top"><p><b><u>Sender</u></b></p></td>  
                  <td width="25px" valign="top"><p><b><u>Select</u></b></p></td>
              </tr>
            <?
          
            while($inbox = mysql_fetch_array($sqlinbox))
                {
              
                $pm_id = $inbox['id'];
                $sender = $inbox['sender'];
                $subject = $inbox['subject'];
                
                
                ?>
                <tr>
                  <td width="75%" valign="top"><p><a href="viewmsg.php?msg_id=<? echo $pm_id; ?>"><? echo $subject; ?></a></p></td>
                  <td width="120px" valign="top"><p><? echo $sender; ?></p></td>
                  <td width="25px" valign="top"><input name="pms[]" type="checkbox" value="<? echo $pm_id; ?>"></td>
                </tr>
                <?
                
                }
            ?>
              <tr>  
                <td colspan="3"><input type="submit" name="Submit" value="Delete Selected"></td>
                <td></td>
                <td></td>
              </tr>
            </table>
            </center>
            <?
            
            }
    
        
        }
    ?>
</body>
<html>


7)now you will save it.
8)create a new file and call it deletemsg.php and insert the following code:
CODE
<?
    session_start();
    header("Location:inbox.php");
    
    $user = $_SESSION['username'];
    
    include 'db.php';
    
    
    $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
    $row = mysql_fetch_array ($sql);
    $pm_count = $row['pm_count'];
            
    
    foreach($_POST['pms'] as $num => $pm_id)
        {
    
        mysql_query("DELETE FROM messages WHERE id='$pm_id' AND reciever='$user'");
            
        
        $pm_count = $pm_count - '1';
            
      
        mysql_query("UPDATE users SET pm_count='$pm_count' WHERE username='$user'");
        }
    ?>


9)save it and create a new file called viewmsg.php and insert the follwoing code:
CODE
<?
require('db_connect.php');    // database connect script.
    session_start();
    $user = $_SESSION['username'];
    
    if(!$user)
        {
        echo "<br><p>You are not logged in. Please go log in.</p><br>";
        }
        
    else
        {
        
        $msg_id = $_REQUEST['msg_id'];
        
        
        $view_msg = mysql_query("SELECT * FROM messages WHERE id = '$msg_id'");
        $msg = mysql_fetch_array($view_msg);
        
        $reciever = $msg['reciever'];
        $sender = $msg['sender'];
        $subject = $msg['subject'];
        $message = $msg['message'];
        
        
        if($reciever == $user)
            {
            
            mysql_query("UPDATE messages SET recieved='1' WHERE id = '$msg_id'");
            
            
            $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
            $row = mysql_fetch_array ($sql);
            $pm_count = $row['pm_count'];
            
            
            $percent = $pm_count/'50';
            $percent = $percent * '100';
            
          
            ?>



<html>
<head>
<title>View Message</title>
</head>
<body>
<center><b><p><table width="80%">
              <tr>
                <td width="120px"><p>From:</p></td>
                <td width=""><p><a href = "<? echo "../members.php?art=$sender"; ?>"><? echo $sender; ?></a></p></td>
              </tr>
              
              <tr>
                <td width="120px"><p>Subject:</p></td>
                <td width=""><p><? echo $subject; ?></p></td>
              </tr>
              
              <tr>    
                <td width="120px"><p>Message Body:</p></td>
                <td width=""><p><? echo $message; ?></p></td>
              </tr>
            </table>
            </center>
            <?
            }
        
        else
            {
            ?>
            <p>It appears you are trying to view someone else's private message. Please view your own private messages, or go away.</p>
            <?
            }
        }
    ?>
    
</body>
<html>


10)save the file and create a new file
11)call it compose.php and insert the following code into it:
CODE
<?
    session_start();
    $user = $_SESSION['username'];
    
    include 'db.php';
    
    
    if(!$user)
        {
        echo "<br><p>You are not logged in, please do so.</p><br>";
        }
        
    else
        {
        
        $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
        $row = mysql_fetch_array ($sql);
        $pm_count = $row['pm_count'];
        
        $percent = $pm_count/'50';
        $percent = $percent * '100';
        ?>



<html>
<head>
<title>Compose Message</title>
</head>
<body>
<b><p><? echo "$pm_count"." of 50 Total  |  "."$percent"."% full"; ?></p></b>
        </center>
        <br>
        <?php
        //So here we get the variable submitted through the form to this page
        $reciever = $_POST['username'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
        $error = '0';
        
      
        
        //If they are all blank we jsut say to compose a message
        if(!$reciever AND !$subject AND !$message)
            {
            ?>
            <p><b>Please compose a message.</b></p>
            <br>
            <?php
            }
        
        //Since this form was partially filled out we need to return an error message
        else
            {
            if (!$reciever)
                {
                $error = 'You must enter a reciever to your message';
                }
            
            if (!$subject)
                {
                $error = 'You must enter a subject';
                }
            
            if (!$message)
                {
                $error = 'You must enter a message';
                }
            
            //If the variable error is not set to zero, we have a problem and should show the error message
            if($error != '0')
                {
                echo "<p>$error</p><br>";
                }
            
            //There are no errors so far which means the form is completely filled out    
            else
                {
                //Are the trying to send a message to a real user or to something they just made up?
                $user_check = mysql_query("SELECT username FROM users WHERE username='$reciever'");
                $user_check = mysql_num_rows($user_check);
                
                //The user is real and not made up if this is true
                if($user_check > '0')
                    {
                    //There might already be a sessioned time variable, if so we need to get it for the flood check
                    $time = $_SESSION['time'];
                    
                    //If there is a time variable already, set it to the varialbe $old_time
                    if($time > '0')
                        {
                        $old_time = $time;
                        }
                    
                    //Here we get the minutes and seconds on the server time using the date function, and set that to the $time variable
                    //Now we find the difference between this time ($time) and the time that the page was submitted ($old_time)
                    $time = date('is');
                    $difference = $time - $old_time;
                    
                    $_SESSION['time'] = $time;
                    
                    //If the two times have a difference greater or equal to 15, which is 15 seconds, they can submit the message, this is for flood protection
                    if($difference >= '15')
                        {
                        //Get their private message count
                        $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$reciever'");
                        $row = mysql_fetch_array ($sql);
                        $pm_count = $row['pm_count'];
                        
                        //You cant have more than 50 private messages, if they try sending a message to a user with a full inbox return an error message
                        if(pm_count == '50')
                            {
                            $error = 'The user you are trying to send a message to has 50 private messages, sorry but we cant send your message untill that user deletes some of their messages.';
                            }
                            
                        else
                            {    
                                                       //We add the message to the pm count
$new_pm_count = ($pm_count + 1);

mysql_query("UPDATE users (pm_count) VALUES('$new_pm_count')") or die (mysql_error());


                            //And not we stick the message in the database with all the correct information
                            mysql_query("INSERT INTO messages (reciever, sender, subject, message) VALUES('$reciever', '$user', '$subject', '$message')") or die (mysql_error());
                            }
                            
                        //Let the user know everything went ok.
                        echo "<p><b>You have successfully sent a private message!</b></p><br>";
                        }
                    
                    //Since they are trying to send messages faster than every 15 seconds, give them an error message    
                    else
                        {
                        $error = 'You must wait 15 seconds before sending another private message';
                        }
                    }
                
                //If they mis spelled or, made up a username, then give an error message telling them its wrong.
                else
                    {
                    $error = 'That username does not exist, please try again. Remember to check your spelling, and dont make stuff up at random.';

                    }
                }
             }
            
        //Since we may have set the error variable to something while trying to send the messae, we need another error check
        if($error != '0')
            {
            echo "<p>$error</p><br>";
            }
            
        else
            {
            //Here's the form for the input
            ?>
            <form name="send" method="post" action="compose.php">
            <table width="80%">
              <tr>
                <td width="150px" align="left" valign="top"><p>Username</p></td>
                <td width="" align="left" valign="top"><input name="username" type="text" id="username" value="<? echo "$reciever"; ?>"></td>
              </tr>
              
              <tr>
                <td width="150px" align="left" valign="top"><p>Subject</p></td>
                <td width="" align="left" valign="top"><input name="subject" type="text" id="subject" value="<? echo "$subject"; ?>"></td>
              </tr>
              
              <tr>
                <td width="150px" align="left" valign="top"><p>Message Body</p></td>
                <td width="" align="left" valign="top"><textarea name="message" type="text" id="message" value="" cols="50" rows="10"></textarea></td>
              </tr>
                  
              <tr>  
                <td></td>
                <td><input type="submit" name="Submit" value="Send Message"></td>
              </tr>
            </table>
            </center>
            </form>
            <?php
            }
        
    ?>


      
</body>
</html>


12)hopefully your still interested, cause we are almost done!!!
13)now create a new file and call it sent.php and insert the following code into it:
CODE
<?
require('db_connect.php');    // database connect script.
    session_start();
    $user = $_SESSION['username'];
    
    include 'db.php';
    
    //Are they logged in or not?
    if(!$user)
        {
        echo "<br><p>You arent logged in, please do so.</p><br>";
        }
        
    else
        {
        //Get your private message count
        $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
        $row = mysql_fetch_array ($sql);
        $pm_count = $row['pm_count'];
        
        $percent = $pm_count/'50';
        $percent = $percent * '100';
        ?>


<html>
<head>
<title>Sent Messages</title>
</head>
<body>
<center><b><p><? echo "$pm_count"." of 50 Total  |  "."$percent"."% full"; ?></p></b>
        </center>
        <br>
        <?
                $query = "SELECT * FROM messages WHERE sender='$user' AND recieved='0'";
        $sqlinbox = mysql_query($query);
        
               if(!$sqlinbox)
            {
            ?>
            <p><? print '$query: '.$query.mysql_error();?></p>
            <?
            }
        
        
        elseif (!mysql_num_rows($sqlinbox) )
            {
            ?>
            <p><b>You have no un-recieved messages to display</b></p>
            <?
            }
        
        
        else
            {
            
            ?>
            <table width="80%" border="0">
              <tr>
                  <td width="" valign="top"><p><b><u>Subject</u></b></p></td>
                  <td width="120px" valign="top"><p><b><u>Sender</u></b></p></td>  
              </tr>
            <?
            
            while($inbox = mysql_fetch_array($sqlinbox))
                {
                $reciever = $inbox['reciever'];
                $subject = $inbox['subject'];
                ?>
                <tr>
                  <td width="" valign="top"><p><? echo "$subject"; ?></p></td>
                  <td width="120px" valign="top"><p><? echo "$reciever"; ?></p></td>  
                </tr>
                <?
                }
            echo "</table>";
            }
        }    
    ?>




</body>
</html>


14)and save it.


you are done!! you have just made a personal message system that only YOUR members can access!!! please keep tuning in for the rest of the user system tutorials!!

thank you and good night!
Soul Of Me
Very nice, ima try this out, thanks wink.gif
uncled1023
i remember when i was just starting, i hated those types of tutorials, because i didnt know squat and all i wanted was a working script. This allows new website creators to have a working script that is EASY to install and when they learn php a little more, they can then edit it all they want. not everyone has to use this tutorial, but if you would like me to move it elsewhere, you are admin, so i will listen...
wozzym
i like this because im learning php right now and it helps me see how everything works together. It helps. Thanks
oziyn
yeah im kinda same as wazzym and by just reading these codes im actually learning alot of stuff and rly...bud.....im not very good at this stuff and my website already has a login system and soon a pm/profile w/e hese trying to do system so imo hese doing a great job and i think its mostly self explanetory........
wozzym
also uncled, you could put the files into a folder and let people download it smile.gif but i guess they would have to go into it and change some stuff but it might be good wink.gif
uncled1023
yea, i might do that later on, when i get the tutorials section of my site up. smile.gif
wozzym
cool smile.gif good luck with that wink.gif on jakal horn?
oziyn
hey... yeah that is a good idea save some ppl the truble.........please tell us if u do il be checking in alot
RussellSprouts
I haven't done any code in Php, and I understood it. I do know other languages though... so that could help.
wozzym
I might tweak this a little bit and add it to a forum system i am creating...and probably some more scripts too biggrin.gif
MrTouz
Thank you very much. Exactly what i am looking for.

I am going to remove the database connection and use my own file (pretty much the same) and use a different way of to check if user is logged in...

i did not know if i had to add a field to my users saying Messages or create a table of messages... never knew how a pm system worked. Well you cleared that for me. I will come back to say if i could install it or not. Thanks a lot !
MrTouz
Damn dude, its working. But i can't compose... somehow it says

QUOTE
Parse error: syntax error, unexpected $end in /homepages/23/d222196706/htdocs/rabfly/pages/eng/compose.php on line 171


I guess this is his problem :

CODE
            <?php
            }
        
    ?>


but looks good to me.. it ends everything perfectly.

So i had to write a message from phpmyadmin and i werote one... but my username as the receiver and unknown as the sender... put some fake data and it freakin worked tongue.gif I had a message in my inbox from unknown tongue.gif

I just need to fix the compose :/


Ok so after a little search you are missing a }

this one :

CODE
    else
        {
        
        $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
        $row = mysql_fetch_array ($sql);
        $pm_count = $row['pm_count'];
        
        $percent = $pm_count/'50';
        $percent = $percent * '100';
        ?>


At the very begening... i was looking at all the 'braces' (if its what its called) and checking them all on my editor notepad++ (helps a lot) this brace was not highlighted RED so i was like.. Ok there is no end to it ? So i added one at the end

CODE
            <?php
            }
        }
    ?>


And the error was gone biggrin.gif And i composed a message and it worked tongue.gif i received it correctly ! This is just perfect !

I just need to style it up a little bit.

Thanks a lot !

__________________________________

Shit i forgot the pm_count does not work... i have 2 messages on my inbox but says 0/50 with 0% but i believe it is due to my mistake :

I set the pm_count to pm_count varchar(255) default 0. I dunno if its the right one tongue.gif You did not mention what to set as a type (since i suck at selecting a type) i just put the very first one. I believe the error is related... little help ?
MrTouz
Actually i see no where in your script an UPDATE or INSERT for the pm_count inside the users tables.

So basically the script does not update the users PM_COUNT. I am lost ? can't i see it :/
MrTouz
Bumping this lady.

Need to find out how to INSERT inside the PM_COUNT database inside the users table :/
uncled1023
k, sorry. didnt check this post for awhile..

you are correct, i forgot to put the update pm-count in it.

so, in compose, where it adds the message to the database, put the following code

CODE
$new_pm_count = ($pm_count + 1);

mysql_query("UPDATE users (pm_count) VALUES('$new_pm_count')") or die (mysql_error());
Gadgets4u
Hello Uncled1023,

I was hoping to ask you a few questions regarding some php scripts etc... do you think you could contact me maybe if you have aim, msn etc...

Regards
uncled1023
i have msn and aim, but im usually on msn. uncled1023@hotmail.com
MrTouz
Thanks a lot Uncled. I am updating my script now and i will check if it works biggrin.gif

CODE
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(pm_count) VALUES('-2')' at line 1

i recevied the message but there is an error.

I believe it is due to the negative value i have from deleting messages. Since the delete worked. I will delete all my messages and set the counter to Zero and check again.
MrTouz
I 'fixed' it.

CODE
mysql_query("UPDATE users_info SET pm_count='$new_pm_count' WHERE username='$reciever'") or die (mysql_error());


Your code once modifed (to make it work) was updating all the users number... so at the end everybody had 3 messages and I only received 3 so i had to add the WHERE username='$receiver'

Thanks a lot for your script. I finished it and pimped it out. I will show you how it looks once i am done with my little project.
uncled1023
mk, im glad i helped you out. I installed a similar one in my site, and it works perfectly. and its pretty easy to change.
MrTouz
Wellit took me 5 minutes to style yours. I can add couple different features to it very easily. I really do enjoy it tongue.gif
MaNiAkY-
this is awesome, i was looking for a similar script

i get this error:

CODE
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\modules\message\inbox.php on line 17


i had this error before, but i dont remember how to fix it. i tried 1-2 methods but no result

Edit:
I added (on first line of inbox.php):
CODE
<?php error_reporting(E_ALL ^E_NOTICE ^E_WARNING); ?>


but it still doesn't show me how many messages i have in inbox
MaNiAkY-
come on guys.. i still don't get it right mellow.gif

edit: nevermind.. i succed alone. thanks mellow.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.