復制代碼 代碼如下:
 
<? 
session_start(); 
for($i=0; $i<4; $i++){ 
$rand.= dechex(rand(1,15)); 
} 
$_SESSION[check_pic]=$rand; 
//echo $_SESSION[check_pic]; 
// 設置圖片大小 
$im = imagecreatetruecolor(100,30); 
// 設置顏色 
$bg=imagecolorallocate($im,0,0,0); 
$te=imagecolorallocate($im,255,255,255); 
// 把字符串寫在圖像左上角 
imagestring($im,rand(5,6),rand(25,30),5,$rand,$te); 
// 輸出圖像 
header("Content-type:image/jpeg"); 
imagejpeg($im); 
?> 
復制代碼 代碼如下:
 
<div> 
<fieldset><legend>留言</legend> 
<div> 
<form action="../utity/post.php" method="post" onsubmit="return chkinput(this)"> 
<input type="text" /> 您的名字 
<input type="text" /> 您的郵件 
<input type="text" /> 您的網站 
<textarea> 
</textarea><br /> 
<img src="checkcode.php"><input type="text"><br /> 
<input type="submit" value="提交" /> 
</form> 
</div> 
<br clear="both" /> 
</fieldset> 
復制代碼 代碼如下:
 
<?php 
session_start(); 
if(isset($_POST[check])) 
{ 
if($_POST[check] == $_SESSION[check_pic]) 
{ 
// echo "驗證碼正確".$_SESSION[check_pic]; 
require("dbinfo.php"); 
$name = $_POST['name']; 
$email = $_POST['email']; 
$website = $_POST['website']; 
$content = $_POST['content']; 
$date = date("Y-m-d h:m:s"); 
// 連接到 MySQL 服務器 
$connection = mysql_connect ($host, $username, $password); 
if (!$connection) 
{ 
die('Not connected : ' . mysql_error()); 
} 
// 設置活動的 MySQL 數據庫 
$db_selected = mysql_select_db($database, $connection); 
if (!$db_selected) 
{ 
die ('Can/'t use db : ' . mysql_error()); 
} 
// 向數據庫插入數據 
$query = "insert into table (nowamagic_name, nowamagic_email, nowamagic_website, nowamagic_content, nowamagic_date) values ('$name','$email','$website','$content','$date')"; 
$result = mysql_query($query); 
if($result) 
{ 
echo "<script>alert('提交成功'); history.go(-1);</script>"; 
} 
if (!$result) 
{ 
die('Invalid query: ' . mysql_error()); 
} 
} 
else 
{ 
echo "<script>alert('驗證碼錯誤'); history.go(-1);</script>"; 
} 
} 
?> 
新聞熱點
疑難解答