Web Programming >> PHP Programming
|
[목차] |
제19장 대화방 만들기(MySQL)
4. 대화방 수정(귀속말 기능)
이번에는 귀속말 기능을 추가합니다. 귀속말 기능을 추가하기 위해서는 chat.html과 say.html문서를 수정해야 합니다. 1. 귀속말 기능 추가 파일명 : chat.html <? if(!$user_id || !$user_name) { echo "<SCRIPT>self.close();</SCRIPT>"; exit; }
########## 데이터베이스 서버에 연결한다. ########## $dbconn = mysql_connect("localhost","php","php99") or die("DB서버 연결에 실패하였습니다!");
########## 작업 데이터베이스로 sample을 선택한다. ########## $status = mysql_select_db("sample",$dbconn); if(!$status) { $errNO = mysql_errno($dbconn); $errMSG = mysql_error($dbconn);
echo("서버의 sample 데이터베이스연결에 실패하였습니다!<br>"); echo("에러코드 $errNO : $errMSG<br>"); exit; }
//message 디랙토리 절대경로 $message_dir = "./data";
//대화방 퇴장시 연결될 페이지 URL $redirect_url = "./room_list.html";
if(!$fontcolor) $fontcolor = "black"; if(!$fontsize) $fontsize = "3"; if(!$bold) $bold = "N"; if(!$italic) $italic = "N";
// 서브루틴 function error() { header("Content-type: text/html"); return print"<body bgcolor=ffffff><h1>에러임돠 열심히 해보세요</body></html>"; exit;
}
function write($username, $room_uid, $message, $tomsg, $ear,$message_dir){ global $fontsize, $fontcolor, $bold, $italic, $user_name;
$lines = file("${message_dir}/${room_uid}.html");
if($tomsg == "") { $tomsg = all; }
$total = count($lines); $start = $total - 20; if($start < 0) $start = 0; $end = $total;
if($bold == "Y") $message = "<b>$message</b>"; if($italic == "Y") $message = "<i>$message</i>"; $message = "<font size='${fontsize}' color='${fontcolor}'><b>${user_name}</b> : ${message}</font>";
$newmessage = "$tomsg|$username|$message<br>";
$fp=fopen ("${message_dir}/${room_uid}.html", "w");
for ($i = $start; $i < $end; $i++) { fwrite($fp, "$lines[$i]"); }
fwrite($fp, "$newmessage\n"); fclose($fp);
}
// if($room == "") { error; }
// LOG OUT if (!strcmp($logoff ,"ok")) { $date = date( "l dS of F Y h:i:s A" ); $message = "$message($date)"; write($user_name, $room_uid, $message, $tomsg, $ear,$message_dir);
// $query="delete from room_login where chat_id='$user_id' "; $result= mysql_query($query, $dbconn);
// 대화방에 입실한 회원이 없으면 대화방을 삭제하는 부분 $result= mysql_query("select * from room_login where room_uid='$room_uid' ", $dbconn); $Total = mysql_num_rows($result); if(!$Total) { $query="delete from room where uid='$room_uid' "; $result= mysql_query($query, $dbconn);
unlink("${message_dir}/${room_uid}.html");
}
print "<META HTTP-EQUIV=Refresh CONTENT='0; url=$redirect_url'>"; exit; }
header("Content-type: text/html"); print" <HTML> <HEAD> <TITLE>대화방</TITLE> <style> a:link, a:visited, a:active { text-decoration:none; color:#003366; } A:hover{color:blue;} body, tr, td { font-size : 10pt;} </style>
<SCRIPT LANGUAGE='JavaScript'> <!-- function FocusOn() { document.form_submit.message.focus(); return; }
function chat() { form_submit.submit();
}
//--> </SCRIPT>
</HEAD> <BODY bgcolor=#FFFFFF text=#000000 topmargin=5 onLoad=FocusOn()> <table cellspacing=0 cellpadding=1 border=0> <tr> <form name='form_submit' action='chat.html' method='post'> <input name=room_uid type=hidden value='$room_uid'> <td align=\"left\"> ";
$query = "SELECT uid,chat_id FROM room_login WHERE room_uid =$room_uid ORDER BY uid DESC";
$result= mysql_query($query); if (!$result) { error("QUERY_ERROR"); exit; }
$total_record = mysql_num_rows($result);
########## 게시물의 가상번호(게시물의 개수에 따른 일련번호) ########## ?> <select name="tomsg"> <option value="all">모두에게</option> <? for($i = 0; $i < $total_record; $i++) { $my_uid = mysql_result($result,$i,0); $my_chat_id = mysql_result($result,$i,1);
if($user_id != $my_chat_id) { $query5 = "SELECT name FROM member WHERE id = '$my_chat_id'"; $result5 = mysql_query($query5); if(!$result5) { error("QUERY_ERROR"); exit; } $my_chat_name = mysql_result($result5,0,0); ?> <option value="<?echo("$my_chat_id");?>" <?if($my_chat_id == $tomsg) echo(" selected");?>><?echo("$my_chat_name");?></option> <? } } print " </select> <input type=text name=message value='' size=45> <a href='javascript:form1.submit()'>Logout</a> </td> </tr> <tr> <td align=\"left\"> <INPUT TYPE=\"checkbox\" NAME=\"bold\" value='Y'"; if(!strcmp($bold,"Y")) echo(" checked"); print ">진하게 <INPUT TYPE=\"checkbox\" NAME=\"italic\" value='Y'"; if(!strcmp($italic,"Y")) echo(" checked"); print ">기울림 <select name=\"fontcolor\"> <option value=\"black\" "; if(!strcmp($fontcolor,"black")) echo(" selected"); print ">black</option> <option value=\"#004080\" "; if(!strcmp($fontcolor,"#004080")) echo(" selected"); print ">dark blue</option> <option value=\"#3232CD\""; if(!strcmp($fontcolor,"#3232CD")) echo(" selected"); print ">medium blue</option> <option value=\"purple\""; if(!strcmp($fontcolor,"purple")) echo(" selected"); print ">purple</option> <option value=\"green\""; if(!strcmp($fontcolor,"green")) echo(" selected"); print ">green</option> <option value=\"orange\""; if(!strcmp($fontcolor,"orange")) echo(" selected"); print ">orange</option> <option value=\"brown\""; if(!strcmp($fontcolor,"brown")) echo(" selected"); print ">brown</option> <option value=\"gray\""; if(!strcmp($fontcolor,"gray")) echo(" selected"); print ">gray</option> <option value=\"#007FFF\""; if(!strcmp($fontcolor,"#007FFF")) echo(" selected"); print ">slate blue</option> <option value=\"orchid\""; if(!strcmp($fontcolor,"orchid")) echo(" selected"); print ">orchid</option> <option value=\"fuchsia\""; if(!strcmp($fontcolor,"fuchsia")) echo(" selected"); print ">magenta</option> <option value=\"#FF7F00\""; if(!strcmp($fontcolor,"#FF7F00")) echo(" selected"); print ">coral</option> <option value=\"#9F5F9F\""; if(!strcmp($fontcolor,"#9F5F9F")) echo(" selected"); print ">blue violet</option> <option value=\"violet\""; if(!strcmp($fontcolor,"violet")) echo(" selected"); print ">violet</option> <option value=\"#FF6EC7\""; if(!strcmp($fontcolor,"#FF6EC7")) echo(" selected"); print ">neon pink</option> <option value=\"#D98719\""; if(!strcmp($fontcolor,"#D98719")) echo(" selected"); print ">cooper</option> <option value=\"#9932CD\""; if(!strcmp($fontcolor,"#9932CD")) echo(" selected"); print ">dark orchid</option> <option value=\"#7093DB\""; if(!strcmp($fontcolor,"#7093DB")) echo(" selected"); print ">dark turquoise</option> <option value=\"#8E236B\""; if(!strcmp($fontcolor,"#8E236B")) echo(" selected"); print ">maroon</option> <option value=\"mediumpurple\""; if(!strcmp($fontcolor,"mediumpurple")) echo(" selected"); print ">medium orchid</option> <option value=\"#4D4DFF\""; if(!strcmp($fontcolor,"#4D4DFF")) echo(" selected"); print ">neon blue</option> <option value=\"#EAADEA\""; if(!strcmp($fontcolor,"#EAADEA")) echo(" selected"); print ">plum</option> <option value=\"#8C1717\""; if(!strcmp($fontcolor,"#8C1717")) echo(" selected"); print ">scarlet</option> <option value=\"#238E68\""; if(!strcmp($fontcolor,"#238E68")) echo(" selected"); print ">sea green</option> <option value=\"#3299CC\""; if(!strcmp($fontcolor,"#3299CC")) echo(" selected"); print ">sky blue</option> <option value=\"#38B0DE\""; if(!strcmp($fontcolor,"#38B0DE")) echo(" selected"); print ">summer sky</option> <option value=\"#CC3229\""; if(!strcmp($fontcolor,"#CC3229")) echo(" selected"); print ">violet red</option> <option value=\"#32CD99\""; if(!strcmp($fontcolor,"#32CD99")) echo(" selected"); print ">aquamarine</option> <option value=\"cadetblue\""; if(!strcmp($fontcolor,"cadetblue")) echo(" selected"); print ">cadetblue</option> <option value=\"chocolate\""; if(!strcmp($fontcolor,"chocolate")) echo(" selected"); print ">chocolate</option> <option value=\"darkgoldenrod\""; if(!strcmp($fontcolor,"darkgoldenrod")) echo(" selected"); print ">darkgoldenrod</option> <option value=\"darkkhaki\""; if(!strcmp($fontcolor,"darkkhaki")) echo(" selected"); print ">darkkhaki</option> <option value=\"darkred\""; if(!strcmp($fontcolor,"darkred")) echo(" selected"); print ">darkred</option> <option value=\"darksalmon\""; if(!strcmp($fontcolor,"darksalmon")) echo(" selected"); print ">darksalmon</option> <option value=\"darkslateblue\""; if(!strcmp($fontcolor,"darkslateblue")) echo(" selected"); print ">darkslateblue</option> <option value=\"lightslategray\""; if(!strcmp($fontcolor,"lightslategray")) echo(" selected"); print ">lightslategray</option> <option value=\"salmon\""; if(!strcmp($fontcolor,"salmon")) echo(" selected"); print ">salmon</option> <option value=\"silver\""; if(!strcmp($fontcolor,"silver")) echo(" selected"); print ">silver</option> <option value=\"tan\""; if(!strcmp($fontcolor,"tan")) echo(" selected"); print ">tan</option> <option value=\"tomato\""; if(!strcmp($fontcolor,"tomato")) echo(" selected"); print ">tomato</option> <option value=\"turquoise\""; if(!strcmp($fontcolor,"turquoise")) echo(" selected"); print ">turquoise</option> <option value=\"#FF9696\""; if(!strcmp($fontcolor,"#FF9696")) echo(" selected"); print ">난이옷</option> <option value=\"#A537DC\""; if(!strcmp($fontcolor,"#A537DC")) echo(" selected"); print ">보라색</option></select> <select name=\"fontsize\"> <option value=\"2\""; if(!strcmp($fontsize,"2")) echo(" selected"); print ">작게</option> <option value=\"3\""; if(!strcmp($fontsize,"3")) echo(" selected"); print ">보통</option> <option value=\"4\""; if(!strcmp($fontsize,"4")) echo(" selected"); print ">크게</option> <option value=\"5\""; if(!strcmp($fontsize,"5")) echo(" selected"); print ">아주크게</option> </select> </td> </tr> </form> <form name= form1 action='chat.html' method='post' target=_top> <input name=room_uid type=hidden value='$room_uid'> <input name='logoff' type=hidden value='ok'> <input type=hidden name=message value='님이 퇴장하셨습니다!'> </form> </table> ";
if ($message) { write($user_id, $room_uid, $message, $tomsg, $ear,$message_dir); echo(" <SCRIPT LANGUAGE='JavaScript'> <!-- top.msg_list.document.location.href='say.html?room_uid=$room_uid&message=$message' //--> </SCRIPT>"); } exit;
?> </body></html>
파일명 : say.html <?
//message 디랙토리의 절대경로 $message_dir = "./data";
$lines = file("${message_dir}/${room_uid}.html"); header("Content-type: text/html");
print "<HTML><HEAD><META HTTP-EQUIV='Refresh' CONTENT='5; url=$PHP_SELF?room_uid=$room_uid'></HEAD><BODY BGCOLOR='FFFFFF'>";
for($i=0; $i<=20;$i++) { $talk = $lines[$i]; $lsat = split('\|', $talk); if(($lsat[0] == "") || ($lsat[0] == "all")){ print "$lsat[2]"; } else{ if($lsat[0] == $user_id) print "☎귓속말:$lsat[2]"; elseif($lsat[1] == $user_id) print "☏귓속말:$lsat[2]"; } } exit; ?>
2. 문제 : 귀속말 거부기능 추가하기...
|
[목차] |