开始的开始——最简单的通讯录
即使是一个伪coder,我也知道在开始code之前,需要做下设计,首先要确认界面原型,然后要进行数据模型和业务模型的概要设计,再然后才是coding,所以,一步一步来。
其实我想做的东西包含挺多内容的,但是呢,事情总得一步一步来做,首先呢,实现一个最简单的通讯录,然后叠代然后再叠代。
好吧,因为只是一个伪coder所以也不找专业的设计工具了,就用viso大概的意思一下吧。
界面原型
数据模型
业务模型呢?不好意思没专门学过uml不知道怎么画呢,那也就大概来一下吧!
ok了 对于这么个简单的通讯录,设计部分应该算是完成了,设置好数据库,搭建好环境,下面就可以开始coding了。
花了一个下午,感谢php100和wordpress,边抄边模仿,终于弄出个最朴素的程序,没有CSS,少量的js实现了基本的功能。后续的验证,美工什么的再慢慢弄。
文件c-config.php
1: <?php2: header('Content-Type:text/html; charset=UTF-8');3: session_start();4: $conn=mysql_connect('localhost','root','123456');5: mysql_select_db('contacts', $conn);6: mysql_query("set names 'utf-8'");7:8: define('C_ALL_PS', 'SOCIALCONTACT');9:10: function user_shell($uid, $ushell){11: $sql = "select * from c_users where u_id = '".$uid."'";12: $query = mysql_query($sql);13: $us = is_array($row = mysql_fetch_array($query));14: $ushell= $us?$ushell==md5($row['u_email'].$row['u_pass'].C_ALL_PS):false;15: if ($ushell){16: return $row;17: }else{18: return false;19: }20: }21: ?>
文件c-login.php
1: <?php2: include 'c-config.php';3:4: if(!empty($_GET['exit'])){5: if ($_GET['exit'] == "true"){6: session_destroy();7: echo "<script>location.href=''</script>";8: }9: }10: if (!empty($_SESSION['uid']) && !empty($_SESSION['ushell']) && user_shell($_SESSION['uid'], $_SESSION['ushell'])){11: echo "<script>alert('您已登录,即将直接跳转到主页面');location.href='c-main.php'</script>";12: }13:14: if (!empty($_POST['submit'])) {15: $sql = "select * from c_users where u_email = '".$_POST['email']."'";16: $query = mysql_query($sql);17: $row = mysql_fetch_array($query);18: $us = is_array($row);19: $ps = $us?md5($_POST['password'].C_ALL_PS) == $row['u_pass'] : false;20: if ($ps) {21: $_SESSION['uid'] = $row['u_id'];22: $_SESSION['ushell'] = md5($row['u_email'].$row['u_pass'].C_ALL_PS);23: echo "<script>alert('登录成功');location.href='c-main.php'</script>";24: }else{25: echo "<script>alert('用户名或者密码错误');</script>";26: session_destroy();27: }28: }29: ?>30:31: <form action="" method="post" name="myform">32: 邮箱:<input type="text" name="email" /><br>33: 密码:<input type="password" name="password" /> <input type="submit" name="submit" value="登陆"/>34: </form>
文件c-register.php
1: <?php2: include 'c-config.php';3: if (user_shell($_SESSION['uid'], $_SESSION['ushell'])){4: echo "<script>alert('您已登录,即将直接跳转到主页面');location.href='c-main.php'</script>";5: }6: if (!empty($_POST['register'])) {7: $sql="insert into c_users (u_id, u_email, u_pass, u_name, u_telephone) "8: ."values (null, '".$_POST['email']."', '".md5($_POST['password'].C_ALL_PS)."', '".$_POST['name']."', '".$_POST['telephone']."')";9: $query = mysql_query($sql);10: $sql = "select * from c_users where u_email = '".$_POST['email']."'";11: $query = mysql_query($sql);12: $row = mysql_fetch_array($query);13: $_SESSION['uid'] = $row['u_id'];14: $_SESSION['ushell'] = md5($row['u_email'].$row['u_pass'].C_ALL_PS);15: echo "<script>alert('注册成功');location.href='c-main.php'</script>";16: }17: ?>18:19: <form action="" method="post" name="myform">20: 邮箱:<input type="text" name="email" /><br>21: 密码:<input type="password" name="password" />22: 姓名:<input type="text" name="name" />23: 电话:<input type="text" name="telephone" />24: <input type="submit" name="register" value="注册"/>25: </form>
文件c-main.php
1: <?php2: include 'c-config.php';3: if (empty($_SESSION['uid']) || empty($_SESSION['ushell']) ||!user_shell($_SESSION['uid'], $_SESSION['ushell'])){4: echo "无权限访问该页面";5: exit();6: }7:8: $sql = "select * from c_users where u_id = '".$_SESSION['uid']."'";9: $query = mysql_query($sql);10: $row = mysql_fetch_array($query);11: echo $row['u_email']."--".$row['u_name']."--".$row['u_telephone']."<br>";12: echo "<a href='c-login.php?exit=true'>退出登录</a><br>";13:14: $pagesize = 5;15: $url = $_SERVER["REQUEST_URI"];16: $url = parse_url($url);17: $url=$url['path'];18: $numq = mysql_query("select * from c_contacts where u_id ='".$_SESSION['uid']."'");19: $num = mysql_num_rows($numq);20: $totalpage = ceil($num/$pagesize);21: $pageval = 1;22: $page='0 ,';23: if (!empty($_GET['page'])) {24: $pageval = $_GET['page'];25: $page = ($pageval - 1) * $pagesize;26: $page.=" ,";27: }28:29: if($pageval <= 0){30: $pageval = 1;31: }32: echo "<center>共".$num."条<br>";33: if ($pageval <= 1){34: echo "<a>上一页</a>";35: }else {36: echo "<a href=$url?page=".($pageval - 1).">上一页</a>";37: }38: echo "$pageval/$totalpage";39: if ($pageval >= $totalpage){40: echo "<a>下一页</a>";41: }else {42: echo "<a href=$url?page=".($pageval + 1).">下一页</a><center>";43: }44:45: $sql = "select * from c_contacts where u_id ='".$_SESSION['uid']."' limit $page $pagesize";46: $query=mysql_query($sql);47: echo "<center><table border='1' width='600'>";48: echo "<tr><td>邮件</td><td>姓名</td><td>电话</td><td>备注</td></tr>";49: while($row=mysql_fetch_array($query)){50: echo "<tr><td>".$row['c_email']."</td><td>".$row['c_name']."</td><td>".$row['c_telephone']."</td><td>".$row['c_description']."</td></tr>";51: }52: echo "</table></center>";53: ?>




浙公网安备 33010602011771号