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>";  
          }
      ?>
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?