此留言本是博主写的第一个php程序,现在分享给大家。
这是主程序
index.php
<?php $conn = mysqli_connect("","","1",""); //此处填写数据库用户名密码 mysqli_query($conn,"set names utf8"); if(!empty($_POST)) { if(mysqli_query($conn,"insert into msg (title,content,pubtime) values ('".$_POST['title']."','".$_POST['content']."',".time().")")) { header('Location:index.php'); }else { echo "失败"; } } $res = mysqli_query($conn,"select * from msg"); $nr=array(); while($row = mysqli_fetch_assoc($res)) { $nr[]=$row; } /** * 获取用户真实 IP */ function getIP() { static $realip; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv("HTTP_X_FORWARDED_FOR")){ $realip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv("HTTP_CLIENT_IP")) { $realip = getenv("HTTP_CLIENT_IP"); } else { $realip = getenv("REMOTE_ADDR"); } } return $realip; } /** * 获取 IP 地理位置 * 淘宝IP接口 * @Return: array */ function getCity($ip = '') { if($ip == ''){ $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json"; $ip=json_decode(file_get_contents($url),true); $data = $ip; }else{ $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ip=json_decode(file_get_contents($url)); if((string)$ip->code=='1'){ return false; } $data = (array)$ip->data; } return $data; } ////获得访客浏览器类型 function GetBrowser(){ if(!empty($_SERVER['HTTP_USER_AGENT'])){ $br = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/MSIE/i',$br)) { $br = 'MSIE'; }elseif (preg_match('/Firefox/i',$br)) { $br = 'Firefox'; }elseif (preg_match('/Chrome/i',$br)) { $br = 'Chrome'; }elseif (preg_match('/Safari/i',$br)) { $br = 'Safari'; }elseif (preg_match('/Opera/i',$br)) { $br = 'Opera'; }else { $br = 'Other'; } return $br; }else{return "获取浏览器信息失败!";} } //获取操作系统 function get_os(){ $agent = $_SERVER['HTTP_USER_AGENT']; $os = false; if (preg_match('/win/i', $agent) && strpos($agent, '95')) { $os = 'Windows 95'; } else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90')) { $os = 'Windows ME'; } else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent)) { $os = 'Windows 98'; } else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent)) { $os = 'Windows Vista'; } else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent)) { $os = 'Windows 7'; } else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent)) { $os = 'Windows 8'; }else if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent)) { $os = 'Windows 10';#添加win10判断 }else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent)) { $os = 'Windows XP'; } else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent)) { $os = 'Windows 2000'; } else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent)) { $os = 'Windows NT'; } else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent)) { $os = 'Windows 32'; } else if (preg_match('/linux/i', $agent)) { $os = 'Linux'; } else if (preg_match('/unix/i', $agent)) { $os = 'Unix'; } else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent)) { $os = 'SunOS'; } else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent)) { $os = 'IBM OS/2'; } else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent)) { $os = 'Macintosh'; } else if (preg_match('/PowerPC/i', $agent)) { $os = 'PowerPC'; } else if (preg_match('/AIX/i', $agent)) { $os = 'AIX'; } else if (preg_match('/HPUX/i', $agent)) { $os = 'HPUX'; } else if (preg_match('/NetBSD/i', $agent)) { $os = 'NetBSD'; } else if (preg_match('/BSD/i', $agent)) { $os = 'BSD'; } else if (preg_match('/OSF1/i', $agent)) { $os = 'OSF1'; } else if (preg_match('/IRIX/i', $agent)) { $os = 'IRIX'; } else if (preg_match('/FreeBSD/i', $agent)) { $os = 'FreeBSD'; } else if (preg_match('/teleport/i', $agent)) { $os = 'teleport'; } else if (preg_match('/flashget/i', $agent)) { $os = 'flashget'; } else if (preg_match('/webzip/i', $agent)) { $os = 'webzip'; } else if (preg_match('/offline/i', $agent)) { $os = 'offline'; } else { $os = '未知操作系统'; } return $os; } // echo getCity()["province"];//省份 // echo getCity()["city"];//城市 // echo "操作系统".get_os(); // echo "浏览器".GetBrowser(); include("1.html"); ?>
这是模板文件
1.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>liuyan</title> </head> <style> .ly{width: 800px; height: 100px; border: 1px solid black; margin: 0 auto; margin-top: 10px; } .title{ margin:10px; } .titl{color: red; margin-left: 10px;} h4{margin-left: 10px; margin-top: 10px; } h3{margin-bottom: 20px;} </style> <body> <?php foreach($nr as $c) { ?> <div class="ly"> <span class="titl">用户名:</span><span class="title"> <?php echo $c['title']; ?> </span> <span class="titl">时间:</span><span class="sj"> <?php date_default_timezone_set("Asia/Shanghai"); echo date("m-d H:i:s",$c['pubtime']); ?> </span> <span class="titl">系统:</span><span class="xt"> <?php echo get_os(); ?> </span> <span class="titl">地址:</span><span class="dizhi"> <?php echo getCity()["province"];//省份 echo getCity()["city"];//城市 //echo $_SERVER["REMOTE_ADDR"]; ?> </span> <span class="titl">浏览器:</span><span class="dizhi"> <?php echo GetBrowser(); ?> </span> <h4><?php echo $c['content']; ?></h4> <h5><a href="del.php?id=<?php echo $c["id"]; ?>">删除</a></h5> </div> <?php } ; ?> <hr> <form action="" method="post"> 用户名:<input type="text" name="title" required = "required"> <br><br> 内容:<textarea name="content" id="" cols="30" rows="10"> </textarea><br><br> <input type="submit" value="提交留言"> </form> </body> </html>这是删除程序
del.php
<meta charset="UTF-8"> <?php $conn = mysqli_connect("","","",""); //数据库 mysqli_query($conn,"set names utf8"); $id = $_GET["id"]; $sql = "delete from msg where id=$id"; if(mysqli_query($conn,$sql)) { //echo "删除成功"; header('Location:index.php'); }else { echo "删除失败"; } ?>
这是效果图
不明白的可以问我哦!