1)前台登录模板:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Index</title> <link rel=‘stylesheet‘ type=‘text/css‘ href=‘__CSS__/basic.css‘/> <script src="__JS__/basic.js"></script> </head> <body> <form action=‘__URL__/do_login‘ method=‘post‘ name=‘myForm‘> 用户名:<input type=‘text‘ name=‘username‘/><br/> 密 码:<input type=‘password‘ name=‘password‘/><br/> 验证码:<input type=‘text‘ name=‘code‘/> <img src="__APP__/Public/code" onclick=‘this.src=this.src+"?"+Math.random()‘/> </br/> <img src=‘__PUBLIC__/Images/login.gif‘ onclick="sub()"/> </form> </body> </html>
2)js验证并提交(basic.js):
function sub(){ var ou=document.myForm.username; var op=document.myForm.password; var oc=document.myForm.code; if(ou.value==‘‘ || op.value==‘‘ ||oc.value==‘‘){ alert(‘用户名或者密码或者验证码不能为空‘); }else{ document.myForm.submit(); } }
3)生成验证码控制器的方法:
<?php class PublicAction extends Action{ function code(){ import(‘ORG.Util.Image‘); Image::buildImageVerify(); } }
4)登录处理的控制器:
<?php class LoginAction extends Action { function index(){ $this->display(); } function do_login(){ //获取用户名和密码等。和数据库中比对,有该用户允许登录否则输出错误页面 $username=$_POST[‘username‘]; $password=$_POST[‘password‘]; $code=$_POST[‘code‘]; if($_SESSION[‘verify‘]!==md5($code)){ $this->error(‘验证码错误!‘); } $m=M(‘User‘); $where[‘username‘]=$username; $where[‘password‘]=$password; $i=$m->where($where)->count(); if($i>0){ $this->redirect(‘User/index‘); }else{ $this->error(‘该用户不存在‘); } } }
时间: 2024-10-06 12:47:57