<?php
//啟動session會話
session_start();
header('content-type:text/html;charset=utf-8');
//接收表單數據
$username=trim($_POST['username']);
$password=md5(trim($_POST['password'])); //密碼MD5加密
$password_confirm=md5(trim($_POST['password']));
$code=$_POST['code'];
if($username==''||$password==''||$password_confirm==''){
echo "<script>alert('請確定資訊的完整性!');history.back();</script>";
}elseif($code!=$_SESSION['var_code']){
echo "<script>alert('驗證碼不對喔!');history.back();</script>";
}else{
if($password==$password_confirm){
//2.連接MYSQL,選擇資料庫
$link = mysql_connect('localhost','root','root') or die("資料庫連線失敗!");
mysql_query('set names utf8');
mysql_query('use `demodb`');
//執行sql
$sql="select * from `manager` where `username`='$username'";
$res=mysql_query($link,$sql);
$nums=mysql_num_rows($res);
if($nums){ //存在
echo "<script> alert('使用者名稱已存在,請換個名稱!');history.back();</script>";
exit;
}else{
$sql_insert="insert into manager values('$username','$password')";
$res_insert=mysql_query($link,$sql_insert);
if($res_insert){
echo "<script>alert('註冊成功!');history.back();</script>";
}else{
echo "<script>alert('註冊失敗!');history.back();</script>";
}
}
}
else{
echo "<script>alert('密碼不一致!');history.back();</script>";
}
}
?>