`

Ext带验证码登录框可刷新验证码

阅读更多
js代码

程序代码
// @filename   login_panel.js
// @version    0.1
// @author     jayboy
// @contact    115519889@qq.com
// @update     2008-11-16
// @comment    jayboy.Cn
function reloadcode(){//刷新验证码函数
    var verify = document.getElementById('safecode');
    verify.setAttribute('src', 'chknumber.php?' + Math.random());
}

Ext.onReady(function(){
    var select_status = new Ext.data.JsonStore({
        url: "./js/_status.js",
        fields: ['name', 'value'],
        totalProperty: "total",
        root: "result",
        id: "name"
    });
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    var login = new Ext.FormPanel({
        id: 'login',
        name: 'login',
        labelWidth: 60,
        baseCls: 'x-plain',
        bodyStyle: 'padding:5px 5px 0',
        width: 350,
        border: false,
        defaults: {
            width: 230
        },
        defaultType: 'textfield',//默认字段类型
        items: [{
            fieldLabel: '用户名',
            name: 'username',
            allowBlank: false,//禁止为空
            blankText: '用户名不能为空,学生输入学号'
        }, {
            fieldLabel: '密码',
            name: 'userpass',
            inputType: 'password',
            allowBlank: false,//禁止为空
            blankText: '密码不能为空'//可限制多种类型,具体参照api文档
        }, {
            xtype: 'combo',
            name: 'status',
            fieldLabel: '登录身份',
            displayField: 'name',
            valueField: 'value',
            store: select_status,
            triggerAction: 'all',
            emptyText: '学生',
            selectOnFocus: true,
            editable: false
        }, {
            cls: 'key',
            name: 'chknumber',
            id: 'randCode',
            fieldLabel: '验证码',
            maxLength: 4,
            width: 80,
            allowBlank: false,
            blankText: '验证码不能为空!'
        }],
        keys: {
            key: 13,
            fn: submit_login
        },
        buttons: [{
            text: '登录',
            handler: submit_login
        }, {
            text: '取消',
            handler: function(){
                login.form.reset();
            }//重置表单
        }]
    });
   
    function submit_login(){
        if (win.getComponent('login').form.isValid()) {
            win.getComponent('login').form.submit({
                url: 'login_chk.php',
                waitTitle: '提示',
                method: 'POST',
                waitMsg: '正在登录验证,请稍候...',
                success: function(form, action){
                    var loginResult = action.result.success;
                    if (loginResult == false) {
                        Ext.MessageBox.alert('提示', action.result.message);
                    }
                    else
                        if (loginResult == true) {
                            Ext.MessageBox.alert('提示', action.result.message);
                            window.location.href = 'main.php';
                        }
                },
                failure: function(form, action){
                    Ext.MessageBox.alert('提示', action.result.message);
                    win.getComponent('login').form.reset();
                   
                }
            });
        }
    }
    win = new Ext.Window({
        id: 'win',
        title: '登陆',
        layout: 'fit',
        width: 360,
        height: 200,
        bodyStyle: 'padding:5px;',
        maximizable: false,
        closeAction: 'close',
        closable: false,
        collapsible: true,
        plain: true,
        buttonAlign: 'center',
        items: login
    });
    win.show();
    var bd = Ext.getDom('randCode');
    var bd2 = Ext.get(bd.parentNode);
    bd2.createChild([{
        tag: 'span',
        html: '     <a href="javascript:reloadcode();">'
    }, {
        tag: 'img',
        id: 'safecode',
        src: 'chknumber.php',
        align: 'absbottom'
    }, {
        tag: 'span',
        html: '</a>  <b>点击图片可刷新</b>'
    }]);
});

程序代码
<?
//checkNum.php

  Function   getRandNumber   ($fMin,   $fMax)   {  
    srand((double)microtime()*1000000);
    $fLen   =   "%0 ".strlen($fMax). "d ";
    Return   sprintf($fLen,   rand($fMin,$fMax));
  }  
$str=getRandNumber(1000,9999);
setcookie("code",$str);



//function images($str){
//$str=random(4); //随机生成的字符串

$width = 50; //验证码图片的宽度
$height = 22; //验证码图片的高度
@header("Content-Type:image/png");
//$_SESSION["code"] = $str;
//echo $str;
$im=imagecreate($width,$height);
//背景色
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
//模糊点颜色
$pix=imagecolorallocate($im,187,230,247);
//字体色
$font=imagecolorallocate($im,41,163,238);
//绘模糊作用的点
mt_srand();
for($i=0;$i<1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 3,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
//$_SESSION["code"] = $str;

//}

?>

  • 大小: 48.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics