<?php
//This function will display the registration form
function register_form(){
echo "ระบบสมัครสมาชิก :: ";
$date = date('D'M,Y');
echo "<form action='?act=register' method='post'>"
."Username <input type='text' name='username' ><br>"
."Password <input type='password' name='password'><br>"
."ชื่อ-สกุล <input type='text' name='name'><br>"
."อีเมล์ <input type='text' name='email'><br>"
."ที่อยู่ <textarea name='address' cols=30 row=4></textarea><br>"
."วันเกิด <input type='text' name='date'><br>"
."เบอร์โทรศัพท์ <input type='text' name='tel'><br>"
."<input type='hidden' name='date' value='$date'><br>
<input type='submit' value='สมัครสมาชิก'>
<input type='reset' value='กรอกข้อมูลใหม่'>
</form>";
}
//This function will register users data
function register(){
//Connecting to database
$connect = mysql_connect("localhost", "root", "");
if(!$connect){
die(mysql_error());
}
//Selecting database
$select_db = mysql_select_db("mydatabase", $connect);
if(!$select_db){
die(mysql_error());
}
//Collecting info
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$address = $_REQUEST['address'];
$date = $_REQUEST['date'];
$tel = $_REQUEST['tel'];
//Here we will check do we have all inputs filled
if(empty($username)){
die("Please enter your username!<br>");
}
if(empty($password)){
die("Please enter your password!<br>");
}
if(empty($name)){
die("Please enter your name!<br>");
}
if(empty($email)){
die("Please enter your email!");
}
if(empty($address)){
die("Please enter your address!<br>");
}
if(empty($tel)){
die("Please enter your telephone!<br>");
}
//Let's check if this username is already in use
$user_check = mysql_query("SELECT username FROM member WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);
//Now if email is already in use
$email_check = mysql_query("SELECT email FROM member WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);
//Now display errors
if($do_user_check > 0){
die("Username is already in use!<br>");
}
if($do_email_check > 0){
die("Email is already in use!");
}
//If everything is okay let's register this user
$insert = mysql_query("INSERT INTO member (username, password,name,email,address,date,tel) VALUES ('$username', '$password','$name','$email','$address','$date','$tel')");
if(!$insert){
die("There's little problem: ".mysql_error());
}
echo $username.", you are now registered. Thank you!<br><a href=login.php>Login</a> | <a href=index.php>Index</a>";
}
switch($act){
default;
register_form();
break;
case "register";
register();
break;
}
?>
หน้า login.php
<?php
$link=mysql_connect($dbserver, $dbuser, $dbpass) or die("ติดต่อฐานข้อมูลไม่ได้");
mysql_select_db($dbname) or die("ติดต่อฐานข้อมูลในตารางไม่ได้");
mysql_query("set NAMES tis620 ");
error_reporting(E_ALL ^ E_NOTICE);
$dbhost = "localhost";
$dbname = "test";
$dbuser = "root";
$dbpass = "root";
mysql_select_db("$dbname",$link);
$ckus="select * from tb_user where user='$user'";
$resultus=mysql_query($ckus,$link);
$numus=mysql_num_rows($resultus);
$rowus=mysql_fetch_row($resultus);
if($num != 0) /*ตรวจสอบจำนวนแถว ว่า ในแถวมีชื่อ อยู่หรือไม่*/
{
echo"<br>ชื่อนี้มีผู้ใช้งานแล้ว ";
}
else
{
$add ="INSERT INTO tb_user (user,passwd,name,lastname,address) values('$user_id','$password','$first_name','$sur_name','$address_user')";
mysql_query($add,$link) or die ("ขาดการติดต่อกับฐานข้อมูล กรุณา รอเพื่อปรับปรุ่งระบบ ");
echo "บันทึกข้อมูลเสร็จเรียบร้อยแล้ว ";
}
?>
โค๊ต capcha
<?php
require_once('recaptchalib.php');
// Get a key from http://recaptcha.net/api/getkey
$publickey = "";
$privatekey = "";
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
# was there a reCAPTCHA response?
echo recaptcha_get_html($publickey, $error);
?>