1. $(".login").live('click',function(){

  2. var username=$(".input_user").val();
  3. var password=$(".input_ps").val();
  4. var code=$('.input_checkcode').val();
  5. if(username==""){
  6. alert("用户名不能为空");
  7. return false;
  8. }
  9. if(password==""){
  10. alert("密码不能为空");
  11. return false;
  12. }

  13. var URL="checkLogin. var DATA="&username="+username+"&password="+password+"&code="+code;

  14. $.getJSON(URL+DATA,function(json){
  15. if(json.code=='code_error'){
  16. alert('验证码错误,请重新输入验证码');
  17. }
  18. if(json.username=='true_u'&&json.password=='true_p'){
  19. //alert(json.username+"|"+username+'...1');
  20. window.location="index. }
  21. if(json.username=='error_u'||json.password=='error_p'){
  22. alert("用户名输入或密码输入有误,请检查后重新登陆!");
  23. window.location="login. }
  24. });
  25. });
复制代码

后台checkLogin.

  1. $code=$_GET['code'];
  2. if($code!=$_SESSION['captchaCode']['content'])
  3. {$adminInfo['code']='code_error';};
  4. if($row['username']==$username&&$row['password']==$password){
  5. $_SESSION['username']=$row['username'];
  6. $adminInfo['username']='true_u';
  7. $adminInfo['password']='true_p';
  8. mysql_close();
  9. }else
  10. if($row['username']!=$username){
  11. $adminInfo['username']='error_u';
  12. }
  13. if($row['password']!=$password){
  14. $adminInfo['password']='error_p';
  15. }
  16. //var_dump($adminInfo);exit;
  17. echo json_encode($adminInfo);
复制代码

具体如下:checkCode.class.

  1. * Captcha Class base on * @author Design
  2. * @version 1.0
  3. * @copyright js8.in 2010 bbs.it-home.org
  4. * @demo
  5. * include('captchaClass.* $captchaDemo=new Captcha();
  6. * $captchaDemo->createImage();
  7. */
  8. class Captcha{
  9. //@定义验证码图片高度
  10. private $height;
  11. //@定义验证码图片宽度
  12. private $width;
  13. //@定义验证码字符个数
  14. private $textNum;
  15. //@定义验证码字符内容
  16. private $textContent;
  17. //@定义字符颜色
  18. private $fontColor;
  19. //@定义随机出的文字颜色
  20. private $randFontColor;
  21. //@定义字体大小
  22. private $fontSize;
  23. //@定义字体
  24. private $fontFamily;
  25. //@定义背景颜色
  26. private $bgColor;
  27. //@定义随机出的背景颜色
  28. private $randBgColor;
  29. //@定义字符语言
  30. private $textLang;
  31. //@定义干扰点数量
  32. private $noisePoint;
  33. //@定义干扰线数量
  34. private $noiseLine;
  35. //@定义是否扭曲
  36. private $distortion;
  37. //@定义扭曲图片源
  38. private $distortionImage;
  39. //@定义是否有边框
  40. private $showBorder;
  41. //@定义验证码图片源
  42. private $image;
  43. //@Constructor 构造函数
  44. public function Captcha(){
  45. $this->textNum=4;
  46. $this->fontSize=16;
  47. $this->fontFamily='c:\\windows\\fonts\SIMYOU.ttf';//设置中文字体,可以改成linux的目录
  48. $this->textLang='en';
  49. $this->noisePoint=30;
  50. $this->noiseLine=3;
  51. $this->distortion=false;
  52. $this->showBorder=false;
  53. }
  54. //@设置图片宽度
  55. public function setWidth($w){
  56. $this->width=$w;
  57. }
  58. //@设置图片高度
  59. public function setHeight($h){
  60. $this->height=$h;
  61. }
  62. //@设置字符个数
  63. public function setTextNumber($textN){
  64. $this->textNum=$textN;
  65. }
  66. //@设置字符颜色
  67. public function setFontColor($fc){
  68. $this->fontColor=sscanf($fc,'#%2x%2x%2x');
  69. }
  70. //@设置字号
  71. public function setFontSize($n){
  72. $this->fontSize=$n;
  73. }
  74. //@设置字体
  75. public function setFontFamily($ffUrl){
  76. $this->fontFamily=$ffUrl;
  77. }
  78. //@设置字符语言
  79. public function setTextLang($lang){
  80. $this->textLang=$lang;
  81. }
  82. //@设置图片背景
  83. public function setBgColor($bc){
  84. $this->bgColor=sscanf($bc,'#%2x%2x%2x');
  85. }
  86. //@设置干扰点数量
  87. public function setNoisePoint($n){
  88. $this->noisePoint=$n;
  89. }
  90. //@设置干扰线数量
  91. public function setNoiseLine($n){
  92. $this->noiseLine=$n;
  93. }
  94. //@设置是否扭曲
  95. public function setDistortion($b){
  96. $this->distortion=$b;
  97. }
  98. //@设置是否显示边框
  99. public function setShowBorder($border){
  100. $this->showBorder=$border;
  101. }
  102. //@初始化验证码图片
  103. public function initImage(){
  104. if(empty($this->width)){$this->width=floor($this->fontSize*1.3)*$this->textNum+10;}
  105. if(empty($this->height)){$this->height=$this->fontSize*2;}
  106. $this->image=imagecreatetruecolor($this->width,$this->height);
  107. if(empty($this->bgColor)){
  108. $this->randBgColor=imagecolorallocate($this->image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
  109. }else{
  110. $this->randBgColor=imagecolorallocate($this->image,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]);
  111. }
  112. imagefill($this->image,0,0,$this->randBgColor);
  113. }
  114. //@产生随机字符
  115. public function randText($type){
  116. $string='';
  117. switch($type){
  118. case 'en':
  119. $str='ABCDEFGHJKLMNPQRSTUVWXY3456789';
  120. for($i=0;$i<$this->textNum;$i++){
  121. $string=$string.','.$str[mt_rand(0,29)];
  122. }
  123. break;
  124. case 'cn':
  125. for($i=0;$i<$this->textNum;$i++) {
  126. $string=$string.','.chr(rand(0xB0,0xCC)).chr(rand(0xA1,0xBB));
  127. }
  128. $string=iconv('GB2312','UTF-8',$string); //转换编码到utf8
  129. break;
  130. }
  131. return substr($string,1);
  132. }
  133. //@输出文字到验证码
  134. public function createText(){
  135. $textArray=explode(',',$this->randText($this->textLang));
  136. $this->textContent=join('',$textArray);
  137. if(empty($this->fontColor)){
  138. $this->randFontColor=imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
  139. }else{
  140. $this->randFontColor=imagecolorallocate($this->image,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  141. }
  142. for($i=0;$i<$this->textNum;$i++){
  143. $angle=mt_rand(-1,1)*mt_rand(1,20);
  144. imagettftext($this->image,$this->fontSize,$angle,5+$i*floor($this->fontSize*1.3),floor($this->height*0.75),$this->randFontColor,$this->fontFamily,$textArray[$i]);
  145. }
  146. }
  147. //@生成干扰点
  148. public function createNoisePoint(){
  149. for($i=0;$i<$this->noisePoint;$i++){
  150. $pointColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  151. imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$pointColor);
  152. }
  153. }
  154. //@产生干扰线
  155. public function createNoiseLine(){
  156. for($i=0;$i<$this->noiseLine;$i++) {
  157. $lineColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),20);
  158. imageline($this->image,0,mt_rand(0,$this->width),$this->width,mt_rand(0,$this->height),$lineColor);
  159. }
  160. }
  161. //@扭曲文字
  162. public function distortionText(){
  163. $this->distortionImage=imagecreatetruecolor($this->width,$this->height);
  164. imagefill($this->distortionImage,0,0,$this->randBgColor);
  165. for($x=0;$x<$this->width;$x++){
  166. for($y=0;$y<$this->height;$y++){
  167. $rgbColor=imagecolorat($this->image,$x,$y);
  168. imagesetpixel($this->distortionImage,(int)($x+sin($y/$this->height*2*M_PI-M_PI*0.5)*3),$y,$rgbColor);
  169. }
  170. }
  171. $this->image=$this->distortionImage;
  172. }
  173. //@生成验证码图片
  174. public function createImage(){
  175. $this->initImage(); //创建基本图片
  176. $this->createText(); //输出验证码字符
  177. if($this->distortion){$this->distortionText();} //扭曲文字
  178. $this->createNoisePoint(); //产生干扰点
  179. $this->createNoiseLine(); //产生干扰线
  180. if($this->showBorder){imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$this->randFontColor);} //添加边框
  181. imagepng($this->image);
  182. imagedestroy($this->image);
  183. if($this->distortion){imagedestroy($this->$distortionImage);}
  184. return $this->textContent;
  185. }
  186. }
  187. ?>
  188. code.header("Content-type:image/png");
  189. include('checkCode.class.$captcha5=new Captcha();
  190. //@设置验证码宽度
  191. //$captcha5->setWidth(200);
  192. //@设置验证码高度
  193. //$captcha5->setHeight(50);
  194. //@设置字符个数
  195. $captcha5->setTextNumber(4);
  196. //@设置字符颜色
  197. //$captcha5->setFontColor('#ffffff');
  198. //@设置字号大小
  199. //$captcha5->setFontSize(25);
  200. //@设置字体
  201. $captcha5->setFontFamily('c:\\windows\\fonts\\comic.TTF');
  202. //@设置语言
  203. $captcha5->setTextLang('en');
  204. //@设置背景颜色
  205. //$captcha5->setBgColor('#000000');
  206. //@设置干扰点数量
  207. //$captcha5->setNoisePoint(600);
  208. //@设置干扰线数量
  209. //$captcha5->setNoiseLine(10);
  210. //@设置是否扭曲
  211. //$captcha5->setDistortion(true);
  212. //@设置是否显示边框
  213. $captcha5->setShowBorder(true);
  214. //输出验证码
  215. $code=$captcha5->createImage();
  216. $_SESSION['captchaCode']['content']=$code;
  217. //$_SESSION['captchaCode']['time']=microtime();
  218. ?>
复制代码

login.

  1. 验证码:
  • 复制代码


  • 09-12 22:16