dongzhanlu0658 2010-11-22 10:42
浏览 32
已采纳

PHP - 重复代码或双重用户身份验证功能

All,

I am building a small, non-commercial consumer website. During the user registry, I want to check whether their chosen user name already exist in my user db (MySQL).

Currently, I re-use the function below, which is mostly used to retrieve a user, based on the user name, during the login process:

function retrieveUserByUserName($userName){
 $dbConnection=$this->dbInstance->createConnexion();
 $query=$dbConnection->prepare("SELECT * FROM users WHERE userName= :userName");
 $query->bindParam(":userName", $userName);
 $query->execute();
 $result=$query->fetchObject('userName');
 if ($result){
  return $result;
 } else {
  return false;
 }
}

This is slightly overengineered just to test whether the user name exists, but my main concern is that it returns the full user object, which might be unsafe and is certainly somewhat wasteful. My alternative would be to create a dedicated function:

function checkUserName($userName){
  $dbConnection=$this->dbInstance->createConnexion();
  $query=$dbConnection->prepare("SELECT * FROM users WHERE userName= :userName");
  $query->bindParam(":userName", $userName);
  $result=$query->execute();
  return $result;
}

This makes the code slightly heavier.

So my question to the SO community is: what is the right tradeoff? Duplicate code or use a function for a secondary purpose? Is there a good rule of thumb for that kind of question, or does it depend?

Many thanks,

JDelage

PS: I'm pretty new at all that, sorry for the very basic question.

PPS: I'm now wondering whether the 2nd query would ever do what I want. If there are no records in the db with those parameters, the query still executes and the value of $result is always true, right?

  • 写回答

3条回答 默认 最新

  • duanjiang7505 2010-11-22 10:57
    关注

    It completely depends. I think this is about taste. Programming is all about keeping overview of what you are doing and what you have done. As long as you don't create 20 functions with the same purpose you will be fine.

    Most of the time I use a seperate AJAX-request to validate my username when the inputfield loses focus. In that request I use a seperate function which only checks if the username exists in the database.

    public static function isUsernameValid($userName){
      $dbConnection=$this->dbInstance->createConnexion();
      $query=$dbConnection->prepare("SELECT count(*) AS total FROM users WHERE userName= :userName");
      $query->bindParam(":userName", $userName);
      $result=$query->execute();
    
      if($query->fetchColumn() == 0) {
           return true;
      } else {
           return false;
      }
    }
    

    In this case you don't retrieve any information about the user itself and don't have to fetch the object. This same function can, and shoud, ofcourse also be used to check once the form is submitted.

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输