Title1

Title2

Title3

6-3-3-3 20171025上課範例

  1. function.php
    
    ###############################
    # 檢查users=> email 是否重覆
    # Email存在 傳回 false
    ###############################
    function checkEmail($email){
      global $db;
     
      $valid = true;
     
      $sql= "select uid
             from `users`
             where `email`='{$email}'
      ";
      $result = $db->query($sql);
      if($result){
        list($uid) = $result->fetch_row();
        if($uid){
          $valid = false;
        }
      }
      return $valid;
    }

     

  2. index.php signup()
    
    function signup(){
    	global $db;
      //print_r($_POST);die("");
      //Array ( [name] => asdfas [email] => 1@gmail.com [pass] => asdf [confirmPass] => asdfasdfasdf [op] => signup ) 
      #過濾
      $_POST['name'] = db_CleanVars($_POST['name'], "姓名");
      $_POST['email'] = db_CleanVars($_POST['email'], "email", FILTER_VALIDATE_EMAIL);
      $_POST['pass'] = db_CleanVars($_POST['pass'], "密碼");
      $_POST['confirmPass'] = db_CleanVars($_POST['confirmPass'], "確認密碼");
    
      if($_POST['pass'] != $_POST['confirmPass'] ){
        redirect_header(WEB_URL, 3000,  '密碼不對!');
      }
    
      checkEmail($_POST['email']) or redirect_header(WEB_URL, 3000, "email重複");
       #密碼加密
      $_POST['pass']  = password_hash($_POST['pass'], PASSWORD_DEFAULT);
     
      #寫進資料庫
      $sql = "insert into `users`
            (`email`,`pass`,`name`) values
            ('{$_POST['email']}','{$_POST['pass']}','{$_POST['name']}')"; //die($sql);
      
      $db->query($sql) or  redirect_header(WEB_URL, 3000, "(".$db->errno.")".$sql);
      // if(!$db->query($sql)){
      //   redirect_header(WEB_URL, 3000, "(".$db->errno.")");
      // }
      return "註冊成功!!";
    
    }