MEMBUAT APLIKASI CHATTING SEDERHANA DENGAN PHP MYSQL
Tutorial PHP MySQLMEMBUAT APLIKASI CHATTING SEDERHANA DENGAN PHP MYSQL
Berikut ini adalah jendela utama obrolan.
berikutnya adalah membuat kode form login.
Selamat mencoba semoga bermanfaat...
Langkah 1. HTML
Seperti biasa, kita mulai dengan HTML.Berikut ini adalah jendela utama obrolan.
index.html
1 | < frameset rows = "65%,35%" framespacing = "1" frameborder = "yes" border = "1" bordercolor = "#FF0000" > |
2 | < frame src = "messages.php" name = "main_frame" > |
3 | < frame src = "main.php" name = "login_frame" scrolling = "no" noresize target = "middle" > |
4 | </ frameset > |
login_form.html
1 | < link type = "text/css" rel = "stylesheet" href = "styles.css" /> |
2 |
3 | < form class = "login_form" method = "post" action = "main.php" > |
4 | < div >Username: < input type = "text" name = "username" /></ div > |
5 | < div >Password: < input type = "password" name = "password" /></ div > |
6 | < div >< input type = "submit" value = "Login" name = "Login" /></ div > |
7 | </ form > |
8 | < div >You can use username "User1" or "User2" or "User3" and password "qwerty" to login in system</ div > |
chat_begin.html
1 | < link type = "text/css" rel = "stylesheet" href = "styles.css" /> |
2 |
3 | < div class = "chat_main" > |
4 | < h3 >Chat</ h3 > |
chat_end.html
1 | </ div > |
chat_input.html
1 | < link type = "text/css" rel = "stylesheet" href = "styles.css" /> |
2 |
3 | < form class = "submit_form" method = "post" action = "main.php" > |
4 | < div >< input type = "text" name = "s_message" />< input type = "submit" value = "Say" name = "s_say" /></ div > |
5 | </ form > |
6 | < div >You can type anything in chat</ div > |
Langkah 2. CSS
styles.css
01 | .login_form { |
02 | border : 1px solid #AAA ; |
03 | padding : 10px ; |
04 | } |
05 |
06 | h 3 { margin-top : 3px ;} |
07 |
08 | .chat_main { |
09 | border : 1px solid #AAA ; |
10 | -moz-box-shadow: 0 0 10px #ccc ; |
11 | -webkit-box-shadow: 0 0 10px #ccc ; |
12 | width : 350px ; |
13 | padding : 10px ; |
14 | background : #f3f3f3 ; |
15 | } |
16 |
17 | .message { |
18 | border : 1px solid #AAA ; |
19 | margin : 4px ; |
20 | padding : 5px ; |
21 | -moz-border-radius: 7px ; |
22 | -webkit-border-radius: 7px ; |
23 | background : #ffffff ; |
24 | } |
25 |
26 | .textf { |
27 | -moz-box-shadow: 0 0 10px #CCCCCC ; |
28 | -webkit-box-shadow: 0 0 10px #CCCCCC ; |
29 | border : 1px solid #CCCCCC ; |
30 | height : 40px ; |
31 | } |
32 |
33 | .submit { |
34 | -moz-border-radius: 7px ; |
35 | -webkit-border-radius: 7px ; |
36 | background : #F3F3F3 ; |
37 | border : 1px solid #CCCCCC ; |
38 | font-size : 16px ; |
39 | font-weight : bold ; |
40 | height : 35px ; |
41 | margin-left : 10px ; |
42 | padding : 5px ; |
43 | } |
44 | .message span { |
45 | font-size : 10px ; |
46 | color : #888 ; |
47 | margin-left : 10px ; |
48 | } |
49 |
50 | .submit_form { |
51 | margin : 10px 0px ; |
52 | } |
Langkah 3. SQL
Buat table seperti di bawah ini, fungsi nya yaitu untuk menampug semua percakapan.
1 | CREATE TABLE `s_chat_messages` ( |
2 | `id` INT (11) NOT NULL AUTO_INCREMENT , |
3 | ` user ` VARCHAR (255) NOT NULL , |
4 | `message` VARCHAR (255) NOT NULL , |
5 | ` when ` INT (11) NOT NULL , |
6 | PRIMARY KEY (`id`) |
7 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8; |
Langkah 4. PHP
Agar aplikasi chat ini dapat berjalan sesuai dengan fungsi nya, maka langkah yang harus di lakukan adalah membuat file PHP, langsung saja copas script yang ada di bawah ini..
main.php
01 | <?php |
02 |
03 | // set error reporting level |
04 | if (version_compare(phpversion(), "5.3.0" , ">=" ) == 1) |
05 | error_reporting (E_ALL & ~E_NOTICE & ~E_DEPRECATED); |
06 | else |
07 | error_reporting (E_ALL & ~E_NOTICE); |
08 |
09 | require_once ( 'inc/login.inc.php' ); |
10 | require_once ( 'inc/chat.inc.php' ); |
11 |
12 | // initialization of login system and generation code |
13 | $oSimpleLoginSystem = new SimpleLoginSystem(); |
14 |
15 | $oSimpleChat = new SimpleChat(); |
16 |
17 | // draw login box |
18 | echo $oSimpleLoginSystem ->getLoginBox(); |
19 |
20 | // draw chat application |
21 | $sChatResult = 'Need login before using' ; |
22 | if ( $_COOKIE [ 'member_name' ] && $_COOKIE [ 'member_pass' ]) { |
23 | if ( $oSimpleLoginSystem ->check_login( $_COOKIE [ 'member_name' ], $_COOKIE [ 'member_pass' ])) { |
24 | $sChatResult = $oSimpleChat ->acceptMessages(); |
25 | } |
26 | } |
27 | echo $sChatResult ; |
28 |
29 | ?> |
messages.php
1 | <meta http-equiv= "refresh" content= "5" > |
2 | <?php |
3 |
4 | require_once ( 'inc/chat.inc.php' ); |
5 | $oSimpleChat = new SimpleChat(); |
6 | echo $oSimpleChat ->getMessages(); |
7 |
8 | ?> |
inc/chat.inc.php
01 | <?php |
02 |
03 | // simple chat class |
04 | class SimpleChat { |
05 |
06 | // DB variables |
07 | var $sDbName ; |
08 | var $sDbUser ; |
09 | var $sDbPass ; |
10 |
11 | // constructor |
12 | function SimpleChat() { |
13 | //mysql_connect("localhost","username","password"); |
14 | $this ->sDbName = 'database_name' ; |
15 | $this ->sDbUser = 'username' ; |
16 | $this ->sDbPass = 'password' ; |
17 | } |
18 |
19 | // adding to DB table posted message |
20 | function acceptMessages() { |
21 | if ( $_COOKIE [ 'member_name' ]) { |
22 | if (isset( $_POST [ 's_say' ]) && $_POST [ 's_message' ]) { |
23 | $sUsername = $_COOKIE [ 'member_name' ]; |
24 |
25 | //the host, name, and password for your mysql |
26 | $vLink = mysql_connect( "localhost" , $this ->sDbUser, $this ->sDbPass); |
27 |
28 | //select the database |
29 | mysql_select_db( $this ->sDbName); |
30 |
31 | $sMessage = mysql_real_escape_string( $_POST [ 's_message' ]); |
32 | if ( $sMessage != '' ) { |
33 | mysql_query( "INSERT INTO `s_chat_messages` SET `user`='{$sUsername}', `message`='{$sMessage}', `when`=UNIX_TIMESTAMP()" ); |
34 | } |
35 |
36 | mysql_close( $vLink ); |
37 | } |
38 | } |
39 |
40 | ob_start(); |
41 | require_once ( 'chat_input.html' ); |
42 | $sShoutboxForm = ob_get_clean(); |
43 |
44 | return $sShoutboxForm ; |
45 | } |
46 |
47 | function getMessages() { |
48 | $vLink = mysql_connect( "localhost" , $this ->sDbUser, $this ->sDbPass); |
49 |
50 | //select the database |
51 | mysql_select_db( $this ->sDbName); |
52 |
53 | //returning the last 15 messages |
54 | $vRes = mysql_query( "SELECT * FROM `s_chat_messages` ORDER BY `id` ASC LIMIT 15" ); |
55 |
56 | $sMessages = '' ; |
57 |
58 | // collecting list of messages |
59 | if ( $vRes ) { |
60 | while ( $aMessages = mysql_fetch_array( $vRes )) { |
61 | $sWhen = date ( "H:i:s" , $aMessages [ 'when' ]); |
62 | $sMessages .= '<div class="message">' . $aMessages [ 'user' ] . ': ' . $aMessages [ 'message' ] . '<span>(' . $sWhen . ')</span></div>' ; |
63 | } |
64 | } else { |
65 | $sMessages = 'DB error, create SQL table before' ; |
66 | } |
67 |
68 | mysql_close( $vLink ); |
69 |
70 | ob_start(); |
71 | require_once ( 'chat_begin.html' ); |
72 | echo $sMessages ; |
73 | require_once ( 'chat_end.html' ); |
74 | return ob_get_clean(); |
75 | } |
76 | } |
77 |
78 | ?> |
inc/login.inc.php
01 | <? |
02 |
03 | // class SimpleLoginSystem |
04 | class SimpleLoginSystem { |
05 |
06 | // variables |
07 | var $aExistedMembers ; // Existed members array |
08 |
09 | // constructor |
10 | function SimpleLoginSystem() { |
11 | $this ->aExistedMembers = array ( |
12 | 'User1' => 'd8578edf8458ce06fbc5bb76a58c5ca4' , |
13 | 'User2' => 'd8578edf8458ce06fbc5bb76a58c5ca4' , |
14 | 'User3' => 'd8578edf8458ce06fbc5bb76a58c5ca4' |
15 | ); |
16 | } |
17 |
18 | function getLoginBox() { |
19 | ob_start(); |
20 | require_once ( 'login_form.html' ); |
21 | $sLoginForm = ob_get_clean(); |
22 |
23 | $sLogoutForm = '<a href="' . $_SERVER [ 'PHP_SELF' ]. '?logout=1">logout</a>' ; |
24 |
25 | if ((int) $_REQUEST [ 'logout' ] == 1) { |
26 | if (isset( $_COOKIE [ 'member_name' ]) && isset( $_COOKIE [ 'member_pass' ])) |
27 | $this ->simple_logout(); |
28 | } |
29 |
30 | if ( $_REQUEST [ 'username' ] && $_REQUEST [ 'password' ]) { |
31 | if ( $this ->check_login( $_REQUEST [ 'username' ], MD5( $_REQUEST [ 'password' ]))) { |
32 | $this ->simple_login( $_REQUEST [ 'username' ], $_REQUEST [ 'password' ]); |
33 | return 'Hello ' . $_REQUEST [ 'username' ] . '! ' . $sLogoutForm ; |
34 | } else { |
35 | return 'Username or Password is incorrect' . $sLoginForm ; |
36 | } |
37 | } else { |
38 | if ( $_COOKIE [ 'member_name' ] && $_COOKIE [ 'member_pass' ]) { |
39 | if ( $this ->check_login( $_COOKIE [ 'member_name' ], $_COOKIE [ 'member_pass' ])) { |
40 | return 'Hello ' . $_COOKIE [ 'member_name' ] . '! ' . $sLogoutForm ; |
41 | } |
42 | } |
43 | return $sLoginForm ; |
44 | } |
45 | } |
46 |
47 | function simple_login( $sName , $sPass ) { |
48 | $this ->simple_logout(); |
49 |
50 | $sMd5Password = MD5( $sPass ); |
51 |
52 | $iCookieTime = time() + 24*60*60*30; |
53 | setcookie( "member_name" , $sName , $iCookieTime , '/' ); |
54 | $_COOKIE [ 'member_name' ] = $sName ; |
55 | setcookie( "member_pass" , $sMd5Password , $iCookieTime , '/' ); |
56 | $_COOKIE [ 'member_pass' ] = $sMd5Password ; |
57 | } |
58 |
59 | function simple_logout() { |
60 | setcookie( 'member_name' , '' , time() - 96 * 3600, '/' ); |
61 | setcookie( 'member_pass' , '' , time() - 96 * 3600, '/' ); |
62 |
63 | unset( $_COOKIE [ 'member_name' ]); |
64 | unset( $_COOKIE [ 'member_pass' ]); |
65 | } |
66 |
67 | function check_login( $sName , $sPass ) { |
68 | return ( $this ->aExistedMembers[ $sName ] == $sPass ); |
69 | } |
70 | } |
71 |
72 | ?> |
Selamat mencoba semoga bermanfaat...