PHP圖形圖像的典型應用 --常用圖像的應用(驗證碼)

php生成動態的驗證碼,是php防止惡意登陸或者注冊等常規手段-廢話不多說,直接看例子。(只是一個簡單的應用,如果要安全或者更復雜的,請期待我以后的文章)

PHP生成驗證碼核心文件 (checks.php):

<?php/*成生隨機的驗證碼。此實例只是一個簡單的例子,如果不想被人識別,還可以加噪點等干擾技術*/session_start(); //啟動sessionheader("Content-type:image/x-png"); //設置創建圖像的格式$image_width = 70;  //設置圖像的寬度$image_height = 18;  //設置圖像的高度srand(microtime()*100000);  //設置隨機數的種子。 --這里關于srand和microtime函數請自行查閱php手冊for($i=0; $i<4; $i++)    //循環輸出一個4位數的隨機數
    {$new_number.=dechex(rand(0,15)); //將十進制隨機數轉為十六進制
    }/*將獲取的隨機數驗證碼寫入到Session變量中  --這里注意php的變量作用范圍,和別的高級語言可能有些不同,所以要理解,自己去查手冊*/$_SESSION['check_checks'] = $new_number;$num_image = imagecreate($image_width,$image_height);  //創建一個畫布imagecolorallocate($num_image,255,255,255);  //設置畫布的顏色for($i=0; $i<strlen($_SESSION['check_checks']); $i++)  //循環讀取session變量中的驗證碼
    {$font = mt_rand(3,5); //設置隨機的字體大小$x = mt_rand(1,8)+$image_width*$i/4;  //設置隨機字符所在的位置的X坐標$y = mt_rand(1,$image_height/4);  //設置隨機字符所在的位置的Y坐標//設置字符的顏色$color = imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//水平畫出一行字符串 --輸出字符,此函數自行查閱php手冊。要GD2庫支持imagestring($num_image,$font,$x,$y,$_SESSION['check_checks'][$i],$color);}imagepng($num_image); //生成png格式的圖像imagedestroy($num_image);  //結束圖像,釋放資源
?>
View Code

PHP頁面驗證文件,判斷是否和生成的驗證碼相同 (login.php):

<?phpheader("Content-type:text/html;charset=utf-8");session_start();  //初始化sessionif($_POST['checks'] != "")  //判斷是否請求過來的為空
    {//如果不為空進行一個正則的替換,替換掉所有的空格 preg_replace()函數中用''而不是""來表示$checks = preg_replace('/[\s| ]/','',$_POST['checks']);echo "<script type='text/javascript'>prompt('這是您輸入的驗證碼:','$checks');</script>";if($checks == ""){echo "<script type='text/javascript'>alert('驗證碼不能為空');window.location.href='index.php';</script>";    }//如果用戶輸入驗證碼的值與隨機生成的驗證碼的值相等,則彈出登錄成功提示if($checks == $_SESSION['check_checks']){echo "<script type='text/javascript'>alert('用戶登錄成功');window.location.href='index.php';</script>";    }else{echo "<script type='text/javascript'>alert('您輸入的驗證碼不正確!');window.location.href='index.php';</script>";    }}else{echo "<script type='text/javascript'>alert('您沒有輸入驗證碼!');window.location.href='index.php';</script>";    }
?>
View Code

頁面呈現登陸文件 (index.html或者index.php):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script type="text/javascript">//封裝javascript的trim去掉字符串空格功能。function trim(strToTrim){//用正則來返回替換后的字符。--功能,去掉所有的空格!包括換行符等。 關于正則知識,請自行查閱資料return strToTrim.replace(/(\s+)/g,"");}function yz(){//先用js檢測是否已經輸入驗證碼,減少服務器壓力,當然服務器那邊也是要驗證的if(trim(form1.checks.value) == ""){alert("對不起!你沒有輸入驗證碼!");return false; //返回flase不提交
        }return true;}
</script>
</head><body><div id="main"><form action="login.php" method="post" name="form1"><label>驗證碼:</label><input type="text" name="checks" /><img src="checks.php" title="驗證碼" alt="驗證碼顯示錯誤" /><br /><input type="submit" value="提交驗證" onclick="return yz();" /></form></div>
</body>
</html>
View Code

?

轉載于:https://www.cnblogs.com/clouds008/archive/2013/06/11/3131970.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274210.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274210.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274210.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

接口練習

前臺代碼&#xff1a; <form id"form1" runat"server"> <div> 見面時間:<asp:TextBox ID"MeetTime" runat"server"></asp:TextBox><br /> 見面地點:<asp:TextBox ID"MeetAddr…

寫saas創業的書_我在SaaS創業公司擔任UX設計師的第一個月中學到的三件事

寫saas創業的書I recently transitioned from being a copywriter at an ad agency to a UX Designer at a SaaS startup. To add more multidisciplinary skills into the mix, I graduated with a Bachelor in Accountancy.我最近從一名廣告代理商的撰稿人過渡到了SaaS初創公…

ui項目答辯中學到了什么_我在UI設計9年中學到的12件事

ui項目答辯中學到了什么重點 (Top highlight)I know these can seem a bit clich but I will try to explain everything from my own experience.我知道這些內容似乎有些陳詞濫調&#xff0c;但我會嘗試根據自己的經驗來解釋所有內容。 第一名 (No.1 Never assume) The first…

linux下命令行的使用:使用sed命令操作文件

用該命令sed刪除文件test.txt中包含某個字符串abc的行: sed /adc/d test.txt >result.txt 在文件test.txt中刪除從開頭到含有某個字符串abc的行 sed 1,/abc/d test.txt >result.txt 獲取文件test.txt中包含字符串abc的行 cat test.txt |grep "abc" > resul…

ux的重要性_UX中清晰的重要性

ux的重要性重點 (Top highlight)Times, since the very first occurrences of web design in the 90’s, have changed a lot design-wise. The particular technology and its applications got more stable. Human-computer interaction (HCI) was deeply researched, design…

工欲善其事,必先利其器

vs2010中一些常用的快捷鍵、組合鍵&#xff1a; 1、快速格式化 CtrlED 2、注釋選中部分 CtrlEC 3、停止調試 ShiftF5 4、取消注釋選中部分 CtrlEU 5、顯示解決方案資源管理器 CtrlWS 6、快速折疊 CtrlMO 7、封裝一個字段 CtrlRE 8、查看屬性 CtrlWP 9…

可靠消息最終一致性設計_如何最終啟動您的設計產品組合

可靠消息最終一致性設計It’s not a secret that most designers procrastinate on their portfolios whether it is to update them or to create them in the first place.大多數設計師在更新產品組合時還是拖延產品組合并不是秘密。 首先創建它們 。 Hopefully, by the e…

臺式機共享筆記本的無線網絡(只需要一根網線)

以windowsXP系統為例 一、筆記本的設置方法&#xff1a; 1.先將筆記本的無線連接共享給有線網卡 將鼠標放到桌面的 網上鄰居 上&#xff0c;按鼠標右鍵&#xff0c;選屬性&#xff0c;再將鼠標放到無線連接上&#xff0c;又是按鼠標右鍵&#xff0c;選屬性&#xff0c;在彈出的…

游戲用戶體驗指標_電子游戲如何超越游戲化的用戶體驗

游戲用戶體驗指標游戲UX (GAMES UX) During a time when the time spent on video games has reached record breaking heights, due to excessive time indoors, gamification has more of a place now than ever before.d uring的時候花在視頻游戲的時間已經達到了 破紀錄的高…

JAVA編程心得-JAVA實現CRC-CCITT(XMODEM)算法

CRC即循環冗余校驗碼&#xff08;Cyclic Redundancy Check&#xff09;&#xff1a;是數據通信領域中最常用的一種差錯校驗碼&#xff0c;其特征是信息字段和校驗字段的長度可以任意選定。 1 byte checksum CRC-16 CRC-16 (Modbus) CRC-16 (Sick) …

什么字體字母和數字大小一樣_字母和字體如何適應我們的屏幕

什么字體字母和數字大小一樣Writing went through many iterations before it became what is today. Times New Roman wasn’t the default script for ancient Egyptians, in fact, paper didn’t even exist when the first words were written.寫作經歷了許多迭代&#xff…

jenkins 通過批處理自動構建 非標準項目

之前介紹了java和vs2010的項目構建&#xff0c;這些都是比較常見的&#xff0c;所以都用專門的工具。但但難免會遇到一些不常見的項目&#xff0c;下面介紹通過批處理進行構建&#xff0c;并用jenkins調用.我們這里使用plc語言&#xff0c;沒有標準環境&#xff0c;只有使用bat…

效果圖底圖 線框圖_5分鐘的線框圖教程

效果圖底圖 線框圖為什么使用線框&#xff1f; (Why wireframe?) Simply put, wireframes provide a structure and layout for content and assets.簡而言之&#xff0c;線框提供了內容和資產的結構和布局。 You can wireframe just about any kind of presentation, from p…

多線程 - 你知道線程棧嗎

問題 1. local 變量的壓棧和出棧過程 void func1(){ int a 0; int b 0; } 系統中有一個棧頂指針&#xff0c;每次分配和回收local 變量時&#xff0c;其實就是移動棧指針。 2. static local變量的分配風險 void func2(){ static int a 0; } 這個變量a可能會被分…

怎么讓qt發聲_第3部分:添加網絡字體-讓我們的單詞發聲

怎么讓qt發聲This is a big week for the project. While it was an important step last week to establish some basic responsiveness, we couldn’t really nail down the typography until we added the typeface. Too many aspects of the feel, proportions, and overal…

mysql語句中把string類型字段轉datetime類型

mysql語句中把string類型字段轉datetime類型在mysql里面利用str_to_date&#xff08;&#xff09;把字符串轉換為日期此處以表h_hotelcontext的Start_time和End_time字段為例&#xff0c;查詢當前時間在此范圍之內的數據。 www.2cto.com select * from h_hotelcontext where …

名詞解釋:對等知識互聯網_網站設計理論:比較和對等

名詞解釋:對等知識互聯網Equivalence and contrast, connection and distinction, categorization and non-categorization are all ways to distinguish the same or different elements. Based on the information they carry, we hope that the equivalent elements can hav…

hadoop深入研究:(五)——Archives

轉載請注明來源地址&#xff1a;http://blog.csdn.net/lastsweetop/article/details/9123155 簡介 我們在hadoop深入研究:(一)——hdfs介紹里已講過&#xff0c;hdfs并不擅長存儲小文件&#xff0c;因為每個文件最少一個block&#xff0c;每個block的元數據都會在namenode節點占…

人民幣小寫金額轉大寫金額

#region 人民幣小寫金額轉大寫金額/// <summary>/// 小寫金額轉大寫金額/// </summary>/// <param name"Money">接收需要轉換的小寫金額</param>/// <returns>返回大寫金額</returns>public static string ConvertMoney(Decimal…

饑餓的盛世讀后感_滿足任何設計師饑餓感的原型制作工具

饑餓的盛世讀后感Tell me if this story sounds familiar to you. You just wrapped up a design in Sketch -a design that took you hours, and now you want to bring it to life. Sketch’s built-in prototyping tool doesn’t allow you to create all the interactions …