doukezi4606 2016-05-05 09:23
浏览 62
已采纳

oops在php中登录系统

i am developing a login system in php with oops. I am not able to pass the connection to the query so the query is not processing.

below is the files : config.php

<?php

define("DB_HOST",'localhost');
define("DB_USER",'root');
define("DB_PASSWORD",'');
define("DB_DATABASE",'admin_safeplace');


?>

dbConnect.php--->for database connection

<?php

class dbConnect
{

    public $conn;
    function __construct()
    {

      //Initialize connection to database
      require_once('config.php');
      $this->conn=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
      if(!$this->conn)
      {
        die('Cannot connect to the database');    

      }
      return $this->conn;

    }



}

?>

dbFunction.php--->For various function

<?php
require_once('dbConnect.php');
session_start();
class dbFunction 
{

    function __construct()
    {
       //Connecting to the database
       $db=new dbConnect();      


    }
    function login($email,$password) //Login Function
    {

       $pass=md5($password);
       $sql="Select * from `super_user` where `email`='$email' AND `password`='$pass'";
       $res=mysqli_query($db->conn,$sql);
       $user_data = mysqli_fetch_array($res);
       $rowCount=mysqli_num_rows($res);
       if($rowCount==1)
       {

          $_SESSION['login']=true;
          $_SESSION['email']=$user_data['email'];
          return TRUE;


       }

       else

       {

         return FALSE;

       }


    }



}



?>

login page code:

<!--Login Code-->
<?php

$funObj = new dbFunction(); 
    if(isset($_POST['submit']))
    {
        $email = $_POST['email'];  
        $password = $_POST['password'];  
        $user = $funObj->login($email, $password);  
        if ($user) 
        {  
            // Login Success  
           header("location:list_users.php");  
        } 
       else 
        {  
            // Login Failed  
            echo "<script>alert('Emailid / Password Not Match')</script>";  
        }  


    }
?> 

How can i pass the $conn i.e connection to the query in login() function.can anyone help ?

  • 写回答

1条回答 默认 最新

  • douyi8760 2016-05-05 12:19
    关注

    If you are start using OOPS, Its good to use the PDO library to connect the database and grab the fully featured SQL database concepts such as prepared statements, stored procs, transactions etc.

    1. Make a config.php as you created but change as like following

      <?php
          define("DB_HOST",'localhost');
          define("DB_USER",'root');
          define("DB_PASSWORD",'');
          define("DB_DATABASE",'admin_safeplace');
          try {
              $pdo = new PDO('mysql:host='. DB_HOST .';dbname='.DB_DATABASE, DB_USER, DB_PASSWORD);
          } catch (PDOException $e) {
              exit('Error Connecting To DataBase');
          }
      ?>
      
    2. Create dbfunction.php as follwing code of snippet.

      <?php
          require_once('config.php');
      
          class dbFunction {
              function __construct($pdo) {
                  $this->pdo = $pdo;
              }
      
              function login($email,$password) { //Login Function
                  $pass=md5($password);
                  $sql="Select * from `super_user` where `email`='$email' AND `password`='$pass'";
                  $res=$this->pdo->prepare($sql);
                  $query->execute();
                  $query->fetchAll();
                  $rowcount =  $query->rowCount();
      
                  if($rowCount==1) {
                      $_SESSION['login']=true;
                      $_SESSION['email']=$user_data['email'];
      
                      return TRUE;
                  }  else {
                      return FALSE;
                  }
              }
          }
      ?>
      
    3. Create your loginscreen.php with HTML and their php code would be to check the login credential as following.

      <?php
          $db = new dbFunction($pdo);
          // Get the form data as in your case email and password
          $email = $_POST['email'];  
          $password = $_POST['password'];  
          $user = $db->login($email, $password);
      
          if($user) { // Login Success  
              header("location:list_users.php");  
          } else { // Login Failed  
              echo "<script>alert('Email id / Password Not Match')</script>";  
          }
      ?>
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面