最好在客户端进行初步判断:其是否输入了用户名;该项验证请参考http://www.yygs.com/benifit/admin_index.htm,你通过查看页面源文件即可看到使用的方法。
在服务器端读数据库表进行用户名的验证。
以下为服务器端验证源代码:
<?php
Session_Start();
if( $code && $password ) {
$code = trim( $code );
$password = trim( $password );
此处连接数据库,用你自己的。
$checkid = "select * from 你的用户和密码存储表名 where code=\"".$code."\"";//两个字段,即code和password
$idresu = mysql_query( $checkid, $db);
if( mysql_error() ) { echo "数据库检索失败!"; return; }
//如果没有检索到用户名,即该用户名不存在
if( !mysql_num_rows($idresu) ||
//如果检索到用户名,但相应的密码不正确
strcmp($password,mysql_result($idresu,0,"password")) ) {
//断开与数据库的联接
mysql_close( $db );
//显示错误信息
//echo"不正确的用户名或密码!";
session_destroy(); //liu add
Header("Location: errormsg.php?errmsg=不正确的用户名或密码!");
//并返回
reutrn;
//如果用户名和密码都正确,则执行ELSE语句
} else {
//注册一个新的Session变量,名为:current_user
Session_Register("current_user");
//为该变量赋值为用户名
$current_user = $code;
include("你想转到的页面文件名称");//验证完毕后转到另一个页面。
}
}
?>
在服务器端读数据库表进行用户名的验证。
以下为服务器端验证源代码:
<?php
Session_Start();
if( $code && $password ) {
$code = trim( $code );
$password = trim( $password );
此处连接数据库,用你自己的。
$checkid = "select * from 你的用户和密码存储表名 where code=\"".$code."\"";//两个字段,即code和password
$idresu = mysql_query( $checkid, $db);
if( mysql_error() ) { echo "数据库检索失败!"; return; }
//如果没有检索到用户名,即该用户名不存在
if( !mysql_num_rows($idresu) ||
//如果检索到用户名,但相应的密码不正确
strcmp($password,mysql_result($idresu,0,"password")) ) {
//断开与数据库的联接
mysql_close( $db );
//显示错误信息
//echo"不正确的用户名或密码!";
session_destroy(); //liu add
Header("Location: errormsg.php?errmsg=不正确的用户名或密码!");
//并返回
reutrn;
//如果用户名和密码都正确,则执行ELSE语句
} else {
//注册一个新的Session变量,名为:current_user
Session_Register("current_user");
//为该变量赋值为用户名
$current_user = $code;
include("你想转到的页面文件名称");//验证完毕后转到另一个页面。
}
}
?>
如有不懂的地方,请给我写信。[email protected]
请记住多散分!
服务端是一定要判断的,不然假设你不允许用小于6位的密码而又仅用javascript判断了的话,很容易就能注册出来密码小于6位的用户。
<script language="JavaScript"><!--
function Form_Validator(theForm)
{
if (theForm.username.value=="")
{
alert("用户名不能为空!");
theForm.username.focus();
return(false);
}
return(true);
}
//-->
</script>
<form name=sform action="" method=post onsubmit="checkform()">
其中checkform() 为验证函数
<script language=javascript>
function isdigit() { return (('0'<=c) && (c<='9')); } function isalpha(c) { return ((('a'<=c) && (c<='z')) || (('A'<=c) && (c<='Z'))) }
functio checkform()
{
name=document.sform.username.value;//sform表单名 username用户名
if(name="")
{
alert("青输入用户名");
document.sform.user.foucs();
return false;
}
for (nIndex=0; nIndex<name.length; nIndex++) { c = name.charAt(nIndex); if (!(isdigit(c) || isalpha(c))) { alert("用户名只能使用字母、数字,并且不能使用中文"); document.sform.username.focus(); return false; } }
//下面还可以其他的,字段,你可以自己去写了
//有问题可以一起商量 我得邮箱是[email protected]
..........
}
</script>