會員登入的處理 (教學示範)

建立 html 的登入表
<form>
<fieldset><legend>會員登入</legend>
<ol>
<li><label for="email">帳號 (email) :</label><input id="email" name="email" type="email" maxlength="120" placeholder="thanks@lot.freinds"></li>
<li><label for="pwd">密碼:</label><input id="pwd" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><button type="submit">登入</button></li></ol>
</fieldset>
</form>
<p><a href="register.php">申請新會員 (註冊)</a></p>

善用 html 5 填表元素新屬性

<form>
<fieldset><legend>會員登入</legend>
<ol>
<li><label for="email">帳號 (email) :</label><input id="email" name="email" type="email" maxlength="120" placeholder="thanks@lot.freinds" title="有效的電子信箱,如 thanks@lots.friends" required autofocus></li>
<li><label for="pwd">密碼:</label><input id="pwd" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合" title="密碼:大小寫英文字母及數字混合,6~18字,如 A12Rd6" minlength="6" required pattern="(?=^[A-Za-z0-9]{6,18}$)((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]))^.*$"></li>
<li><button type="submit">登入</button></li></ol>
</fieldset>
</form>
<p><a href="register.php">申請新會員 (註冊)</a></p>
建立 SQL 資料表

①district_tw, ②status_affect, ③ac_basic

創建 PDO 物件連接資料庫
<?php
require_once 'connections/cn-prime.php';
?>
檢驗帳號密碼有否相符
<?php
require_once 'connections/cn-prime.php';
$tb_ac='ac_basic';
if(filter_has_var(INPUT_POST,'logging')){
 if(!empty($_POST['email'])&&!empty($_POST['password'])){
  $sql1="SELECT email, password, nick, count(*) AS matched, id FROM $tb_ac WHERE email=:email AND password=:password";
  $stmt1=$prime->prepare($sql1);
  $stmt1->bindParam(':email',$_POST['email']);
  $stmt1->bindParam(':password',$_POST['password']);
  if($stmt1->execute()){
   $row=$stmt1->fetch(PDO::FETCH_OBJ);
  }
 }
}
?>
<?php
if(isset($row->matched)){
 if($row->matched){
  echo '<p>',$row->nick?$row->nick:'隱名埋姓',',您好!</p>';
 }
 else{
  echo '<p>帳號密碼錯誤。</p>';	
 }
}
?>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<fieldset><legend>會員登入</legend>
<ol>
<li><label for="email">帳號 (email) :</label><input id="email" name="email" type="email" maxlength="120" placeholder="thanks@lot.freinds"></li>
<li><label for="pwd">密碼:</label><input id="pwd" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><button type="submit" name="logging" value="1">登入</button></li></ol>
</fieldset>
</form>
<p><a href="register.php">申請新會員 (註冊)</a></p>
啟動 Session 確保登入資料
<?php
require_once 'connections/cn-prime.php';
$tb_ac='ac_basic';
session_start();
if(filter_has_var(INPUT_POST,'logging')){
 if(!empty($_POST['email'])&&!empty($_POST['password'])){
  $sql1="SELECT email, password, nick, count(*) AS matched, id FROM $tb_ac WHERE email=:email AND password=:password";
  $stmt1=$prime->prepare($sql1);
  $stmt1->bindParam(':email',$_POST['email']);
  $stmt1->bindParam(':password',$_POST['password']);
  if($stmt1->execute()){
   $row=$stmt1->fetch(PDO::FETCH_OBJ);
   if($row->matched){
    session_regenerate_id(true);
    $_SESSION['logAccount']=$row->email;
    $_SESSION['logNickname']=$row->nick?$row->nick:'隱名埋姓';
    $_SESSION['logid']=$row->id;
   }
  }
 }
}
?>
<?php
if(!empty($_SESSION['logAccount'])){
 echo "<p>$_SESSION[logNickname],您好!</p>";
}
else{
?>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<fieldset><legend>會員登入</legend>
<ol>
<li><label for="email">帳號 (email) :</label><input id="email" name="email" type="email" maxlength="120" placeholder="thanks@lot.freinds"></li>
<li><label for="pwd">密碼:</label><input id="pwd" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><button type="submit" name="logging" value="1">登入</button></li></ol>
</fieldset>
</form>
<p><a href="register.php">申請新會員 (註冊)</a></p>
<?php } ?>
登入失敗的處理
<?php
/**
 * v.0.1.0 latest:2013/2/25
 * ©webchain(不惑仔), all rights reserved. Free for commercial and personal use.
 */
class iri{
public static function absoluteHere($scheme='http'){
 return $scheme.'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
}
}
?>
<?php
function autoloadClass($class){require_once "classes/$class.php";}spl_autoload_register('autoloadClass');
if(!filter_has_var(INPUT_COOKIE,'pathToAuth')){
 $pathRef=iri::absoluteHere();
 setcookie('pathToAuth',$pathRef);
}
require_once 'connections/cn-prime.php';
$tb_ac='ac_basic';
session_start();
if(filter_has_var(INPUT_POST,'logging')){
 if(!empty($_POST['email'])&&!empty($_POST['password'])){
  $sql1="SELECT email, password, nick, count(*) AS matched, id FROM $tb_ac WHERE email=:email AND password=:password";
  $stmt1=$prime->prepare($sql1);
  $stmt1->bindParam(':email',$_POST['email']);
  $stmt1->bindParam(':password',$_POST['password']);
  if($stmt1->execute()){
   $row=$stmt1->fetch(PDO::FETCH_OBJ);
   if($row->matched){
    session_regenerate_id(true);
    $_SESSION['logAccount']=$row->email;
    $_SESSION['logNickname']=$row->nick?$row->nick:'隱名埋姓';
    $_SESSION['logid']=$row->id;
    $pathRef=$_COOKIE['pathToAuth'];
    setcookie('pathToAuth','',time()-3600);
    header("Location:$pathRef".'?auth=1');
   }
   else{
    if(!empty($_SESSION['logAccount'])){
     unset($_SESSION['logAccount'],$_SESSION['logNickname'],$_SESSION['logid']);
    }
    header("Location:".iri::absoluteHere().'?invalidlog=1');
   }
  }
 }
}
?>
<?php
if(filter_has_var(INPUT_GET,'invalidlog')){
 echo '<p>帳號密碼錯誤。</p>';
}
if(!empty($_SESSION['logAccount'])){
 echo "<p>$_SESSION[logNickname],您好!</p>";
}
else{
?>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<fieldset><legend>會員登入</legend>
<ol>
<li><label for="email">帳號 (email) :</label><input id="email" name="email" type="email" maxlength="120" placeholder="thanks@lot.freinds"></li>
<li><label for="pwd">密碼:</label><input id="pwd" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><button type="submit" name="logging" value="1">登入</button></li></ol>
</fieldset>
</form>
<p><a href="register.php">申請新會員 (註冊)</a></p>
<?php } ?>
增加登出的選項
<?php
/**
 * v.0.1.0 latest:2013/2/10
 * ©webchain(不惑仔), all rights reserved. Free for commercial and personal use.
 */
class xsession{
public static function discard(){
 $_SESSION=[];
 if(ini_get("session.use_cookies")){
  $params=session_get_cookie_params();
  setcookie(session_name(),'',time()-3600,$params["path"],$params["domain"],$params["secure"],$params["httponly"]);
 }
 session_destroy();
}
}
?>
<?php
function autoloadClass($class){require_once "classes/$class.php";}spl_autoload_register('autoloadClass');
if(!filter_has_var(INPUT_COOKIE,'pathToAuth')){
 $pathRef=iri::absoluteHere();
 setcookie('pathToAuth',$pathRef);
}
require_once 'connections/cn-prime.php';
$tb_ac='ac_basic';
session_start();
if(filter_has_var(INPUT_POST,'logging')){
 if(!empty($_POST['email'])&&!empty($_POST['password'])){
  $sql1="SELECT email, password, nick, count(*) AS matched, id FROM $tb_ac WHERE email=:email AND password=:password";
  $stmt1=$prime->prepare($sql1);
  $stmt1->bindParam(':email',$_POST['email']);
  $stmt1->bindParam(':password',$_POST['password']);
  if($stmt1->execute()){
   $row=$stmt1->fetch(PDO::FETCH_OBJ);
   if($row->matched){
    session_regenerate_id(true);
    $_SESSION['logAccount']=$row->email;
    $_SESSION['logNickname']=$row->nick?$row->nick:'隱名埋姓';
    $_SESSION['logid']=$row->id;
    $pathRef=$_COOKIE['pathToAuth'];
    setcookie('pathToAuth','',time()-3600);
    header("Location:$pathRef".'?auth=1');
   }
   else{
    if(!empty($_SESSION['logAccount'])){
     unset($_SESSION['logAccount'],$_SESSION['logNickname']);
    }
    header("Location:".iri::absoluteHere().'?invalidlog=1');
   }
  }
 }
}
if(filter_has_var(INPUT_GET,'loggingout')){
 xsession::discard();
 header("Location:".iri::absoluteHere());
}
?>
<?php
if(filter_has_var(INPUT_GET,'invalidlog')){
 echo '<p>帳號密碼錯誤。</p>';
}
if(!empty($_SESSION['logAccount'])){
 echo "<p>$_SESSION[logNickname],您好!</p>",'<p><a href="?loggingout=1">登出</a></p>';
}
else{
?>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<fieldset><legend>會員登入</legend>
<ol>
<li><label for="email">帳號 (email) :</label><input id="email" name="email" type="email" maxlength="120" placeholder="thanks@lot.freinds"></li>
<li><label for="pwd">密碼:</label><input id="pwd" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><button type="submit" name="logging" value="1">登入</button></li></ol>
</fieldset>
</form>
<p><a href="register.php">申請新會員 (註冊)</a></p>
<?php } ?>
增加資料查看的選項
<?php
if(filter_has_var(INPUT_GET,'invalidlog')){
 echo '<p>帳號密碼錯誤。</p>';
}
if(!empty($_SESSION['logAccount'])){
 echo "<p>$_SESSION[logNickname],您好!</p>",'<ul><li><a href="?loggingout=1">登出</a></li><li><a href="modify.php">查看資料</a></li></ul>';
}
else{
?>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<fieldset><legend>會員登入</legend>
<ol>
<li><label for="email">帳號 (email) :</label><input id="email" name="email" type="email" maxlength="120" placeholder="thanks@lot.freinds"></li>
<li><label for="pwd">密碼:</label><input id="pwd" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><button type="submit" name="logging" value="1">登入</button></li></ol>
</fieldset>
</form>
<p><a href="register.php">申請新會員 (註冊)</a></p>
<?php } ?>
參考資源

更新日期:

google 論壇

App javascript (groups.google.com/group/app-javascript/)