|
楼主 |
发表于 2011-5-7 19:04:20
|
显示全部楼层
回复 jeongee 的帖子
什么敢不敢啊,发就发:
controller:
<?php if(!defined('BASEPATH')) exit('no direct script access allowed');
class LeaveMessage extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper(array('form','url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', '用户名','required');
$this->form_validation->set_rules('title', '主题', 'required');
$this->form_validation->set_rules('mess','required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('leavemessage/leavemessage');
}else
{
$filename = "text_data.txt";
$message = $_POST["username"]."||".$_POST["title"]."||".$_POST["mess"]."<|>";
$handle = fopen($filename, "a");
flock($handle, LOCK_EX);
fwrite($handle, $message);
flock($handle, LOCK_UN);
fclose($handle);
$buffer = "";
if(file_exists($filename))
{
$handle = fopen($filename, "r");
flock($handle, LOCK_SH);
while(!feof($handle))
{
$buffer.= fread($handle, 1024);
}
$data = explode('<|>', $buffer);
foreach($data as $line)
{
@list($username, $title, $mess) = explode('||', $line);
if($username !=="" && $title !=="" && $mess !=="")
{
echo $username."说:".$mess.'<hr>';
}
}
$this->load->view('leavemessage/leavemessage');
flock($handle, LOCK_UN);
fclose($handle);
}
}
}
}
?>
view:
<html>
<head>
<title>网络留言板</title>
</head>
<body>
<?php echo form_open('leavemessage/leavemessage');?>
用户名:<input type="text" name="username"><br>
主题:<input type="text" name="title"><br>
<textarea name="mess" rows="10" cols="50">请在这里留言!</textarea>
<input type="submit" name="sub" value="提交">
</form>
</body>
</html> |
|