PHP 개발자로서 저는 가끔 소리 상자를 만들어 달라는 요청을 받습니다. 같은 일이 발생하면 여기에 빠른 가이드가 있습니다. 분명히 위에 있는 CSS를 추가하고 싶지만 기본 아이디어는 다음과 같습니다.
MySQL 데이터베이스 테이블과 세 개의 PHP 파일이 필요합니다.
먼저 데이터베이스 정보를 저장할 파일이 필요합니다
. ---파일 #1:mysql.inc.php---
<?php
#Simply Shouting-shoutboxexample
# 파일 이름:mysql.inc.php
# 설명: 데이터베이스 정보를 담는 파일입니다.
$host ='로컬호스트';
$user ='데이터베이스_사용자_이름';
$password='database_user_password';
$name ='데이터베이스_이름';
?>
4개의 필드가 있는 데이터 테이블을 생성합니다. 이전에는 이 SQL 파일이 없었을 수 있으므로 이 파일을 한 번 사용한 후에는 삭제하는 것을 잊지 마세요.
-- 파일 #2: install.php --
<?php
#Simply Shouting-shoutboxexample
# 파일명 : install.php
# 설명: 데이터베이스 테이블을 생성합니다.
// 데이터베이스 정보 파일을 포함합니다.
include("mysql.inc.php");
//데이터베이스에 연결
$connection= @mysql_connect($host,$user,$password) 또는 die(mysql_error());
$db= @mysql_select_db($name,$connection) or die(mysql_error());
//이미 "shouts"라는 테이블이 있으면 먼저 삭제해야 합니다.
$sql='존재하는 경우 테이블 삭제 `소리`';
$result= @mysql_query($sql,$connection) or die(mysql_error());
// 이제 같은 이름의 테이블이 없는지 확인하고 생성합니다.
$sql='CREATE TABLE `소리`(
`id` int(11) NOT NULL auto_increment,
`타임스탬프` TIMESTAMP NOT NULL 기본 CURRENT_TIMESTAMP,
`shoutby` varchar(50) 기본값은 NULL,
`shout` varchar(50) 기본값은 NULL,
기본 키 `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1';
echo'테이블 생성 중: '소리지르기'....';
// 연결 끊기
$result= @mysql_query($sql,$connection) 또는 die(mysql_error());?>
<html>
<머리>
<title>간단한 소리 - 설치</title>
</head>
<본문>
<br />
설치 프로세스가 완료되었습니다. 이 프로그램에는 다음 설치 파일이 포함되어 있습니다.<br />
<br />
1) 설치.php<br />
<br />
<br />
<!-- 자동으로 index.php로 보내면 제대로 생성되었는지 궁금해할 것입니다. -->
시작하려면 <a href="index.php">여기</a>를 클릭하세요.</html>
이것이 메인 파일입니다:
--- 파일 #3: index.php---
<?
#Simply Shouting-shoutboxexample
# 파일 이름: index.php
# 설명: 우리의 외침을 표시하는 메인 페이지
//데이터베이스 정보를 포함합니다
.
include_once("mysql.inc.php");
//데이터베이스에 연결
$connection= @mysql_connect($host,$user,$password) 또는 die(mysql_error());
$db= @mysql_select_db($name,$connection) 또는 die(mysql_error());
?>
<html>
<머리>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css">
<!--
몸, td, 번째 {
글꼴 모음: Verdana, Arial, Helvetica, sans-serif;
글꼴 크기: 12px;
}
-->
</style><본문>
<div style="너비:500px;높이 400px; 테두리:얇은 홈 #519554;">
<?
// 최근 10개의 메시지를 표시합니다. 먼저 카운터를 초기화합니다.
$counting=0;
// ASC 순서로 외침을 표시하고 싶기 때문에 카운터가 필요합니다.
// (채팅방과 유사)
$sql=mysql_query("SELECT * FROM `shouts`");
while($data=mysql_fetch_array($sql)){
//모든 행을 계산합니다.
$counting=$counting+1;
} //end while
// 개수가 10보다 큰 경우 마지막 항목을 선택합니다.
// 표시할 10개의 소리입니다.
if($counting>10){
$countlessten=$counting-9;
$sql=mysql_query("SELECT * FROM `shouts` ORDER BY `shouts`.`id` ASC LIMIT $countlessten,10");
}또 다른{
//그렇지 않으면 상관없습니다. 10개 미만이면 됩니다!
$sql=mysql_query("SELECT * FROM `shouts` ORDER BY `shouts`.`id` ASC LIMIT 10");
}
while($data=mysql_fetch_array($sql)){
//데이터베이스의 타임스탬프 필드는 기본적으로 나에게 쓸모가 없습니다.
//다음 코드는 타임스탬프를 구문 분석합니다.
//사용할 수 있습니다.
$timestamp=$data['timestamp'];
$postedyear=substr($timestamp,0,4);
$postedmonth=substr($timestamp,5,2);
$postedday=substr($timestamp,8,2);
$postedtime=substr($timestamp,11,5);
$newpostedtime="";
$nomilitary=substr($postedtime,0,2);
// 시간이 12보다 크므로 1-12로 다시 전환해야 합니다.
// "pm" 추가
if($nomilary>=13){
$nomilitary=$nomilitary-12;
$newpostedtime=$nomilitary;
$newpostedtime.=":";
$newpostedtime.=substr($postedtime,3,2);
$newpostedtime.="오후";
}
if($newpostedtime!=""){
$postedtime=$newpostedtime;
}또 다른{
$postedtime.=" 오전";
}
//이제 시간이 있으니 소리를 지르고 소리를 지르자
$shoutby=$data['shoutby'];
$shout=$data['shout'];
echo$postedmonth."/".$postedday."/".$postedyear." at ".$postedtime." - <strong>".$shoutby." 님이 말했습니다: </strong>".$shout."<br ><br>";
// 다음과 같습니다: 2008년 12월 1일 오후 5시 2분 - Josh가 말했습니다: Yo Yo Yo!
}
//아래는 외침을 생성하기 위한 HTML 형식입니다.
?>
<form id="newshout" name="newshout" action="newshout.php" method="post"><input name="shoutby" type="text" id="shoutby" onClick="javascript:this.value =''" value="여기에 이름을 입력하세요" size="24" maxlength="50" />
<br><br><input name="shout" type="text" id="shout" onClick="javascript:this.value=''" value="클릭하고 외쳐보세요!" size="24" maxlength= "50" />
<br><br><input id="submit" name="submit" type="submit" value="외쳐보세요!"
</div>
</body>
</html>
마지막으로 양식을 처리하려면 PHP 파일이 필요합니다.
-- 파일 #4: newsout.php --
<?
#Simply Shouting-shoutboxexample
# 파일 이름: newsout.php
# 설명: index.php에서 HTML 양식을 처리하고 리디렉션
//메시지를 남긴 사람의 이름을 가져옵니다.
$shoutby=$_POST['shoutby'];
if($shoutby=="여기에 이름을 입력하세요"||$shoutby==""){
//이름을 입력하지 않은 경우
$shoutby="방문자";
}
if($_POST['소리치다']){
// 메시지
if($_POST['shout'] !="클릭하고 외쳐보세요!"){
//기본값을 외치지 않았으므로 계속 처리합니다.
$shout=$_POST['외침'];
//해커를 차단하려면 "<" 및 ">"를 바꾸세요.
$shout=str_replace("<"," ",$shout);
$shout=str_replace(">"," ",$shout);
// 데이터 정보가 들어있습니다.
include_once("dbaccess.php");
//데이터베이스에 연결
$connection= @mysql_connect($host,$user,$password) 또는 die(mysql_error());
$db= @mysql_select_db($name,$connection) 또는 die(mysql_error());
//데이터베이스에 메시지 정보를 삽입합니다.
$sql="`shouts`(`shoutby`,`shout`) VALUES('$shoutby','$shout')"에 삽입";
//연결을 닫습니다.
$result= @mysql_query($sql,$connection);
}
}
?>
<html>
<머리>
</head>
<body onLoad="window.open('index.php','_self')">
</body>
</html>