一個完整php驗證碼執行個體程式[帶demo]

來源:互聯網
上載者:User
 代碼如下 複製代碼

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.111cn.net/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
<title>login</title>
<link rel="stylesheet" type="text/css教程" href="/tryit.css教程" />
<script type="text/網頁特效">
function getpinimg()
{
 document.getelementbyid("pinimg").src="log_demo.php教程?a=pin&r=" + math.random();
}
</script>
</head>

<body>
<form action="log_demo.php" method="get">
 <input type="hidden" name="a" value="log" />
 user:<input type="text" name="user" size="20" value="test" /><br /><br />
 password:<input type="password" name="pass" size="12" value="123456" /><br /><br />
 pincode:<input type="text" name="pin" size="8" value="" />
 <input type="button" value="getpin" onclick="getpinimg();">
   <img id="pinimg" src="log_demo.php?a=pin" onclick="getpinimg();" />
 <input type="submit" value="submit"><br />
</form>
</body>
</html>

log_demo.php代碼

 代碼如下 複製代碼

<?php
require_once("log_aux.php");

if ($_get['a'] == "pin") {
 getpinimg();
 exit;
}

if ($_get['a'] == "log") {
 $ret = checkpincookie($_get['pin'], 300);
 if ($ret < 0) {
  echo "pin error:$ret ";
  exit;
 }
 // check user name and password.
 echo "ok..$ret ";
 if ("123xx" == 123) {
  echo "dddd ";
 }
 echo $_get['u'];
}

log_aux.php代碼

 代碼如下 複製代碼

<?php
$magic_a = "www.111cn.net34589";
$magic_b = "234566***+";

function getpincookie($pin_code)
{
 global $magic_a;
 global $magic_b;
 $now = time();
 $client_ip = $_server['remote_addr'];
 $pin = md5($now . $client_ip . $magic_a . $pin_code . $maigc_b) . $now;
 return $pin;
}

function checkpincookie($pin_code_in, $timeout=300) // 5 miniuteswww.111cn.net
{
 global $magic_a;
 global $magic_b;
 $pin = substr($_cookie['pin'], 0, 32);
 $time = substr($_cookie['pin'], 32);
 $now = time();
 if ($now - $time > $timeout) {
  return -100;  // time_out
 }
 $pin_code_in = strtolower($pin_code_in);
 $client_ip = $_server['remote_addr'];
 $pin_2 = md5($time . $client_ip . $magic_a . $pin_code_in . $maigc_b);
 if ($pin === $pin_2) {
  return 0;
 } else {
  return -200; // pincode is error.
 }
}

function getpinimg()
{
// $rnd = rand(0, 10);
 $rnd = 0;
 $path = "./www.111cn.net/" . substr("00000$rnd", -4);
 for ($i = 0; $i < 4; ++$i) {
  if (__getpinimg($path)) {
   break;
  }
 }
 exit;
}

function __getpinimg($path)
{
 require("$path/pinmap.php");
 $pinv = $pin[rand(0, $pin_max)];
 list ($file, $pin_code) = explode('#', $pinv);
 $filepath = "$path/$file$pin_ext";

 //  output pincode image.
 $fh = fopen($filepath, "rb");
 if (!$fh) {
  return false;
 }
 $data = fread($fh, 10240); // php just read max size: 8k.
 fclose($fh);

 if (strlen($data) < 200) {
  return false;
 }
 // set cookie;
 header("content-type: image/jpeg");
 $pin_code = strtolower($pin_code);
 $pin = getpincookie($pin_code);
 setcookie("pin", $pin);

 echo $data;
 return true;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.