1,准备:PHP中页面调用函数require(),require_once(),include(),include_once()将指定文件引入。<re和in的区别是re会出现致命错误,in则有问题提示>
源码展示:
Connect.php文件
<?php
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
?>
Add.php文件
<?php
include("head.php");
include("connect.php");
if($_POST['submit']){
$sql="insert into `message` (id,user,title,content,lastdate)".
"values('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo "发表成功!!";
}?>
<form action="add.php" method="post">
用户名<input type="text" name="user" /><br />
标题<input type="text" name="title" /><br />
内容<textarea name="content" ></textarea><br />
<input type="submit" name="submit" value="提交" />
</form>
Show,php文件
<?php
include("head.php");
include("connect.php");
?>
<body>
<table width=500px border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<?php
$sql="select * from `message` order by `id` desc";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
?>
<tr bgcolor="#eff3ff">
<td>标题:<?php echo $row['title'];?> 用户:<?php echo $row['user'];?></td>
</tr>
<tr bgColor="#ffffff">
<td>内容:<?php echo $row['content'];?></td>
</tr>
<?php
}
2,javascript进行信息验证。
<script>
function check(){
if(myform.user.value==""){
alert("用户名不可以为空");
myform.user.focus();
return false;}
if(myform.title.value.length<6){
alert("标题长度不可小于6字符");
myform.title.focus();
return false;}
if(myform.content.value==""){
alert("内容不可以为空");
myform.content.focus();
return false;}
}
</script>
3,str_replace()替换函数
Str_replace(被替换的值,替换的值,被替换的内容)
function htmltocode($content){
$content=str_replace("\n","<br>",str_replace(" "," ",$content));
return $content;}
4,htmlspecialchars()用于防止在输出html时被浏览器执行,可视化html
Htmlspecialchars(所要格式化的内容)
5,md(加密对象)加密函数
6,cookie的登录与使用
Setcookie(“cookie名”,”cookie值”,保存时间,”cookie保存路径”,”起效域名”,true)
Login.php:
<?php
include("connect.php");
if(_get('out')){
setcookie("cookie","out");
echo "<script>location.href='login.php'</script>";}
if(_post('id')=='admin'){
//$pwd=md5($_POST[pwd]);
if(_post('pwd')=='cyan'){
setcookie("cookie","ok");
echo "<script>location.href='login.php'</script>";}}
include("head.php");
if($_COOKIE['cookie']!='ok'){
?>
<form action="" method="post" name="loginform" οnsubmit="return logincheck();">
ID:<input type="text" name="id" /><br />
Password<input type="password" name="pwd" /><br />
<input type="submit" name="login" value="Login" />
</form>
<?php
}else{
?>
<a href="?out=login">退出</a>
<?php
}
?>