php入門教程-留言板程式

來源:互聯網
上載者:User


<?php
if($_GET["action"]!=1)
{
?>
<form method="post" action="index.php?action=1">
<table border="1">
<tr>
<td>MySQL主機名稱:</td>
<td><input type="text" name="host" value="localhost"></td>
</tr>
<tr>
<td>MySQL使用者名稱:</td>
<td><input type="text" name="user" value="root"></td>
</tr>
<tr>
<td>MySQL密碼:</td>
<td><input type="password" name="password" value=""></td>
</tr>
<tr>
<td>資料庫名稱:</td>
<td><input type="text" name="database" value="ruizhi_messageboard"></td>
</tr>
<tr>
<td>留言本每頁記錄數:</td>
<td><input type="text" name="pagenum" value="10"></td>
</tr>
<tr>
<td>管理員帳號:</td>
<td><input type="text" name="admin_name" value="admin"></td>
</tr>
<tr>
<td>管理員密碼:</td>
<td><input type="text" name="admin_password" value="admin"></td>
</tr>
<tr>
<td>留言本標題:</td>
<td><input type="text" name="boardname" value="RUIZHINET留言本"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="確定安裝"> <input type="button" name="submit" value="關閉頁面" onclick="window.close()"></td>
<td></td>
</tr>
</table>
</form>
<?php
}else{
@set_time_limit(1000);
$lockfile = "install.lock";
$host="localhost";
$user="root";
$password="";
$database="ruizhi_messageboard";
$pagenum=10;
$admin_name="admin";
$admin_password="123456";
$boardname="RUIZHINET留言本";
if($_POST["host"]!=""){
$host=$_POST["host"];
}
if($_POST["user"]!=""){
$user=$_POST["user"];
}
if($_POST["password"]!=""){
$password=$_POST["password"];
}
if($_POST["database"]!=""){
$database=$_POST["database"];
}
if($_POST["pagenum"]!=""){
$pagenum=$_POST["pagenum"];
}
if($_POST["admin_name"]!=""){
$admin_name=$_POST["admin_name"];
}
if($_POST["admin_password"]!=""){
$admin_password=$_POST["admin_password"];
}
if($_POST["boardname"]!=""){
$boardname=$_POST["boardname"];
}
if(file_exists($lockfile)){
exit("已經安裝過了,如果要重新安裝請先刪除install/install.lock");
}
$conn=mysql_connect($host,$user,$password);
if($conn){
$sql_drop_database="DROP DATABASE IF EXISTS `".$database."`";
$sql_create_database="CREATE DATABASE `".$database."`";
$sql_create_table_messageboard="CREATE TABLE `messageboard` (
`id` int(11) NOT NULL auto_increment,
`author` varchar(15) NOT NULL,
`title` varchar(30) NOT NULL,
`smiles` varchar(30) NOT NULL,
`content` mediumtext,
`photo` varchar(30) NOT NULL,
`addtime` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1";
$sql_create_table_reply="CREATE TABLE `reply` (
`id` int(11) NOT NULL auto_increment,
`msgid` int(11) NOT NULL,
`content` mediumtext,
`addtime` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1";
if(mysql_query($sql_drop_database,$conn)){
if(mysql_query($sql_create_database,$conn)){
mysql_select_db($database,$conn);
if(mysql_query($sql_create_table_messageboard,$conn) && mysql_query($sql_create_table_reply,$conn)){
$config_file="../config.php";
$config_strings="<?phpn";
$config_strings.="$boardname="".$boardname."";n";
$config_strings.="$host="".$host."";n";
$config_strings.="$user="".$user."";n";
$config_strings.="$password="".$password."";n";
$config_strings.="$database="".$database."";n";
$config_strings.="$pagenum="".$pagenum."";n";
$config_strings.="$admin_name="".$admin_name."";n";
$config_strings.="$admin_password="".$admin_password."";n";
$config_strings.="$conn=mysql_connect($host,$user,$password);n";
$config_strings.="mysql_select_db($database,$conn);n";
$config_strings.="?>";
if($fp=fopen($config_file,"wb")){
if(fwrite($fp,$config_strings)){
if($fp2 = fopen($lockfile, 'w'))
{
fwrite($fp2,'1212');
fclose($fp2);
}
echo "安裝成功!設定檔為:config.php,您可以手工修改該檔案";
echo "n<a href='../index.php'>進入系統首頁</a>";
}else{
exit("檔案寫入失敗");
}
fclose($fp);
}
}else{
exit("不能執行CREATE TABLE語句:".$sql_create_table);
}
}else{
exit("不能執行CREATE DATABASE語句:".$sql_create_database);
}
}else{
exit("不能執行DROP DATABASE語句:".$sql_drop_database);
}
}else{
exit("串連資料庫失敗,請檢查MySQL主機名稱、使用者名稱和密碼");
}
}

 

<?php
$boardname="RUIZHINET留言本";
$host="localhost";
$user="root";
$password="";
$database="ruizhi_messageboard";
$pagenum="10";
$admin_name="admin";
$admin_password="admin";
$conn=mysql_connect($host,$user,$password);
mysql_select_db($database,$conn);
?>


<?php
if(!$_GET["page"]){
$page=1;
}else{
$page=$_GET["page"];
}
$sql="SELECT id FROM messageboard";
$result=mysql_query($sql,$conn);
$row_num=mysql_num_rows($result);
$page_count=ceil($row_num/$pagenum);
echo "當前共有".$row_num."條留言";
echo " 共分".$page_count."頁顯示";
echo " 當前為第".$page."頁";
?>
<p>
<?php
if($row_num==0){
echo "暫時沒有留言";
}else{
?>
<table border="1" width="700">
<?php
$offset=($page-1)*$pagenum;
$sql2="SELECT * FROM messageboard ORDER BY id DESC LIMIT $offset,$pagenum";
$result2=mysql_query($sql2,$conn);
while($row=mysql_fetch_array($result2)){
$offset++;
echo "<tr>";
echo "<td align=left bgcolor=#CCCCFF>";
echo $offset.".<img src='smiles/".base64_decode($row[smiles])."'>";
echo base64_decode($row[title])." - ".base64_decode($row[author])." [".date("Y年m月d日",$row[addtime])."] ";
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
echo "<a href=del.php?id=".$row["id"].">刪除 | </a>";
echo "<a href=reply.php?id=".$row["id"].">回複</a>";
}
echo "</td></tr>";
echo "<tr><td align=left>".base64_decode($row["content"])."</td></tr>";
if($row["photo"]!="NONE")
{
echo "<tr><td align=left><img src='upfile/".base64_decode($row["photo"])."'></td></tr>";
}
$sql3="SELECT * FROM reply WHERE msgid=$row[id] ORDER BY id DESC";
$result3=mysql_query($sql3,$conn);
while($row3=mysql_fetch_array($result3)){
echo "<tr><td align=left bgcolor=#99CCFF>回複:".date("Y年m月d日",$row3[addtime]);
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
echo "<a href=delreply.php?id=".$row3["id"].">刪除</a>";
}
echo "</td></tr>";
echo "<tr><td align=left>".base64_decode($row3[content])."</td></tr>";
}
}
?>
<?php
if($page_count>1){
$prev_page=$page-1;
$next_page=$page+1;
if($page<=1){
echo "第一頁 | ";
}else{
echo "<a href='index.php?page=1'>第一頁</a> | ";
}
if($prev_page<1){
echo "上一頁 | ";
}else{
echo "<a href='index.php?page=".$prev_page."'>上一頁</a> | ";
}
if($next_page>$page_count){
echo "下一頁 | ";
}else{
echo "<a href='index.php?page=".$next_page."'>下一頁</a> | ";
}
if($page>=$page_count){
echo "最後一頁";
}else{
echo "<a href='index.php?page=".$page_count."'>最後一頁</a>";
}
}
?>
</table>
<?php
}
?>
<?php
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!=""){
?>
<p><a href="logout.php">退出管理</a></p>
<?php
}else{
?>
<p><a href="login.php">登陸管理</a></p>
<?php
}
?>


<?php
if(!$_POST["author"] || !$_POST["content"])
{
echo "<meta http-equiv="refresh" content="2;url=index.php">n";
echo "你沒有填寫留言姓名或內容,2秒鐘返回首頁";
exit();
}else{
$imgflag=0;
function random($length)
{
$hash = 'IMG-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
echo (double)microtime() * 1000000;
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
if($_FILES["upfile"]["name"]!=""){
$uploaddir="upfile/";
$type=array("jpg","gif","bmp","jpeg","png");
if(!in_array(strtolower(fileext($_FILES['upfile']['name'])),$type))
{
echo "<meta http-equiv="refresh" content="2;url=index.php">n";
$text=implode(",",$type);
echo "您只能上傳以下類型檔案: ",$text,"<br>";
exit();
}
else
{
$filename=explode(".",$_FILES['upfile']['name']);
do
{
$filename[0]=random(10);
$randname=implode(".",$filename);
$uploadfile=$uploaddir.$randname;
} while(file_exists($uploadfile));
if (move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile)){
echo "上傳圖片成功";
$imgflag=1;
}
else{
echo "上傳圖片失敗!";
$imgflag=0;
}
}
}
$author=base64_encode($_POST["author"]);
$content=base64_encode(ereg_replace("rn","<br>",htmlspecialchars($_POST["content"])));
$smiles=base64_encode($_POST["smiles"]);
if($_POST["title"]){
$title=base64_encode($_POST["title"]);
}else{
$title=base64_encode("無標題");
}
$addtime=time();
if($imgflag==1){
$photo=base64_encode($randname);
}else{
$photo="NONE";
}
//儲存資料
$sql="INSERT INTO messageboard(author,title,smiles,content,photo,addtime) VALUES('$author','$title','$smiles','$content','$photo',$addtime)";
mysql_query($sql,$conn);
echo "<meta http-equiv="refresh" content="2;url=index.php">n";
echo "謝謝您的留言,2秒鐘返回首頁";
}
?>
savereply.php:儲存回複
<?php
if(isset($_SESSION["admin_name"]) && $_SESSION["admin_name"]!="")
{
if(!$_POST["content"])
{
echo "<meta http-equiv="refresh" content="2;url=index.php">n";
echo "你沒有填寫回複內容,2秒鐘返回首頁";
exit();
}else{
$msgid=$_POST["msgid"];
$content=base64_encode(ereg_replace("rn","<br>",htmlspecialchars($_POST["content"])));
$addtime=time();
//儲存資料
$sql="INSERT INTO reply(msgid,content,addtime) VALUES($msgid,'$content',$addtime)";
mysql_query($sql,$conn);
echo "<meta http-equiv="refresh" content="2;url=index.php">n";
echo "謝謝您的留言,2秒鐘返回首頁";
}
}
?>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.