Login ในหน้าเดียว

วันก่อน ได้เขียน สคริป login โดยเอาสคริปมาจาก http://www.goragod.com/

เอามาดัดแปลง อีกหน่อยนึง เพื่อที่จะให้เป็นอย่างที่ผมต้องการได้

——————————–

<?  
 $action=$_POST[action];
 $user=$_POST[user];
  $passwd=$_POST[passwd];
   
if (isset($user) && isset($passwd) && $action==”login”)//ตรวจสอบชื่อกับฐานข้อมูล
{
    $host=”localhost”;
          $username=”root”;
          $password=”1234″;
          $dbname=”batt2home”;
          $tablename=”tb_admin”;
         
           //เรียกข้อมูล – Member -
          $db = mysql_connect($host,$username,$password) or die (”ไม่สามารถติดต่อกับฐานข้อมูลได้ในขณะนี้”);
          $sql = “select * from $tablename where admin = ‘$user’ AND adminpass = ‘$passwd’”;
          $query = mysql_db_query($dbname,$sql) or die (”ไม่สามารถเรียกฐานข้อมูลสมาชิกได้ในขณะนี้”);
          $fetch = mysql_fetch_array($query);
          $num = mysql_num_rows($query);
          mysql_close($db);  
 
 if ($num>=1) ////login สำเร็จ  ให้set cookie หรือจะใช้ session ก็ได้
 {
 setcookie(”action”,”login”);
 setcookie(”user”,”$user”,time()+3600);
 setcookie(”passwd”,”$passwd”,time()+3600);
 }
  
       else {
                //ชื่อหรือ รหัสผ่านไม่ถูกต้อง
               $message=”<font color=red size=1>ชื่อ หรือ รหัสผ่าน ไม่ถูกต้อง</font>”;
               unset($user);
          }
     }
 
else if (isset($action) && $action==’logout’) //ออกจากระบบ
{
 
   setcookie(”user”,”");
   setcookie(”passwd”,”");
 $message=”<font color=green >ออกจากระบบเรียบร้อย</font><br />”;
}
<html> 
<body>

 echo”
                    <table cellspacing=5 cellpadding=0 width=200 bgcolor=#CCCCCC>”; ?>
                    <form method=post action=”" name=login_form onSubmit=”return check_login()”>
                    <?
     if (isset($user) && $action==”login”)// แสดงข้อความต้อนรับ
     {
    echo”<tr><td colspan=2 align=center><font color=#666666 >ยินดีต้อนรับ คุณ <font color=green>$user </font> เข้าระบบ</font></td></tr>”;
                echo”<tr><td align=right colspan=2><input type=submit name=action  value=logout class=red title=\”สมาชิก เข้าระบบ\”></td></tr>”;
     }
       else// แสดงฟอร์ม login
       {
                     if (isset($message))
     
      echo”<tr><td colspan=2 align=center>$message</font></td></tr>”;
     
      else
     
    echo”<tr><td colspan=2 align=center><font  color=#666666>กรุณาเข้าระบบ</font></td></tr>”;
                echo”<tr><td align=right><font color=#555555 >user : </font></td><td><input size=15 type=text name=user maxlength=15 class=red></td></tr>”;
                echo”<tr><td align=right><font color=#555555 >passwd : </font></td><td><input size=15 type=password name=passwd maxlength=15  class=red></td></tr>”;
                    echo”<tr><td align=right colspan=2><input type=submit name=action  value=login class=red title=\”สมาชิก เข้าระบบ\”></td></tr>”;
     
     }?>
                    </form>              
                   <? echo” </table>”; //End login ?>
 ?>

</body>
</html> 

<script language=JavaScript>
function check_login()
{
     var v1 = document.login_form.user.value;
     var v2 = document.login_form.passwd.value;

     if (v1.length==0) {
          alert(’กรุณาป้อน Username ก่อน’);
          document.login_form.user.focus();          
          return false;
     } else if (v2.length==0) {
           alert(’กรุณาป้อน Password ก่อน’) ;
           document.login_form.passwd.focus();          
           return false;
     } else
          return true;
}
</script>

Tags:

2 Responses to “Login ในหน้าเดียว”

  1. คนรักการเขียนโปรแกรม Says:

    อ่าาา มีคนกล่าวไว้ว่า ผมเจอคนชอบเขียนโปรแกรมต้องรีบทัก ^_^ ชอบเขียนโปรแกรมเหมือนกันครับ มาเที่ยวบล็อกผมได้นะ

  2. wow gold Says:

    This is a great post and makes me think of where I can fit in. I do a little bit of everything mentioned here and I guess I have to find my competitive advantage.

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word