Long Live The King
ข้าพเจ้าไม่ได้มีพรสวรรค์พิเศษอะไร ข้าพเจ้าเพียงแต่มีความกระหายใคร่รู้อยู่เสมอ ทุ่มเทให้กับสิ่งที่อยากรู้ พากเพียรอย่างทรหด และสำรวจวิจารณ์ความรู้ของตัวเองเป็นประจำ ปัจจัยเหล่านี้คือที่มาของแนวคิดต่างๆ ของข้าพเจ้า .... อัลเบิร์ต ไอน์สไตน์

วันอาทิตย์ที่ 9 ธันวาคม พ.ศ. 2555

Register ลงทะเบียน สมัครสมาชิก php

SQL สร้างตารางฐานข้อมูลสำหรับทดสอบ
CREATE TABLE `myweb`.`members` (
  `member_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL DEFAULT '',
  `password` varchar(255) NOT NULL DEFAULT '',
  `emailaddress` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`member_id`)
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;

ไฟล์ config.php
<?php
$host = "localhost";
$us = "root";
$pw = "123";
$db = "myweb";
 
$link = mysql_connect($host,$us,$pw)or die ("Could not connect to MySQL");
mysql_select_db($db)or die ("Could not connect to Database");
?>


ไฟล์ register.php
<?php session_start(); ?>
 
 
 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link type="text/css" rel="stylesheet" href="style.css">
 
 
 
<div class="content">
<form name="myform" method="post" action="register_sql.php">
 
<fieldset style="border:2px solid goldenrod;">
 
<legend><font color="#12768E"><b>Form Register:</b></font></legend>
<div id="register">
<div>
<label for="username">Username:</label>
<input name="username" id="user" type="text">
<font color="red">*</font>
</div>  
<div>
<label for="password">Password:</label>
<input name="password" id="pass" type="password">
<font color="red">*</font>
</div>
<div>
<label for="password_cf">Password confirm:</label>
<input name="password_cf" id="pcf" type="password">
<font color="red">*</font>
</div>
<div>
<label for="email">Emailaddress:</label>
<input name="emailaddress" id="email" type="text">
<font color="red">*</font>
</div>
<div>
<input id="submit" value="Submit" name="submit"
 style="margin-left:300px;background:#3B59A8;border:1px solid #000;
color:#ffffff;font-weight:bold;" type="submit">
</div>
</div>
<div><?=$empty_error;?></div>
<div><?=$confirm;?></div>
</fieldset>
 
</form>
</div>

ไฟล์ register_sql.php
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register</title>
<link type="text/css" rel="stylesheet" href="style.css">
 
 
<div class="content">
<fieldset style="border:2px solid goldenrod;">
<legend><font color="#12768E"><b>Form Register:</b></font></legend>
<?php
//function ตรวจสอบอีเมล์ จากเว็บ ninenik.com ขอบคุณครับ.
function check_email($email){
list($email_user,$email_host)=explode("@",$email);
$host_ip=gethostbyname($email_host);
if(eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]
           +(.[a-z0-9-]+)*(.[a-z]{2,3})$"
, $email) && !ereg($host_ip,$email_host))
{
return true;
}
else
{
return false;
}
}
?>
 
<?php
include 'config.php'; //เรียกใช้ไฟล์ config
$username = $_POST['username'];
$password = $_POST['password'];
$password_cf = $_POST['password_cf'];
$emailaddress = $_POST['emailaddress'];
 
if($username=="" || $password==""
  || $password_cf=="" || $emailaddress=="")
{
$empty_error="<center><font color=red>กรุณากรอกข้อมูลให้ครบ</font>";
echo $empty_error;
echo "  <a href='register.php'>
<input type='button'value='Back'style='background:#3B59A8;
border:1px solid #000;color:#ffffff;font-weight:bold;'/></a>
</center>";
exit();
}
else if($password != $password_cf)
{
$confirm = "<center><font color=red>การยืนยัน password ไม่ตรงกัน</font>";
echo $confirm;
echo "  <a href='register.php'>
<input type='button'value='Back'style='background:#3B59A8;
border:1px solid #000;color:#ffffff;font-weight:bold;'/></a>
</center>";
exit();
}
else
{
if(check_email($emailaddress)==false)//ใช้งาน function ตรวจสอบอีเมล์
{
$email_error = "<center><font color=red>
                อีเมล์นี้ไม่มีอยู่จริง กรุณากรอกใหม่</font>";
echo $email_error;
echo "  <a href='register.php'>
<input type='button'value='Back'style='background:#3B59A8;
border:1px solid #000;color:#ffffff;font-weight:bold;'/></a>
</center>";
exit();
}
//เมื่อทุกอย่างผ่านหมดแล้ว เช็คข้อมูลซ้ำจากฐานข้อมูล
$sql = "select * from members where username='$username'
|| emailaddress='$emailaddress'";
$result = mysql_query($sql,$link);
$num = mysql_num_rows($result);
if($num>0)
{
$was_used = "<center><font color=red>username หรือ
             emailaddress นี้ได้ถูกใช้ไปแล้ว</font>";
echo $was_used;
echo "  <a href='register.php'>
<input type='button'value='Back'style='background:#3B59A8;
border:1px solid #000;color:#ffffff;font-weight:bold;'/></a>
</center>";
exit();
}
else
{
$sql = "insert into members (username,password,emailaddress)
        values ('$username','$password','$emailaddress')";
$result = mysql_query($sql,$link);
//ตรงส่วนนี้อาจจะเป็นการส่งอีเมล์เพื่อให้ user ทำการยืนยันก็ได้
echo "<center><font color=blue>Success</font>
        กลับไป Login";
echo "  <a href='login.php'>
<input type='button'value='Login'style='background:#3B59A8;
border:1px solid #000;color:#ffffff;font-weight:bold;'/></a>
</center>";
mysql_close();
}
}
?>
</fieldset>
</div>


จบแล้วสำหรับบทความนี้....

1 ความคิดเห็น:

© Bookneo, AllRightsReserved.

ขับเคลื่อนโดย Blogger Designed by Nikhorn Thongchuay