1. Token.php  
    
    <?php  
    
    /*
    * Created on 2013-3-25
    *
    * To change the template for this generated file go to
    * Window - Preferences - PHPeclipse - PHP - Code Templates
    */
    function getToken($len = 32, $md5 = true) {
    # Seed random number generator
    # Only needed for PHP versions prior to 4.2
    mt_srand((double) microtime() * 1000000);
    # Array of characters, adjust as desired
    $chars = array (
    'Q',
    '@',
    '8',
    'y',
    '%',
    '^',
    '5',
    'Z',
    '(',
    'G',
    '_',
    'O',
    '`',
    'S',
    '-',
    'N',
    '<',
    'D',
    '{',
    '}',
    '[',
    ']',
    'h',
    ';',
    'W',
    '.',
    '/',
    '|',
    ':',
    '1',
    'E',
    'L',
    '4',
    '&',
    '6',
    '7',
    '#',
    '9',
    'a',
    'A',
    'b',
    'B',
    '~',
    'C',
    'd',
    '>',
    'e',
    '2',
    'f',
    'P',
    'g',
    ')',
    '?',
    'H',
    'i',
    'X',
    'U',
    'J',
    'k',
    'r',
    'l',
    '3',
    't',
    'M',
    'n',
    '=',
    'o',
    '+',
    'p',
    'F',
    'q',
    '!',
    'K',
    'R',
    's',
    'c',
    'm',
    'T',
    'v',
    'j',
    'u',
    'V',
    'w',
    ',',
    'x',
    'I',
    '$',
    'Y',
    'z',
    '*'
    );
    # Array indice friendly number of chars;
    $numChars = count($chars) - 1;
    $token = '';
    # Create random token at the specified length
    for ($i = 0; $i < $len; $i++)
    $token .= $chars[mt_rand(0, $numChars)];
    # Should token be run through md5?
    if ($md5) {
    # Number of 32 char chunks
    $chunks = ceil(strlen($token) / 32);
    $md5token = '';
    # Run each chunk through md5
    for ($i = 1; $i <= $chunks; $i++)
    $md5token .= md5(substr($token, $i * 32 - 32, 32));
    # Trim the token
    $token = substr($md5token, 0, $len);
    }
    return $token;
    }
    ?>
    form.php <?php
    include_once("token.php");
    $token = getToken();
    session_start();
    $_SESSION['token'] = $token;
    ?>
    <form action="action.php" method="post"
    <input type="hidden" name="token" value="<?=$token?>" />
    <!-- 其他input submit之类的 -->
    </form>
    action.php <?php
    session_start();
    if($_POST['token'] == $_SESSION['token']){
    unset($_SESSION['token']);
    echo "这是一个正常的提交请求";
    }else{
    echo "这是一个非法的提交请求";
    }
    ?>

最新文章

  1. actor concurrency
  2. C++ Const引用详解
  3. SQL数据库基础(二)
  4. linux locate: command not found
  5. php中关于抽象(abstract)类和抽象方法的问题解析
  6. Microsoft Visual Studio 语言切换
  7. css虚线和css双实线
  8. 【POJ 1830】 开关问题 (高斯消元)
  9. [R]django的HTTPREQUEST对象
  10. win32线程池代码(WinApi/C++)
  11. IDL 创建数组
  12. 我的第一个python web开发框架(12)——工具函数包说明(三)
  13. Java基础总结--面向对象2
  14. 我从.net转到java的心得和体会
  15. Java PDF页面设置——页面大小、页边距、纸张方向、页面旋转
  16. [Swift]LeetCode166. 分数到小数 | Fraction to Recurring Decimal
  17. [ERROR] --gtid-mode=ON or UPGRADE_STEP_1 or UPGRADE_STEP_2 requires --log-bin and --log-slave-updates
  18. js顺序播放列表中的音乐
  19. C#中哈希表(HashTable)的用法详解以及和Dictionary比较
  20. SpringBoot入门之Thymeleaf的使用

热门文章

  1. js内置数据类型
  2. vuejs组件通信
  3. iOS 载入图片选择imageNamed 方法还是 imageWithContentsOfFile?
  4. 剑指offer 面试5题
  5. $.messager.confirm修改弹出框按钮提示文字
  6. hadoop linux 杂记
  7. PAT 天梯赛 L1-037. A除以B 【水】
  8. VC引用静态库
  9. 使用git从本地上传至git码云远程仓库
  10. Cocos2d-x项目移植到WP8系列之七:中文显示乱码