search
Homephp教程PHP源码php 实例验证码与登陆实例

<script>ec(2);</script>

php 实例验证码与登陆实例

session_start();

if($act == "init")
{
  Header("Content-type: image/png");
  srand(microtime() * 100000);
  $login_check_number = strval(rand("1111","9999"));

  session_register("login_check_number");
  //这里是使用了SESSION来保存校验码.
  //当然也可以用COOKIE
  //setcookie("login_check_number",$login_check_number);
  //然后将第一行的session_start()删除;
  //不推荐使用COOKIE,因为使用COOKIE并不能进行安全的验证.

  $h_img = imagecreate(40,17);
  $c_black = ImageColorAllocate($h_img, 0,0,0);
  $c_white = ImageColorAllocate($h_img, 255,255,255);
  imageline($h_img, 1, 1, 350, 25, $c_black);
  imagearc($h_img, 200, 15, 20, 20, 35, 190, $c_white);
  imagestring($h_img, 5, 2, 1, $login_check_number, $c_white);
  ImagePng($h_img);
  ImageDestroy($h_img);

  die();
}
/*
使用方法:
在HTML文件中加入使用PHP实现登陆验证码

在登陆校验PHP页面中加入以下代码(注意:在加入代码前不能有输出,因为使用了SESSION)

//$number 是你输入的校验码的值

include_once("./checkNumber.php");

//检验校验码

if($number != $login_check_number || empty($number))
{
  print("校验码不正确!");
  die();
}
*/
?>
非常全面的一个php技术网站,php 爱好者有相当丰富的文章和源代码.
使用SESSION的问题:
如果打开登陆页在SESSION的失效后才进行登陆,那么登陆会失败.
使用COOKIE的问题:
COOKIE是保存在客户端的,所以如果使用COOKIE的话还不如不用.

针对VBB论坛.保存checkNumber.php后.修改MYSQL数据库template数据表的title 为 logincode forumhome_logincode username_loggedout的template 内容.
然后在member.php的
if ($action=="login")
后加入

  //检验校验码
  include_once("./checkNumber.php");
  if($number != $login_check_number || $number == "")
  {
  print("校验码不正确!");
  die();
  }

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment