今天終於成功了!!一直無法解決的Captcha不能呈現的問題!!趕快將成紀錄,免得自己忘記。
sudo apt-get install apache2
sudo apt-get install mysql-server
sudo apt-get install php libapache2-mod-php php-mysql php-gd
sudo systemctl restart apache2
若不想安裝mysql ,可安裝 mariadb
sudo apt-get install mariadb-server
設定mariadb
$ sudo mysql -u root
> SELECT User, Host, plugin FROM mysql.user;
>CREATE USER 'webadmin'@'localhost' IDENTIFIED BY 'demo1234';
>GRANT ALL PRIVILEGES ON *.* TO 'webadmin'@'localhost';
>UPDATE mysql.user SET plugin='mysql_native_password' WHERE User='webadmin';
>FLUSH PRIVILEGES;
>exit;
>CREATE USER 'webadmin'@'localhost' IDENTIFIED BY 'demo1234';
>GRANT ALL PRIVILEGES ON *.* TO 'webadmin'@'localhost';
>UPDATE mysql.user SET plugin='mysql_native_password' WHERE User='webadmin';
>FLUSH PRIVILEGES;
>exit;
若要安裝編輯器,如Geany、Bluefish、Visual Studio Code。
指令如下:
$sudo apt-get install geany bluefish
$sudo apt install gnupg2 software-properties-common apt-transport-https wget
$wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add
$sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
$sudo apt update
$sudo apt install code
$wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add
$sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
$sudo apt update
$sudo apt install code
2.php 程式碼
檔案名稱:captcha_index.php
檔案內容:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php圖形驗證碼</title>
<script>
function refresh_code(){
document.getElementById("imgcode").src="captcha.php";
}
</script>
<form name="form1" method="post" action="./checkcode.php">
<p>請輸入下圖字樣:</p><p><img id="imgcode" src="captcha.php" onclick="refresh_code()" /><br />
點擊圖片可以更換驗證碼
</p>
<input type="text" name="checkword" size="10" maxlength="10" />
<input type="submit" name="Submit" value="送出" />
</form>
檔案名稱:captcha.php
檔案內容:
<?php
if(!isset($_SESSION)){ session_start(); } //檢查SESSION是否啟動
$_SESSION['check_word'] = ''; //設置存放檢查碼的SESSION
//設置定義為圖片
header("Content-type: image/PNG");
/*
imgcode($nums,$width,$high)
設置產生驗證碼圖示的參數
$nums 生成驗證碼個數
$width 圖片寬
$high 圖片高
*/
imgcode(5,120,30);
//imgcode的function
function imgcode($nums,$width,$high) {
//去除了數字0和1 字母小寫O和L,為了避免辨識不清楚
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMOPQRSTUBWXYZ";
$code = '';
for ($i = 0; $i < $nums; $i++) {
$code .= $str[mt_rand(0, strlen($str)-1)];
}
$_SESSION['check_word'] = $code;
//建立圖示,設置寬度及高度與顏色等等條件
$image = imagecreate($width, $high);
$black = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
$border_color = imagecolorallocate($image, 21, 106, 235);
$background_color = imagecolorallocate($image, 235, 236, 237);
//建立圖示背景
imagefilledrectangle($image, 0, 0, $width, $high, $background_color);
//建立圖示邊框
imagerectangle($image, 0, 0, $width-1, $high-1, $border_color);
//在圖示布上隨機產生大量躁點
for ($i = 0; $i < 80; $i++) {
imagesetpixel($image, rand(0, $width), rand(0, $high), $black);
}
$strx = rand(3, 8);
for ($i = 0; $i < $nums; $i++) {
$strpos = rand(1, 6);
imagestring($image, 5, $strx, $strpos, substr($code, $i, 1), $black);
$strx += rand(10, 30);
}
imagepng($image);
imagedestroy($image);
}
?>
檔案名稱:checkcode.php
檔案內容:
<?php
if(!isset($_SESSION)){
session_start();
} //判斷session是否已啟動
if((!empty($_SESSION['check_word'])) && (!empty($_POST['checkword']))){
//判斷此兩個變數是否為空
if($_SESSION['check_word'] == $_POST['checkword']){
$_SESSION['check_word'] = '';
//比對正確後,清空將check_word值
header('content-Type: text/html; charset=utf-8');
echo '<p> </p><p> </p><a href="./chptcha_index.php">OK輸入正確,將於一秒後跳轉(按此也可返回)</a>';
echo '<meta http-equiv="refresh" content="1; url=./captcha_index.php">';
exit();
}else{
echo '<p> </p><p> </p><a href="./chptcha_index.php">Error輸入錯誤,將於一秒後跳轉(按此也可返回)</a>';
echo '<meta http-equiv="refresh" content="1; url=./captcha_index.php">';
}
}
?>
資料來源:
2. php gd 模組安裝
沒有留言:
張貼留言