小型留言板

来源:互联网 发布:广电网络属于什么单位 编辑:程序博客网 时间:2024/04/27 14:56

<?php

//链接数据库

session_start();
$sq= @mysql_connect("127.0.0.1","root","")or die("数据库还没有好");
$sa= @mysql_select_db("thinkphp",$sq);
      mysql_query("set name'utf8'");
      define("ALL", "SHA");
   //判断权限方法
function user_shell($id,$shell){
if(empty($id)||empty($shell)){
echo "meiyouquanxim";
   exit();
}else {
   $sq="select*from `old` where `id`=$id";
  $sa=mysql_query($sq);
  $us=is_array($sc=mysql_fetch_array($sa));
  $shell=$us ? $shell=md5($sc['username'].$sc['password'].ALL):false;
  if($shell){
  return $sc;
  }else{
  echo "没有权限";
  exit();
  }
  }
}

?>


//需要数据库中建两张表old(字段id,username,passname,),news(id,title,dates,content,hit)

<?php 
include 'conn.php';
$action= isset($_GET['action']) ? htmlspecialchars($_GET['action']) : '';
if($action=='register') {
if(!empty($_POST['sub'])){
if(empty($_POST['user'])&&empty($_POST['pass'])){//判断用户名或密码不能为空
echo "用户名或密码不能为空";
} else {
$user=$_POST['user'];
$pass=md5($_POST['pass'].ALL);//MD5增加密码安全
$sq=  "insert into `old` (`id`,`username`,`password`) values( null,'$user','$pass' )";
$query=mysql_query($sq);
echo "<script>alert('注册成功 ');location.href='http://127.0.0.1/add/test.php?action=land'</script>";
}
}


 ?>
 <form action="" method="post">
 用户名:<input type="text" name=user><br> 
 密码:<input type="password" name="pass"><br>
 <input type="submit" name="sub" value="提交注册">
 </form>
 <?php 
}
 elseif($action=='land') { //登陆页面
  if(!empty($_POST['sub'])){
 
  if(empty($_POST['user'])){
  echo "用户名不能为空";//判断用户名是否为空;
  }else{
  $user=$_POST['user'];
  $sa=str_replace(" ", "", $user);
  $sq= "select*from`old`where `username`='$sa'";//判断用户名是否存在;
  $query=mysql_query($sq);
  $sr=mysql_fetch_array($query);
  if(md5($_POST['pass'].ALL)== $sr['password']){
  $_SESSION['id']=$sr['id'];
  $_SESSION['shell']=md5($sr['password'].$sr['username'].ALL);
  echo "<script>alert('登陆成功 ');location.href='http://127.0.0.1/add/test.php?action=add'</script>";
  }else {
  echo "用户名或密码错误";
  session_destroy();
  }
 
     }
  }


 ?>
  <form action="" method="post">
 用户名:<input type="text" name=user><br> 
 密码:<input type="password" name="pass"><br>
 <input type="submit" name="sub" value="登陆">
 </form>
 <?php 
  }
 elseif($action=='add'){//添加页面
 if(empty($_SESSION['id'])||empty($_SESSION['shell'])){
echo "非法操作";
exit();
}else{
  $sw=user_shell($_SESSION['id'],$_SESSION['shell']);
}//判断用户是否为登陆状态
    //echo $_SESSION['id'];echo"<br>";
  //echo $_SESSION['shell'];
  if(!empty($_POST['sub'])){
  if(!empty($_POST['title'])&&!empty($_POST['con'])){
   $sq=$_POST['title'];
   $sa=$_POST['con'];
   $sr="insert into`news`(`id`,`title`,`dates`,`content`)values(null,'$sq',now(),'$sa')";
        mysql_query($sr);
   echo "<script>alert('发表成功 ');location.href='http://127.0.0.1/add/test.php?action=index'</script>";
  }else {
  echo "标题或内容不能为空";
  }
  }
 
 
?>
<form action="" method="post">
标题:<input type="text" name="title"><br>
内容:<textarea  rows="5" cols="50" name="con"></textarea> 
<input type="submit" name="sub" value="提交"> 
</form>
<?php 
 }
elseif ($action=='index'){//主页
if(empty($_SESSION['id'])||empty($_SESSION['shell'])){
echo "非法操作";
exit();
}else{
  $sw=user_shell($_SESSION['id'],$_SESSION['shell']);
}
  echo "<a href=\"http://127.0.0.1/add/test.php?action=add\">添加内容</a><br>
 <form action=\"\" method=\"get\">
 <input type=\"text\" name=\"keys\">
 <input type=\"submit\" name=\"sub\" value=\"搜索\">
 </form>";
  $sql="select*from`news`";
  $sa=mysql_query($sql);




    while($sr=mysql_fetch_array($sa)){
?>
<h2>标题:<a href='http://127.0.0.1/add/test.php?action=viwe&id=<?php echo $sr['id']?>'><?php echo iconv_substr($sr['title'],0,2,"utf-8")?>.....</a>|<a href='http://127.0.0.1/add/test.php?action=edit&id=<?php echo $sr['id']?>'>编辑</a>|<a href='http://127.0.0.1/add/test.php?action=del&id=<?php echo $sr['id']?>'>删除</a>|</h2>
<li>时间:<?php echo $sr['dates']?></li>
<p>内容:<?php echo $sr['content']?></p>
<?php 
 }}
elseif($action=='edit'){//编辑页面
if(empty($_SESSION['id'])||empty($_SESSION['shell'])){
echo "非法操作";
exit();
}else{
$sw=user_shell($_SESSION['id'],$_SESSION['shell']);
}

  if(!empty($_GET['id'])){
     $sql="select*from`news`where `id`='".$_GET['id']."'";
     $se=mysql_query($sql);
$sw=mysql_fetch_array($se);
}


 if(!empty($_POST['sub'])){
        $a=$_POST['title'];
        $b=$_POST['con'];
        $hid=$_POST['hid'];
        $up="update`news` set `title`='$a',`content`='$b' where id='$hid'";
        mysql_query($up);
        echo "<script>alert('更新成功 ');location.href='http://127.0.0.1/add/test.php?action=index'</script>";
       
 }
  

 
?>
<form action="" method="post">
<input type="hidden" name="hid" value="<?php echo $sw['id']?>">
标题:<input type="text" name="title" value="<?php echo $sw['title']?>"><br>
内容:<textarea  rows="5" cols="50" name="con"><?php echo $sw['content']?></textarea><br>
<input type="submit" name="sub" value="提交"> 
</form>
<?php 
 }
 elseif($action=='del'){
  if(empty($_SESSION['id'])||empty($_SESSION['shell'])){
  echo "非法操作";
  exit();
  }else{
  $sw=user_shell($_SESSION['id'],$_SESSION['shell']);
  }
  if(!empty($_GET['id'])){
  $sql="delete from`news`where `id`='".$_GET['id']."'";
  $sa=mysql_query($sql);
  echo "<script>alert('删除成功 ');location.href='http://127.0.0.1/add/test.php?action=index'</script>";
  }else{
  echo "删除失败"; 
  }


?>
<?php 
 }
 elseif($action=='viwe'){
  if(empty($_SESSION['id'])||empty($_SESSION['shell'])){
  echo "非法操作";
  exit();
  }else{
  $sw=user_shell($_SESSION['id'],$_SESSION['shell']);
  }
  if(!empty($_GET['id'])){
  $sq="select*from `news` where `id`='".$_GET['id']."'";
  $sw=mysql_query($sq);
  $se=mysql_fetch_array($sw);
  $up="update `news` set `hits`=hits+1  where `id`='".$_GET['id']."'";  
  mysql_query($up);
 
 }
?>
<h1>标题:<?php echo $se['title']?></h1>
<li>时间:<?php echo $se['dates']?></li>
<h3>点击量:<?php echo $se['hits']?></h3>
<p>
内容:<?php echo $se['content']?>
</p>
<?php 
}


?>

1 0
原创粉丝点击