若愚网--------php留言本之注册功能
若愚网--------php留言本之注册功能数据表创建部分代码:
建用户表gb_users
CREATE TABLE `gb_users` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 16 ) NOT NULL ,
`passwd` VARCHAR( 32 ) NOT NULL ,
`email` VARCHAR( 100 ) NOT NULL ,
`insert_time` DATETIME NOT NULL
) ENGINE = MYISAM ;
程序代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>注册</title> <link rel="stylesheet" type="text/css" href="style.css" media="all" /> </head> <body> <?php require('common.php'); if ($_GET['action']=='save') { // 清除空格 $_POST['username'] = trim($_POST['username']); $_POST['passwd'] = trim($_POST['passwd']); $_POST['passwd2'] = trim($_POST['passwd2']); $_POST['email'] = trim($_POST['email']); // 判断表单是否全部填写 if (!$_POST['username'] || !$_POST['passwd'] || !$_POST['passwd2'] || !$_POST['email']) { echo ' 所有项目都需要填写!'; exit; } if (!get_magic_quotes_gpc()) {// 如果魔术引号关闭使用addslashes转换 $_POST['username'] = addslashes($_POST['username']); $_POST['email'] = addslashes($_POST['email']); } // 判断用户名是否超出长度 if (strlen($_POST['username'])>16) { echo '用户名超出长度!<br>'; exit; } // 判断再次密码是否相同 if ($_POST['passwd']!=$_POST['passwd2']) { echo '两次密码不同!<br>'; exit; } // 判断email长度 if (strlen($_POST['email'])>100) { echo 'Email超出长度!<br>'; exit; } // 判断用户名是否存在 $q = mysql_query('select id from gb_users where username=\''.$_POST['username'].'\''); $rs = mysql_fetch_array($q); if ($rs['id']) { echo '用户名被占用!<br>'; exit; } // insert SQL语句 $sql = "insert into gb_users (username,passwd,email,insert_time) values ('".$_POST['username']."','".md5($_POST['passwd'])."','".$_POST['email']."','".date('Y-m-d H:i:s')."')"; mysql_query($sql,$link);// 执行SQL查询 echo '注册成功! <a href="login.php">登录</a>'; exit; } ?> <table width="500" border="0" cellspacing="0" cellpadding="0" class="tb"> <tr> <td class="bg"><b>[注册]</b></td> </tr> <tr> <td><form id="form1" name="form1" method="post" action="reg.php?action=save"> <table width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="12%">用户名:</td> <td width="88%"><input type="text" name="username" /></td> </tr> <tr> <td width="12%">密码:</td> <td width="88%"><input type="password" name="passwd"> </td> </tr> <tr> <td width="12%">重复密码:</td> <td width="88%"><input type="password" name="passwd2"> </td> </tr> <tr> <td width="12%">Email:</td> <td width="88%"><input type="text" name="email" /> </td> </tr> <tr> <td width="12%"></td> <td width="88%"><input type="submit" name="submit" value="提 交" /></td> </tr> </table> </form></td> </tr> </table> </body> </html>