ساخت سیستم ارسال پیام خصوصی در PHP
در این مقاله آموزش PHP قصد داریم به شما نحوه ساخت یک سیستم ارسال پیام خصوصی در PHP را نشان بدیم.
در سایت های شبکه اجتماعی مثل فیسبوک یا فیسنما دیدید که می توانید بصورت مثلا تیکت در سایت های تجاری بین کاربران مختلف پیام خصوصی ارسال کنید.
در این مقاله آموزشی قصد داریم یک سیستم Private Message ایجاد کنیم که در ابتدا قابلیت ثبت نام و لاگین کاربران را دارد.
کاربران با داشتن نام کاربری (Username) فرد موجود در سیستم شما امکان چت کردن و ارسال و دریافت پیام را بصورت خصوصی خواهد داشت.
همچنین تمام پیام ها بصورت تفکیک شده به عنوان خوانده شده/نشده (Unread) در دسترس خواهد بود.
کاربران می توانند لیست کلیه کاربران را ببینند و با کلیک روی آن پروفایل آنها را مشاهده و برای آنها پیام خصوصی (PM) ارسال کنند
کل این سیستم ارسال پیام خصوصی در PHP را می توانید شخصی سازی کنید و مطابق سلیقه و نیاز کاربران کاملا تغییر بدید, قابلیت های جدیدی اضافه کنید و یا حتی قالب و ظاهر اسکریپت تحت وب را تغییر بدید.
این اسکریپت را میتوانید به عنوان یک قابلیت به پروژه های خود اضافه و استفاده کنید تا یک بخش پیام خصوصی را در سایت خود قرار بدید که کاربران با داشتن یوزرنیم کاربر مورد نظر امکان ارسال و دریافت پیام خصوصی را داشته باشند.
فهرست مطالب
سیستم ارسال پیام شخصی ما دارای ۳ صفحه اصلی زیر است.
- لیست تمام پیام ها
- خواندن یک پیام
- ارسال یک پیام
ساخت سیستم ارسال پیام خصوصی در PHP
ساخت دیتابیس
در ابتدا نیاز است یک دیتابیس با دو جدول users
و pm
بسازیم. این دو جدول تمام اطلاعات کاربران و پیام های ردوبدل شده را ذخیره می کند.
فایل sql
خروجی هر دوی این جداول به همراه پروژه کامل از بخش باکس دانلود قابل دریافت است.
سیستم ارسال پیام خصوصی در PHP با استفاده از mysqli به دیتابیس متصل و عملیات مربوط به پایگاه داده را انجام می دهد. (آموزش اتصال به دیتابیس با mysqli)
لیست تمام پیام ها
در این صفحه, لیست تمام پیام های کاربر نمایش داده می شود. پیام های کاربر در دو لیست خوانده شده / خوانده نشده دسته بندی شده است.
List_pm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
include(‘config.php’);
?>
!doctype html>
html>
head>
meta charset=“utf-8”>
link href=“ echo $design; ?>/style.css” rel=“stylesheet” title=“Style” >
title>Personnal Messages/title>
/head>
body>
div class=“header”>
a href=“ echo $url_home; ?>“>img src=“ echo $design; ?>/images/logo.png” alt=“Members Area” >/a>
/div>
div class=“content”>
//We check if the user is logged
if(isset($_SESSION[‘username’]))
{
//We list his messages in a table
//Two queries are executes, one for the unread messages and another for read messages
$req1 = mysqli_query($db,‘select m1.id, m1.title, m1.timestamp, count(m2.id) as reps, users.id as userid, users.username from pm as m1, pm as m2,users where ((m1.user1=”‘.$_SESSION[‘userid’].‘” and m1.user1read=”no” and users.id=m1.user2) or (m1.user2=”‘.$_SESSION[‘userid’].‘” and m1.user2read=”no” and users.id=m1.user1)) and m1.id2=”1″ and m2.id=m1.id group by m1.id order by m1.id desc’);
$req2 = mysqli_query($db,‘select m1.id, m1.title, m1.timestamp, count(m2.id) as reps, users.id as userid, users.username from pm as m1, pm as m2,users where ((m1.user1=”‘.$_SESSION[‘userid’].‘” and m1.user1read=”yes” and users.id=m1.user2) or (m1.user2=”‘.$_SESSION[‘userid’].‘” and m1.user2read=”yes” and users.id=m1.user1)) and m1.id2=”1″ and m2.id=m1.id group by m1.id order by m1.id desc’);
?>
This is the list of your messages:br >
a href=“new_pm.php” class=“link_new_pm”>New PM/a>br >
h3>Unread Messages( echo intval(mysqli_num_rows($req1)); ?>):/h3>
table>
tr>
th class=“title_cell”>Title/th>
th>Nb. Replies/th>
th>Participant/th>
th>Date of creation/th>
/tr>
//We display the list of unread messages
while($dn1 = mysqli_fetch_array($req1))
{
?>
tr>
td class=“left”>a href=“read_pm.php?id= echo $dn1[‘id’]; ?>“> echo htmlentities($dn1[‘title’], ENT_QUOTES, ‘UTF-8’); ?>/a>/td>
td> echo $dn1[‘reps’]–1; ?>/td>
td>a href=“profile.php?id= echo $dn1[‘userid’]; ?>“> echo htmlentities($dn1[‘username’], ENT_QUOTES, ‘UTF-8’); ?>/a>/td>
td> echo date(‘Y/m/d H:i:s’ ,$dn1[‘timestamp’]); ?>/td>
/tr>
}
//If there is no unread message we notice it
if(intval(mysqli_num_rows($req1))==)
{
?>
tr>
td colspan=“4” class=“center”>You have no unread message./td>
/tr>
}
?>
/table>
br >
h3>Read Messages( echo intval(mysqli_num_rows($req2)); ?>):/h3>
table>
tr>
th class=“title_cell”>Title/th>
th>Nb. Replies/th>
th>Participant/th>
th>Date or creation/th>
/tr>
//We display the list of read messages
while($dn2 = mysqli_fetch_array($req2))
{
?>
tr>
td class=“left”>a href=“read_pm.php?id= echo $dn2[‘id’]; ?>“> echo htmlentities($dn2[‘title’], ENT_QUOTES, ‘UTF-8’); ?>/a>/td>
td> echo $dn2[‘reps’]–1; ?>/td>
td>a href=“profile.php?id= echo $dn2[‘userid’]; ?>“> echo htmlentities($dn2[‘username’], ENT_QUOTES, ‘UTF-8’); ?>/a>/td>
td> echo date(‘Y/m/d H:i:s’ ,$dn2[‘timestamp’]); ?>/td>
/tr>
}
//If there is no read message we notice it
if(intval(mysqli_num_rows($req2))==)
{
?>
tr>
td colspan=“4” class=“center”>You have no read message./td>
/tr>
}
?>
/table>
}
else
{
echo ‘You must be logged to access this page.’;
}
?>
/div>
div class=“foot”>a href=“ echo $url_home; ?>“>Go Home/a> – a href=“https://netparadis.com/”>NetParadis/a>/div>
/body>
/html>
|
خواندن یک پیام
این صفحه به کاربر اجازه خواندن یک پیام را می دهد. همچنین کاربر می تواند با کلیک روی دکمه reply به آن پیام پاسخی ارسال کند
Read_pm.php
You dont have the rights to access this page.
‘;
}
}
else
{
echo ‘
‘;
}
}
else
{
echo ‘
‘;
}
}
else
{
echo ‘
‘;
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
include(‘config.php’);
?>
!doctype html>
html>
head>
meta charset=“utf-8”>
link href=“ echo $design; ?>/style.css” rel=“stylesheet” title=“Style” >
title>Read a PM/title>
/head>
body>
div class=“header”>
a href=“ echo $url_home; ?>“>img src=“ echo $design; ?>/images/logo.png” alt=“Members Area” >/a>
/div>
//We check if the user is logged
if(isset($_SESSION[‘username’]))
{
//We check if the ID of the discussion is defined
if(isset($_GET[‘id’]))
{
$id = intval($_GET[‘id’]);
//We get the title and the narators of the discussion
$req1 = mysqli_query($db,‘select title, user1, user2 from pm where id=”‘.$id.‘” and id2=”1″‘);
$dn1 = mysqli_fetch_array($req1);
//We check if the discussion exists
if(mysqli_num_rows($req1)==1)
{
//We check if the user have the right to read this discussion
if($dn1[‘user1’]==$_SESSION[‘userid’] or $dn1[‘user2’]==$_SESSION[‘userid’])
{
//The discussion will be placed in read messages
if($dn1[‘user1’]==$_SESSION[‘userid’])
{
mysqli_query($db,‘update pm set user1read=”yes” where id=”‘.$id.‘” and id2=”1″‘);
$user_partic = 2;
}
else
{
mysqli_query($db,‘update pm set user2read=”yes” where id=”‘.$id.‘” and id2=”1″‘);
$user_partic = 1;
}
//We get the list of the messages
$req2 = mysqli_query($db,‘select pm.timestamp, pm.message, users.id as userid, users.username, users.avatar from pm, users where pm.id=”‘.$id.‘” and users.id=pm.user1 order by pm.id2’);
//We check if the form has been sent
if(isset($_POST[‘message’]) and $_POST[‘message’]!=”)
{
$message = $_POST[‘message’];
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}
//We protect the variables
$message = mysqli_real_escape_string($db,nl2br(htmlentities($message, ENT_QUOTES, ‘UTF-8’)));
//We send the message and we change the status of the discussion to unread for the recipient
if(mysqli_query($db,‘insert into pm (id, id2, title, user1, user2, message, timestamp, user1read, user2read)values(“‘.$id.‘”, “‘.(intval(mysqli_num_rows($req2))+1).‘”, “”, “‘.$_SESSION[‘userid’].‘”, “”, “‘.$message.‘”, “‘.time().‘”, “”, “”)’) and mysqli_query($db,‘update pm set user’.$user_partic.‘read=”yes” where id=”‘.$id.‘” and id2=”1″‘))
{
?>
div class=“message”>Your message has successfully been sent.br >
a href=“read_pm.php?id= echo $id; ?>“>Go to the discussion/a>/div>
}
else
{
?>
div class=“message”>An error occurred while sending the message.br >
a href=“read_pm.php?id= echo $id; ?>“>Go to the discussion/a>/div>
}
}
else
{
//We display the messages
?>
div class=“content”>
h1> echo $dn1[‘title’]; ?>/h1>
table class=“messages_table”>
tr>
th class=“author”>User/th>
th>Message/th>
/tr>
while($dn2 = mysqli_fetch_array($req2))
{
?>
tr>
td class=“author center”>
if($dn2[‘avatar’]!=”)
{
echo ‘.htmlentities($dn2[‘avatar’]).‘” alt=”Image Perso” style=”max-width:100px;max-height:100px;” >’;
}
?>br >a href=“profile.php?id= echo $dn2[‘userid’]; ?>“> echo $dn2[‘username’]; ?>/a>/td>
td class=“left”>div class=“date”>Sent: echo date(‘m/d/Y H:i:s’ ,$dn2[‘timestamp’]); ?>/div>
echo $dn2[‘message’]; ?>/td>
/tr>
}
//We display the reply form
?>
/table>br >
h2>Reply/h2>
div class=“center”>
form action=“read_pm.php?id= echo $id; ?>“ method=“post”>
label for=“message” class=“center”>Message/label>br >
textarea cols=“40” rows=“5” name=“message” id=“message”>/textarea>br >
input type=“submit” value=“Send” >
/form>
/div>
/div>
}
}
else
{
echo ‘
‘; }
}
else
{
echo ‘
‘; }
}
else
{
echo ‘
‘; }
}
else
{
echo ‘
‘; }
?>
div class=“foot”>a href=“list_pm.php”>Go to my personnal messages/a> – a href=“https://NetParadis.com/”>NetParadis/a>/div>
/body>
/html>
|
ارسال یک پیام
این صفحه به کاربر اجازه ارسال یک پیام جدید (نه یک پاسخ به پیام قبلی) را می دهد. برای ارسال پیام نیاز است که کاربر نام کاربری مقصد را وارد کند.
New_pm.php
‘;
}
//We display the form
?>
New Personnal Message
You must be logged to access this page.
‘;
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
include(‘config.php’);
?>
!doctype html>
html>
head>
meta charset=“utf-8”>
link href=“ echo $design; ?>/style.css” rel=“stylesheet” title=“Style” >
title>New PM/title>
/head>
body>
div class=“header”>
a href=“ echo $url_home; ?>“>img src=“ echo $design; ?>/images/logo.png” alt=“Members Area” >/a>
/div>
//We check if the user is logged
if(isset($_SESSION[‘username’]))
{
$form = true;
$otitle = ”;
$orecip = ”;
$omessage = ”;
//We check if the form has been sent
if(isset($_POST[‘title’], $_POST[‘recip’], $_POST[‘message’]))
{
$otitle = $_POST[‘title’];
$orecip = $_POST[‘recip’];
$omessage = $_POST[‘message’];
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
$otitle = stripslashes($otitle);
$orecip = stripslashes($orecip);
$omessage = stripslashes($omessage);
}
//We check if all the fields are filled
if($_POST[‘title’]!=” and $_POST[‘recip’]!=” and $_POST[‘message’]!=”)
{
//We protect the variables
$title = mysqli_real_escape_string($db,$otitle);
$recip = mysqli_real_escape_string($db,$orecip);
$message = mysqli_real_escape_string($db,nl2br(htmlentities($omessage, ENT_QUOTES, ‘UTF-8’)));
//We check if the recipient exists
$dn1 = mysqli_fetch_array(mysqli_query($db,‘select count(id) as recip, id as recipid, (select count(*) from pm) as npm from users where username=”‘.$recip.‘”‘));
if($dn1[‘recip’]==1)
{
//We check if the recipient is not the actual user
if($dn1[‘recipid’]!=$_SESSION[‘userid’])
{
$id = $dn1[‘npm’]+1;
//We send the message
if(mysqli_query($db,‘insert into pm (id, id2, title, user1, user2, message, timestamp, user1read, user2read)values(“‘.$id.‘”, “1”, “‘.$title.‘”, “‘.$_SESSION[‘userid’].‘”, “‘.$dn1[‘recipid’].‘”, “‘.$message.‘”, “‘.time().‘”, “yes”, “no”)’))
{
?>
div class=“message”>The message has successfully been sent.br >
a href=“list_pm.php”>List of my personnal messages/a>/div>
$form = false;
}
else
{
//Otherwise, we say that an error occured
$error = ‘An error occurred while sending the message’;
}
}
else
{
//Otherwise, we say the user cannot send a message to himself
$error = ‘You cannot send a message to yourself.’;
}
}
else
{
//Otherwise, we say the recipient does not exists
$error = ‘The recipient does not exists.’;
}
}
else
{
//Otherwise, we say a field is empty
$error = ‘A field is empty. Please fill of the fields.’;
}
}
elseif(isset($_GET[‘recip’]))
{
//We get the username for the recipient if available
$orecip = $_GET[‘recip’];
}
if($form)
{
//We display a message if necessary
if(isset($error))
{
echo ‘
‘; }
//We display the form
?>
div class=“content”>
h1>New Personnal Message/h1>
form action=“new_pm.php” method=“post”>
Please fill the following form to send a personnal message.br >
label for=“title”>Title/label>input type=“text” value=“ echo htmlentities($otitle, ENT_QUOTES, ‘UTF-8’); ?>“ id=“title” name=“title” >br >
label for=“recip”>Recipientspan class=“small”>(Username)/span>/label>input type=“text” value=“ echo htmlentities($orecip, ENT_QUOTES, ‘UTF-8’); ?>“ id=“recip” name=“recip” >br >
label for=“message”>Message/label>textarea cols=“40” rows=“5” id=“message” name=“message”> echo htmlentities($omessage, ENT_QUOTES, ‘UTF-8’); ?>/textarea>br >
input type=“submit” value=“Send” >
/form>
/div>
}
}
else
{
echo ‘
‘; }
?>
div class=“foot”>a href=“list_pm.php”>Go to my personnal messages/a> – a href=“https://NetParadis.com/”>NetParadis/a>/div>
/body>
/html>
|
تنظیمات اصلی اسکریپت
در این فایل نیاز است که اطلاعات ورود به دیتابیس را در خط ۱۲
تغییر بدید تا به پایگاه داده شما متصل شود. همچنین نیاز است که آدرس روت این اسکریپت روی سرور یا لوکال را به همراه ایمیل تغییر بدید
همچنین می توانید آدرس پیش فرض HOME سایت را تغییر بدید.
در نهایت نیز می توانید یک تم با استایل اختصاصی برای این اسکریپت طراحی و نام فولدر آن را اینجا قرار بدید.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//We start sessions
session_start();
/******************************************************
——————Required Configuration—————
Please edit the following variables so the members area
can work correctly.
******************************************************/
//We log to the DataBase
$db = mysqli_connect(‘localhost’, ‘root’, ”, ‘bs’);
//Webmaster Email
$mail_webmaster = ‘example@example.com’;
//Top site root URL
$url_root = ‘http://localhost/pm/’;
/******************************************************
—————–Optional Configuration—————-
******************************************************/
//Home page file name
$url_home = ‘index.php’;
//Design Name
$design = ‘default’;
?>
|
نکته امنیتی
دقت کنید که در موارد مورد نیاز یک سری اعتبارسنجی ها انجام شده است . اگر قصد استفاده در پروژه واقعی را دارید برای امنیت بیشتر لطفا ورودی و خروجی های کاربر را با دقت اعتبارسنجی و escape کنید که با استفاده از توابع filter_Var و htmlspecialchars به راحتی می توانید اینکار را انجام بدید. (اعتبارسنجی فرم ها در php – آموزش htmlspecialchars)
امیدوارم از آموزش ساخت سیستم ارسال پیام خصوصی در PHP نهایت استفاده را برده باشید.