Discuz教程网

PHP 验证码的实现代码

[复制链接]
authicon dly 发表于 2011-9-12 14:16:24 | 显示全部楼层 |阅读模式
checkcode.php 生成验证码图片,还有变量 $_SESSION[check_pic]。
代码如下:

  1. <?
  2. session_start();
  3. for($i=0; $i<4; $i++){
  4. $rand.= dechex(rand(1,15));
  5. }
  6. $_SESSION[check_pic]=$rand;
  7. //echo $_SESSION[check_pic];
  8. // 设置图片大小
  9. $im = imagecreatetruecolor(100,30);
  10. // 设置颜色
  11. $bg=imagecolorallocate($im,0,0,0);
  12. $te=imagecolorallocate($im,255,255,255);
  13. // 把字符串写在图像左上角
  14. imagestring($im,rand(5,6),rand(25,30),5,$rand,$te);
  15. // 输出图像
  16. header("Content-type:image/jpeg");
  17. imagejpeg($im);
  18. ?>
复制代码


form.php
通过 <img src="checkcode.php"> 调用生成的验证码图片
代码如下:

  1. <div class="bottomAds">
  2. <fieldset class="bottomAds_quote"><legend>留言</legend>
  3. <div class="ads">
  4. <form action="../utity/post.php" method="post" onsubmit="return chkinput(this)">
  5. <input name="name" type="text" /> 您的名字
  6. <input name="email" type="text" /> 您的邮件
  7. <input name="website" type="text" /> 您的网站
  8. <textarea name="content" style="width:340; height:150;">
  9. </textarea><br />
  10. <img src="checkcode.php"><input type="text" name="check"><br />
  11. <input type="submit" value="提交" />
  12. </form>
  13. </div>
  14. <br clear="both" />
  15. </fieldset>
复制代码


imagestring($im,rand(5,6),rand(25,30),5,$rand,$te); 使用了 int imagestring(int im, int font, int x, int y, string s, int col); 函数,这个函数用于绘横式字符串。
这个函数在图片上绘出水平的横式字符串。参数 font 为字形,设为 1 到 5 表示使用默认字形。参数 x、y 为字符串起点坐标。字符串的内容放在参数 s 上。参数 col 表示字符串的颜色。
post.php
比较 $_POST[check] 与 $_SESSION[check_pic],若相等则执行数据库插入操作。不相等就返回上一页。
代码如下:

  1. <?php
  2. session_start();
  3. if(isset($_POST[check]))
  4. {
  5. if($_POST[check] == $_SESSION[check_pic])
  6. {
  7. // echo "验证码正确".$_SESSION[check_pic];
  8. require("dbinfo.php");
  9. $name = $_POST['name'];
  10. $email = $_POST['email'];
  11. $website = $_POST['website'];
  12. $content = $_POST['content'];
  13. $date = date("Y-m-d h:m:s");
  14. // 连接到 MySQL 服务器
  15. $connection = mysql_connect ($host, $username, $password);
  16. if (!$connection)
  17. {
  18. die('Not connected : ' . mysql_error());
  19. }
  20. // 设置活动的 MySQL 数据库
  21. $db_selected = mysql_select_db($database, $connection);
  22. if (!$db_selected)
  23. {
  24. die ('Can\'t use db : ' . mysql_error());
  25. }
  26. // 向数据库插入数据
  27. $query = "insert into table (nowamagic_name, nowamagic_email, nowamagic_website, nowamagic_content, nowamagic_date) values ('$name','$email','$website','$content','$date')";
  28. $result = mysql_query($query);
  29. if($result)
  30. {
  31. echo "<script>alert('提交成功'); history.go(-1);</script>";
  32. }
  33. if (!$result)
  34. {
  35. die('Invalid query: ' . mysql_error());
  36. }
  37. }
  38. else
  39. {
  40. echo "<script>alert('验证码错误'); history.go(-1);</script>";
  41. }
  42. }
  43. ?>
复制代码






上一篇:一个PHP验证码类代码分享(已封装成类)
下一篇:PHP中用hash实现的数组
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 09:36

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表