谁能给我一个随机生成安全提问的源码  如1+1=

解决方案 »

  1.   

    和验证码的生成方式差不多吧,只不过多了一步计算求和的过程。
    比较简单的步骤如下:获得两个随机数分别赋值:
    $intRand1 = mt_rand();
    $intRand2 = mt_rand();然后随即获得加减乘除的其中一个:  $strSigns = '+-*/';
      $intRandMax  = strlen($strSigns) - 1;
      $strSign = $strSigns{mt_rand(0, $intRandMax)};然后进行运算  $intResult = 0;
      switch($strSign)
      {
        case '+':
        $intResult = $intRand1 + $intRand12;
        break;

        case '-':
        $intResult = $intRand1 - $intRand12;
        break;

        case '*':
        $intResult = $intRand1 * $intRand12;
        break;

        case '/':
        $intResult = $intRand1 / $intRand12;
        //这里的除法晕眩会有小数点,你可以用ceil,floor等函数取整,然后告诉浏览者要怎么做除法运算。
        break;
      }  
    最终得到4个变量$strSign,$intRand1,$intRand2,$intResult
    然后将$intResult存入session,将其他三个变量用图片显示在浏览器上(就和验证码的方法一样了)