Maison >développement back-end >tutoriel php >小白求指点:这样的代码结构如何样优化呢
小白求指点:这样的代码结构怎么样优化呢?
我是这样设计的:一个是数据库连接文件 db.conn.php 一个自定义函数文件 config.inc.php
db.conn.php:
<?php<br />$db_host = 'localhost';<br />$db_user = '*****';<br />$db_password = '*******';<br />$db_database = '******';<br />$conn = new mysqli($db_host, $db_user, $db_password, $db_database);<br />mysqli_query($conn, "set names 'utf8'"); <br />if(mysqli_connect_errno()){die('数据库连接失败: ' . mysqli_connect_error());}<br />$site_data = $conn->query("SELECT * FROM `dh_member_control` WHERE `id`=1");<br />$site = mysqli_fetch_array($site_data);<br />?>
function marrbase(){<br />if(isset($_COOKIE["uname"]) && isset($_COOKIE["uid"])){<br />global $mbase;<br />global $conn;<br />$mql = "select * from dh_member where `id`=" . $_COOKIE["uid"] . " and `mck`=1";<br />$mbase_data = mysqli_query($conn, $mql);<br />$mbase = mysqli_fetch_array($mbase_data);}<br />else{exit();}<br />}<br /><br />function login_log($muid){<br />if (empty($muid)){exit();}<br />else {<br />global $conn;<br />$lql = "INSERT INTO `dh_mllog` (`uid`,`ltime`,`lip`,`lptxt`) VALUES (". $muid .",'". date("Y-m-d H:i:s") ."','". getIPaddress() ."','". getIPS138(getIPaddress()) ."')" ;<br />if (!mysqli_query($conn, $lql)){die(mysqli_error());exit();}}<br />}<br /><br />function memberislogin($back = NULL){<br />if(!isset($_COOKIE["uname"]) || !isset($_COOKIE["uid"])){echo "<script>alert('没有登录');window.location.href=\"/member/login.php?backurl=". $back ."\";</script>"; exit();}<br />else {<br />global $conn;<br />$login_check = mysqli_query ("SELECT * FROM `dh_member` WHERE `id`=". $_COOKIE["uid"] ." AND `mck`=1");<br />if(!mysqli_num_rows($login_check)){echo "<script>window.location.href=\"/member/login.php?backurl=". $back ."\"</script>"; exit();}}<br />}<br />.....等等自定义函数<br />