jquery.idcode实现登录验证码

jquery.idcode 两个文件,使用比较方便 jquery.idcode.css,jquery.idcode.js

下载 http://www.jq22.com/jquery-info15396

html里 

<span id="idcode"></span>

js里

$.idcode.setCode();

   var idcode = $.idcode.getCode();     取值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现: 1. 创建一个 HTML 页面,并添加所需的元素,如用户名、密码、验证码登录按钮等。 ```html <!DOCTYPE html> <html> <head> <title>Login Page</title> <link rel="stylesheet" type="text/css" href="styles.css"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="script.js"></script> </head> <body> <div class="login-container"> <h2>Login</h2> <form> <label for="username">Username</label> <input type="text" id="username" name="username" required> <label for="password">Password</label> <input type="password" id="password" name="password" required> <label for="captcha">Captcha</label> <div class="captcha-container"> <input type="text" id="captcha" name="captcha" required> <img src="captcha.php" alt="captcha"> </div> <button type="submit" id="login-btn">Login</button> </form> </div> </body> </html> ``` 2. 创建一个 CSS 文件,并添加所需的样式,如登录框的样式等。 ```css .login-container { max-width: 400px; margin: 0 auto; padding: 20px; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 0 10px #ccc; background-color: #fff; } .login-container h2 { text-align: center; margin-bottom: 20px; } .login-container label { display: block; margin-bottom: 10px; } .login-container input[type="text"], .login-container input[type="password"] { display: block; width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 5px; margin-bottom: 20px; } .captcha-container { display: flex; align-items: center; margin-bottom: 20px; } .captcha-container img { margin-left: 20px; } #login-btn { display: block; width: 100%; padding: 10px; border: none; border-radius: 5px; background-color: #007bff; color: #fff; font-size: 16px; cursor: pointer; } ``` 3. 创建一个 PHP 文件,用于生成验证码图片。 ```php <?php session_start(); $length = 6; $width = 100; $height = 40; $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $random_string = ''; for ($i = 0; $i < $length; $i++) { $random_string .= $characters[rand(0, strlen($characters) - 1)]; } $_SESSION['captcha'] = $random_string; $image = imagecreatetruecolor($width, $height); $bg_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 0, 0, 0); imagefilledrectangle($image, 0, 0, $width, $height, $bg_color); imagettftext($image, 20, 0, 10, 30, $text_color, 'arial.ttf', $random_string); header('Content-type: image/png'); imagepng($image); imagedestroy($image); ?> ``` 4. 创建一个 JavaScript 文件,用于验证用户输入的信息和提交表单。 ```js $(document).ready(function() { $('form').submit(function(e) { e.preventDefault(); var username = $('#username').val(); var password = $('#password').val(); var captcha = $('#captcha').val(); var captcha_session = '<?php echo $_SESSION["captcha"]; ?>'; if (username == '') { alert('Please enter username.'); return; } if (password == '') { alert('Please enter password.'); return; } if (captcha == '') { alert('Please enter captcha.'); return; } if (captcha != captcha_session) { alert('Invalid captcha.'); return; } $.ajax({ url: 'login.php', type: 'POST', data: {username: username, password: password}, success: function(response) { if (response == 'success') { alert('Login successful.'); location.reload(); } else { alert('Login failed.'); } } }); }); }); ``` 5. 创建一个 PHP 文件,用于验证用户输入的信息和登录。 ```php <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; if ($username == 'admin' && $password == '123456') { $_SESSION['user'] = $username; echo 'success'; } else { echo 'error'; } ?> ``` 以上是基本的实现方法,其中验证码生成使用了 PHP 的 GD 库。可以将代码部署在本地或者服务器上运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值