2010年12月22日星期三

用存储过程检查用户登录

BEGIN
    DECLARE studentName,iip VARCHAR(16);
    DECLARE iContestId,LoginFlag,iPaperId,iMinutes INT;
    DECLARE stime datetime;
    DECLARE ntime datetime DEFAULT NOW();
    DECLARE bGameOver bit;

    select starttime,minutes into stime,iMinutes from contests where current=1;
    if stime-ntime>300 THEN
        SELECT '离考试开始时间超过5分钟,不允许用户登录!' as msg;
    else
        select stuname,contests.testid,student.login,student.starttime,paperid,gameover,ipadd into studentName,iContestId,LoginFlag,stime,iPaperId,bGameOver,iip from student,contests where student.stuid=studentid and student.password=passwd and student.contestid=contests.testid and contests.current=1;
        if (iContestId is null) THEN
            select '用户不存在或者密码错误!' as msg;
        ELSEIF bGameOver THEN
            SELECT '您已结束考试,不能再次进入考试!' as msg;
        ELSEIF LoginFlag=1 and ip<>iip THEN
            SELECT '用户已经登录考试,不能重复登录!' as msg;
        ELSEIF iPaperId is null THEN
            SELECT '试卷不存在!' as msg;
        ELSEIF stime is null THEN
            #改变用户登录标志为已登录
            #记录该用户的考试开始时间
            update student set login=1,starttime=ntime,ipadd=ip where student.stuid=studentid and contestid=iContestId;
            #返回登录成功信息,学生姓名,考试编号,试卷编号,考试开始时间,考试时长等信息
            select 'success' as msg,studentName as sname,iContestId as testid,iPaperId as paperid,ntime as starttime,iMinutes as minutes;
        ELSE
            #改变用户登录标志为已登录
            #考生换机,扣除已使用时间,开始考试时间不变
            update student set login=1,ipadd=ip where student.stuid=studentid and contestid=iContestId;
            #返回登录成功信息,学生姓名,考试编号,试卷编号,考试开始时间,剩余考试时长等信息
            select 'success' as msg,studentName as sname,iContestId as testid,iPaperId as paperid,stime as starttime,iMinutes as minutes;
        END IF;
    END IF;
END

 

小插曲:

一开始的时候存储过程写错了,红色部分写成了student.stuid=stuid ,结果记录的总是最后一个人的登录IP和登录时间,然后当用户重新登录时,因为IP不一样,总是登录不到考试系统中去

没有评论:

发表评论