Skip to content

Instantly share code, notes, and snippets.

@dailybird
Created April 8, 2017 15:15
Show Gist options
  • Save dailybird/e5cab963eb2f74fb608fb0351cfc027f to your computer and use it in GitHub Desktop.
Save dailybird/e5cab963eb2f74fb608fb0351cfc027f to your computer and use it in GitHub Desktop.
Simple message borad.
<?php
date_default_timezone_set('PRC');
$filename = 'messages.txt';
$username = $_POST['username'] ?? null;
$title = $_POST['title'] ?? null;
$content = $_POST['content'] ?? null;
if(!is_null($username) && !is_null($title) && !is_null($content)){
$username = strip_tags($username);
$title = strip_tags($title);
$content = strip_tags($content);
$time = date('Y/m/d H:i:s', time());
$origin = file_get_contents($filename);
if(strlen($origin) > 0){
$messages = unserialize($origin);
}else{
$messages = [];
}
$content = compact('username', 'title', 'content', 'time');
array_push($messages, $content);
if(file_put_contents($filename, serialize($messages))){
echo "<script>alert('添加成功');</script>";
echo "<script>window.location.href = './index.php';</script>";
}else{
echo "<script>alert('添加失败,意外错误');</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>留言板 - 创建</title>
<meta name="description" content="Source code generated using layoutit.com">
<meta name="author" content="LayoutIt!">
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="jumbotron well">
<h2>
留言板 - 创建
</h2>
<p>
Say something
</p>
</div>
<form role="form" action="add.php" method="post">
<div class="form-group">
<label for="username">
用户名
</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class="form-group">
<label for="title">
标题
</label>
<input type="text" class="form-control" name="title" required>
</div>
<div class="form-group">
<label for="content">
内容
</label>
<textarea type="content" class="form-control" name="content" rows="10" required></textarea>
</div>
<button type="submit" class="btn btn-primary">
提交
</button>
<a type="submit" class="btn btn-default" href="./index.php">
查看留言
</a>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
/**
* Created by PhpStorm.
* User: dailybird
* Date: 17/4/8
* Time: 下午7:44
*/
date_default_timezone_set('PRC');
$filename = 'messages.txt';
$id = $_GET['id'];
$messages = file_get_contents($filename);
$messagesArr = unserialize($messages);
unset($messagesArr[$id]);
if(file_put_contents($filename, serialize($messagesArr))){
echo "<script>alert('删除成功');</script>";
echo "<script>window.location.href = './index.php';</script>";
}else{
echo "<script>alert('删除失败,未知错误');</script>";
echo "<script>window.location.href = './index.php';</script>";
}
<?php
date_default_timezone_set('PRC');
$filename = 'messages.txt';
$username = $_POST['username'] ?? null;
$title = $_POST['title'] ?? null;
$content = $_POST['content'] ?? null;
$id = $_POST['id'] ?? null;
if(!is_null($username) && !is_null($title) && !is_null($content) && !is_null($id)){
$username = strip_tags($username);
$title = strip_tags($title);
$content = strip_tags($content);
$time = date('Y/m/d H:i:s', time());
$origin = file_get_contents($filename);
if(strlen($origin) > 0){
$messages = unserialize($origin);
if(!is_null($messages[$id])){
$messages[$id] = [
'username' => $username,
'title' => $title,
'content' => $content,
'time' => $time,
];
if(file_put_contents($filename, serialize($messages))){
echo "<script>alert('编辑成功');</script>";
echo "<script>window.location.href = './index.php';</script>";
}
}
}
echo "<script>alert('所编辑内容不存在');</script>";
echo "<script>window.location.href = './index.php';</script>";
}else{
// 未提交数据
$id = $_GET['id'];
if(is_null($id)){
echo "<script>alert('所编辑内容不存在');</script>";
echo "<script>window.location.href = './index.php';</script>";
}
if(file_exists($filename)){
$messages = file_get_contents($filename);
$messagesArr = unserialize($messages);
$message = $messagesArr[$id];
}
if(is_null($message)){
$message = [
'username' => '',
'title' => '',
'content' => ''
];
}
}
// if(file_put_contents($filename, serialize($messagesArr))){
// echo "<script>alert('删除成功');</script>";
// echo "<script>window.location.href = './index.php';</script>";
// }else{
// echo "<script>alert('删除失败,未知错误');</script>";
// echo "<script>window.location.href = './index.php';</script>";
// }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>留言板 - 编辑</title>
<meta name="description" content="Source code generated using layoutit.com">
<meta name="author" content="LayoutIt!">
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="jumbotron well">
<h2>
留言板 - 编辑
</h2>
<p>
Say something
</p>
</div>
<form role="form" action="edit.php" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>">
<div class="form-group">
<label for="username">
用户名
</label>
<input type="text" class="form-control" name="username" value="<?php echo $message['username'];?>">
</div>
<div class="form-group">
<label for="title">
标题
</label>
<input type="text" class="form-control" name="title" value="<?php echo $message['title'];?>">
</div>
<div class="form-group">
<label for="content">
内容
</label>
<textarea type="content" class="form-control" name="content" rows="10"><?php echo $message['username'];?></textarea>
</div>
<button type="submit" class="btn btn-primary">
编辑完成
</button>
<a type="submit" class="btn btn-default" href="./index.php">
查看留言
</a>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
header('Content-type:text/html; charset=urf-8');
$filename = 'messages.txt';
if(file_exists($filename)){
$messages = file_get_contents($filename);
if(strlen($messages) > 0){
$messagesArr = unserialize($messages);
}else{
$messagesArr = [];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>留言板</title>
<meta name="description" content="Source code generated using layoutit.com">
<meta name="author" content="LayoutIt!">
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="jumbotron well">
<h2>
留言板
</h2>
<p>
Say something
</p>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th width="15%">
编号
</th>
<th width="15%">
用户
</th>
<th width="15%">
标题
</th>
<th width="20%">
时间
</th>
<th width="20%">
内容
</th>
<th width="15%">
操作
</th>
</tr>
</thead>
<tbody>
<?php $i = 0; foreach ($messagesArr as $index => $message):?>
<tr>
<td>
<?php echo intval(++$i);?>
</td>
<td>
<?php echo $message['username'];?>
</td>
<td>
<?php echo $message['title'];?>
</td>
<td>
<?php echo $message['time'];?>
</td>
<td>
<?php echo $message['content'];?>
</td>
<td>
<a href="./edit.php?id=<?php echo $index; ?>">编辑</a>
<a onclick="if(!confirm('确认删除?')) return false;" href="./delete.php?id=<?php echo $index; ?>">删除</a>
</td>
</tr>
<?php endforeach?>
</tbody>
</table>
<a type="button" class="btn btn-primary btn-block" href="./add.php">
我要留言
</a>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment