首页 > php开发 > php+dbfile开发小型留言本

php+dbfile开发小型留言本

board.php用来存储数据,可以先在里面添加了一条留言纪录。
代码

<?php
$Board=array(
array(1081410332,'测试','测试留言本','http://www.phprm.com')
);
?>


index.php是留言显示和提交页面。
代码

<?php
header('Content-type:text/html;charset=utf8');
require_once ('board.php');
function htmlencode($content) {
    $content = htmlspecialchars($content);
    $content = preg_replace("/\r/i", "<br />", $content);
    return $content;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $configpath_parts1 = pathinfo($_SERVER['SCRIPT_FILENAME']);
    $time = time();
    $name = $_POST['name'];
if(preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i", $_POST['url']) || $_POST['url'] == '')
{
$url = $_POST['url'];
}else{
$url = 'http://' . htmlspecialchars(preg_replace("/https?\:\/\//i", '', $_POST['url']) , ENT_QUOTES);
}
    $info = htmlencode($_POST['info']);
    if ($name != '' && $info != '') {
        $Board[] = array(
            $time,
            $name,
            $info,
            $url
        );
    }
    for ($i = 0; $i < count($Board); $i++) {
        $bd = current($Board);
        $s[] = "\tarray(" . $bd[0] . ",'" . $bd[1] . "','" . $bd[2] . "','" . $bd[3] . "')";
        next($Board);
    }
    $content = "<?php\n\$Board=array(\n" . join($s, ",\n") . "\n);\n?>";
    $filename = $configpath_parts1['dirname'] . '/' . 'board.php';
    if (is_writable($filename) || !file_exists($filename)) {
        if (!$handle = fopen($filename, 'w')) {
            die();
        }
        if (!fwrite($handle, $content)) {
            die();
        }
        fclose($handle);
    } else {
        die();
    }
    header('Location:.');
} else {
?>
<!DOCTYPE html>
<html>
<head>
<title>留言本</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head> <body> <form method="post" name="form1" action="">
<table border="0" cellspacing="5" cellpadding="0" align="center">
<tr>
<td>
<div style="overflow:auto;height:250px;border:1px dotted #999999;padding:5px;word-wrap:break-word;width:400px;">
<?php
    end($Board);
    for ($i = 0; $i < count($Board); $i++) {
        $bd = current($Board);
        $s[] = '<strong>' . ($bd[3] != '' ? '<a href="' : '') .
(preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i", $bd[3]) ? 'mailto:' : '') . $bd[3] .
(($bd[3] != '' && !preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i", $bd[3])) ? '" target="_blank' : '') .
($bd[3] != '' ? '">' : '') . $bd[1] . ($bd[3] != '' ? '</a>' : '') . ':</strong> ' . $bd[2] .
'<br/><em>-' . date("G:i, M j, Y", $bd[0]) . '</em>';
        prev($Board);
    }
    echo join($s, '<br/><br/>');
?>
</div>
</td>
</tr>
<tr>
<td align="center">名称:<input type="text" name="name"/> URL/Email:
<input type="text" name="url"/><br/>
<textarea name="info" cols="40" rows="8">
</textarea><br/> 
<input type="submit" value="发布"/> </td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>

[Ctrl+A 全部选择 然后拷贝]


config.php中存放的是管理留言本的密码,把密码放在单独一个文件中方便修改。 代码拷贝框


[Ctrl+A 全部选择 然后拷贝]


admin.php是管理页面,功能很简单,只能删除留言。在删除时需要输入管理密码,管理密码存放在config.php中。 代码拷贝框

<?php
require_once ('board.php');
require_once ('config.php');
if (isset($_POST['id']) && $_POST['id'] != '' && addslashes($_POST['password']) == $password) {
    if (count($Board) > 1) {
        unset($Board[intval($_POST['id']) ]);
        for ($i = 0; $i < count($Board); $i++) {
            $bd = current($Board);
            $s[] = "\tarray(" . $bd[0] . ",'" . $bd[1] . "','" . $bd[2] . "','" . $bd[3] . "')";
            next($Board);
        }
        $content = "<?php\n\$Board=array(\n" . join($s, ",\n") . "\n);\n?>";
        $configpath_parts1 = pathinfo($_SERVER['SCRIPT_FILENAME']);
        $filename = $configpath_parts1['dirname'] . '/' . 'board.php';
        if (is_writable($filename)|| !file_exists($filename)) {
            if (!$handle = fopen($filename, 'w')) {
                die();
            }
            if (!fwrite($handle, $content)) {
                die();
            }
            fclose($handle);
        } else {
            die();
        }
    }
    header('Location:admin.php');
} else {
?>
<!DOCTYPE html>
<html>
<head>
<title>管理留言本</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<table width="500" border="0" cellspacing="1" cellpadding="5" align="center" bgcolor="#999999">
<?php
    for ($i = 0; $i < count($Board); $i++) {
        $bd = current($Board);
        $s[] = '<tr><td bgcolor="#' . ($i % 2 != 0 ? 'ececec' : 'ffffff') . '"><strong>' . ($bd[3] != '' ? '<a href="' : '') .
(preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i", $bd[3]) ? 'mailto:' : '') . $bd[3] .
(($bd[3] != '' && !preg_match("/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/i", $bd[3])) ? '" target="_blank' : '') .
($bd[3] != '' ? '">' : '') . $bd[1] . ($bd[3] != '' ? '</a>' : '') . ':</strong> ' . $bd[2] . '<br/><em>-' .
date("G:i, M j, Y", $bd[0]) . '</em></td>' . (count($Board) > 1 ? '<td bgcolor="#' .
($i % 2 != 0 ? 'ececec' : 'ffffff') . '" align="center"><form method="post" action=""><input type="submit" value="删除" />
<input type="hidden" name="id" value="' . $i . '" /><input type="password" name="password" /></form></td>' : '') . '</tr>';
        next($Board);
    }
    echo join($s, ''); 
?>
</table></body>
</html>
<?php
}
?>


教程地址:http://www.phprm.com/develop/15350f9afecd62f37a6efa103e45c32c.html

欢迎转载!但请带上文章地址^^

标签:none

发表留言