2014-11-03 206 views
0

我的PHP腳本有問題,似乎無法解決它。我正在測試一個登錄/註冊模擬站點腳本,並且出現以下錯誤:MySQL嚴格標準警告 - 重新定義類已定義的構造函數

嚴格標準:重新定義C:\ vhosts \ goodgirls1 \ core \ database \ db.php中GoodGirls1Database類的已定義構造函數線25

這裏是爲db.php文件的代碼:

<?php 
// Our database class 
if(!class_exists('GoodGirls1Database')){ 
class GoodGirls1Database { 

    /** 
    * Connects to the database server and selects a database 
    * 
    * PHP4 compatibility layer for calling the PHP5 constructor. 
    * 
    * @uses GoodGirls1Database::__construct() 
    * 
    */ 
    function GoodGirls1Database() { 
     return $this->__construct(); 
    } 

    /** 
    * Connects to the database server and selects a database 
    * 
    * PHP5 style constructor for compatibility with PHP5. Does 
    * the actual setting up of the connection to the database. 
    * 
    */ 
    function __construct() { 
     $this->connect(); 
    } 

    /** 
    * Connect to and select database 
    * 
    * @uses the constants defined in config.php 
    */ 
    function connect() { 
     $link = mysqli_connect('localhost', DB_USER, DB_PASS); 

     if (!$link) { 
      die('Could not connect: ' . mysql_error()); 
     } 

     $db_selected = mysqli_select_db('DB_NAME', $link); 

     if (!$db_selected) { 
      die('Can\'t use ' . DB_NAME . ': ' . mysql_error()); 
     } 
    } 

    /** 
    * Clean the array using mysql_real_escape_string 
    * 
    * Cleans an array by array mapping mysql_real_escape_string 
    * onto every item in the array. 
    * 
    * @param array $array The array to be cleaned 
    * @return array $array The cleaned array 
    */ 
    function clean($array) { 
     return array_map('mysql_real_escape_string', $array); 
    } 

    /** 
    * Create a secure hash 
    * 
    * Creates a secure copy of the user password for storage 
    * in the database. 
    * 
    * @param string $password The user's created password 
    * @param string $nonce A user-specific NONCE 
    * @return string $secureHash The hashed password 
    */ 
    function hash_password($password, $nonce) { 
     $secureHash = hash_hmac('sha512', $password . $nonce, SITE_KEY); 

     return $secureHash; 
    } 

    /** 
    * Insert data into the database 
    * 
    * Does the actual insertion of data into the database. 
    * 
    * @param resource $link The MySQL Resource link 
    * @param string $table The name of the table to insert data into 
    * @param array $fields An array of the fields to insert data into 
    * @param array $values An array of the values to be inserted 
    */ 
    function insert($link, $table, $fields, $values) { 
     $fields = implode(", ", $fields); 
     $values = implode("', '", $values); 
     $sql="INSERT INTO $table (id, $fields) VALUES ('', '$values')"; 

     if (!mysql_query($sql)) { 
      die('Error: ' . mysql_error()); 
     } else { 
      return TRUE; 
     } 
    } 

    /** 
    * Select data from the database 
    * 
    * Grabs the requested data from the database. 
    * 
    * @param string $table The name of the table to select data from 
    * @param string $columns The columns to return 
    * @param array $where The field(s) to search a specific value for 
    * @param array $equals The value being searched for 
    */ 
    function select($sql) { 
     $results = mysql_query($sql); 

     return $results; 
    } 
} 
} 

//Instantiate our database class 
$ggdb = new GoodGirls1Database; 
?> 

當然,我在做劇本的幾個微妙的變化,但我不熟悉,因爲這特別的錯誤。此外,它被命名爲goodgirls_1我的測試數據庫中,被改變了,因爲我也收到此錯誤:

警告:mysqli_select_db()預計參數1是mysqli的,串在C中給出:\虛擬主機\ goodgirls1 \核心\數據庫\ db.php 41行 不能使用goodgirls1:但我仍然得到了錯誤goodgirls1

同樣地,這裏的config.php相對db.php文件的文件中的代碼,:

<?php 
/* Configuration Info 
* Enter your configuration information below. 
*/ 

//Database Information 

/* DB Name 
* Enter the name of your database below. 
*/ 
define('DB_NAME', 'goodgirls1'); 

/* DB Username 
* Enter the username of the user with access to the database below. 
*/ 
define('DB_USER', 'root'); 

/* DB Password 
* Enter the above user's password below. 
*/ 
define('DB_PASS', 'TempPass4!'); 

//SALT Information 

/* Site Key 
* Enter your site key below. Used by adding 8 random 16-character string values.  Recreated by DesignerMind. 
*/ 
define('SITE_KEY', 'DFDfdd Jea*jdfv(087KlwacbMFd dfj()8&^(%)+-dfwqefd55d*[email protected]$%&^%VQJsxGjOIdej#OT3EhCpxqC5Bu6KSOJM$$##VJV9jLF5uWiiFXm1G');     

/* NONCE SALT 
* Enter your NONCE SALT below. Recreated by DesignerMind. 
*/ 
define('NONCE_SALT', 'e^$#fdf)jdffdASQ2_)(eh2DfbOOX4*&F73ldggm8KZP35N48t3OVbTaoOpaOlLydef#_+kvusgNgafnuujTPdazfzqpDy'); 

/* AUTH SALT 
* Enter your AUTH SALT below. Recreated by DesignerMind. 
*/ 
define('AUTH_SALT', '-=+fQ~223_ofydfdUm9SXCqWWvSDm6&^&k3iwMqPghWzTgqMSiy)(&*&RaAoM/**J343^((&!N_=dfdfOp4vH(gwL0fA75/vH04r2xjp7KH2ahNNc'); 
?> 

最後,如果需要的話,這裏有一個class.php文件,我用它來實例化我的數據庫:

<?php 
// Our main class 
if(!class_exists('GoodGirls1')){ 
class GoodGirls1 { 

    function register($redirect) { 
     global $ggdb; 

     //Check to make sure the form submission is coming from our script 
     //The full URL of our registration page 
     $current = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 

     //The full URL of the page the form was submitted from 
     $referrer = $_SERVER['HTTP_REFERER']; 

     /* 
     * Check to see if the $_POST array has date (i.e. our form was submitted) and if so, 
     * process the form data. 
     */ 
     if (!empty ($_POST)) { 

      /* 
      * Here we actually run the check to see if the form was submitted from our 
      * site. The registration from submits to itself; 
      * If the form submission didn't come from the register.php page on our server, 
      * we don't allow the data through. 
      */ 
      if ($referrer == $current) { 

       //Require our database class 
       require_once('../../db.php'); 

       //Set up the variables we'll need to pass to our insert method 
       //This is the name of the table we want to insert data into 
       $table = 'users'; 

       //These are the fields in that table that we want to insert data into 
       $fields = array('username', 'password', 'first_name', 'last_name', 'email', 'user_registered'); 

       //These are the values from our registration form... cleaned using our clean method 
       $values = $ggdb->clean($_POST); 

       //Now, we're breaking apart our $_POST array, so we can storely our password securely 
       $username = $_POST['username']; 
       $userpass = $_POST['password']; 
       $userfirst = $_POST['first_name']; 
       $userlast = $_POST['last_name'];      
       $useremail = $_POST['email']; 
       $userreg = $_POST['date']; 

       //We create a NONCE using the action, username, timestamp, and the NONCE SALT 
       $nonce = md5('registration-' . $username . $userreg . NONCE_SALT); 

       //We hash our password 
       $userpass = $ggdb->hash_password($userpass, $nonce); 

       //Recompile our $value array to insert into the database 
       $values = array(
          'username' => $username, 
          'password' => $userpass, 
          'first_name' => $userfirst, 
          'last_name' => $userlast,        
          'email' => $useremail, 
          'date' => $userreg 
         ); 

       //And, we insert our data 
       $insert = $ggdb->insert($link, $table, $fields, $values); 

       if ($insert == TRUE) { 
        $url = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
        $aredirect = str_replace('register.php', $redirect, $url); 

        header("Location: $redirect?reg=true"); 
        exit; 
       } 
      } else { 
       die('Your form submission did not come from the correct page. Please check with the site administrator.'); 
      } 
     } 
    } 

    function login($redirect) { 
     global $ggdb; 

     if (!empty ($_POST)) { 

      //Clean our form data 
      $values = $ggdb->clean($_POST); 

      //The username and password submitted by the user 
      $subname = $values['username']; 
      $subpass = $values['password']; 

      //The name of the table we want to select data from 
      $table = 'users'; 

      /* 
      * Run our query to get all data from the users table where the user 
      * login matches the submitted login. 
      */ 
      $sql = "SELECT * FROM $table WHERE user_login = '" . $subname . "'"; 
      $results = $ggdb->select($sql); 

      //Kill the script if the submitted username doesn't exit 
      if (!$results) { 
       die('Sorry, that username does not exist!'); 
      } 

      //Fetch our results into an associative array 
      $results = mysql_fetch_assoc($results); 

      //The registration date of the stored matching user 
      $storeg = $results['user_registered']; 

      //The hashed password of the stored matching user 
      $stopass = $results['password']; 

      //Recreate our NONCE used at registration 
      $nonce = md5('registration-' . $subname . $storeg . NONCE_SALT); 

      //Rehash the submitted password to see if it matches the stored hash 
      $subpass = $ggdb->hash_password($subpass, $nonce); 

      //Check to see if the submitted password matches the stored password 
      if ($subpass == $stopass) { 

       //If there's a match, we rehash password to store in a cookie 
       $authnonce = md5('cookie-' . $subname . $storeg . AUTH_SALT); 
       $authID = $ggdb->hash_password($subpass, $authnonce); 

       //Set our authorization cookie 
       setcookie('goodgirls1logauth[user]', $subname, 0, '', '', '', true); 
       setcookie('goodgirls1logauth[authID]', $authID, 0, '', '', '', true); 

       //Build our redirect 
       $url = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
       $redirect = str_replace('login.php', $redirect, $url); 

       //Redirect to the home page 
       header("Location: $redirect"); 
       exit; 
      } else { 
       return 'invalid'; 
      } 
     } else { 
      return 'empty'; 
     } 
    } 

    function logout() { 
     //Expire our auth coookie to log the user out 
     $idout = setcookie('goodgirls1logauth[authID]', '', -3600, '', '', '', true); 
     $userout = setcookie('goodgirls1logauth[user]', '', -3600, '', '', '', true); 

     if ($idout == true && $userout == true) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    function checkLogin() { 
     global $ggdb; 

     //Grab our authorization cookie array 
     $cookie = $_COOKIE['goodgirls1logauth']; 

     //Set our user and authID variables 
     $user = $cookie['user']; 
     $authID = $cookie['authID']; 

     /* 
     * If the cookie values are empty, we redirect to login right away; 
     * otherwise, we run the login check. 
     */ 
     if (!empty ($cookie)) { 

      //Query the database for the selected user 
      $table = 'users'; 
      $sql = "SELECT * FROM $table WHERE username = '" . $user . "'"; 
      $results = $ggdb->select($sql); 

      //Kill the script if the submitted username doesn't exit 
      if (!$results) { 
       die('Sorry, that username does not exist!'); 
      } 

      //Fetch our results into an associative array 
      $results = mysql_fetch_assoc($results); 

      //The registration date of the stored matching user 
      $storeg = $results['user_registered']; 

      //The hashed password of the stored matching user 
      $stopass = $results['password']; 

      //Rehash password to see if it matches the value stored in the cookie 
      $authnonce = md5('cookie-' . $user . $storeg . AUTH_SALT); 
      $stopass = $ggdb->hash_password($stopass, $authnonce); 

      if ($stopass == $authID) { 
       $results = true; 
      } else { 
       $results = false; 
      } 
     } else { 
      //Build our redirect 
      $url = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
      $redirect = str_replace('index.php', 'login.php', $url); 

      //Redirect to the home page 
      header("Location: $redirect?msg=login"); 
      exit; 
     } 

     return $results; 
    } 
} 
} 

//Instantiate our database class 
$gg1 = new GoodGirls1; 
?> 

這是我的第一個主要的PHP腳本,我不得不編輯和分解,並會很感激爲什麼我跑到我碰到的機會h警告。我知道有一些mysql部分,其中應該有mysqli,但我試圖一次一步處理錯誤中的某個部分。謝謝。

回答

1

嚴格的標準:重新定義已經在C中定義構造函數類GoodGirls1Database:\虛擬主機\ goodgirls1 \核心\數據庫\上線db.php中25

這個錯誤是因爲你已經聲明的類名GoodGirls1Database,並且您還在該類中實施了一個名爲GoodGirls1Database的方法。嘗試更改方法的名稱或類名稱。

警告:mysqli_select_db()預計參數1是mysqli的,串在C中給出:\虛擬主機\ goodgirls1 \上線41芯\數據庫\ db.php中無法使用goodgirls1:

您在mysqli_select_db()中傳遞了錯誤的參數順序。 $link應該是第一個參數。

function connect() { 
     $link = mysqli_connect('localhost', DB_USER, DB_PASS); 

     if (!$link) { 
      die('Could not connect: ' . mysql_error()); 
     } 

This line ---->  $db_selected = mysqli_select_db($link,'DB_NAME'); 

     if (!$db_selected) { 
      die('Can\'t use ' . DB_NAME . ': ' . mysql_error()); 
     } 
    } 
+0

謝謝,經過一些調整,並添加到參數,它的工作! – DesignerMind 2014-11-03 18:43:54

+0

很高興我可以幫助一些:) – Rumin 2014-11-04 03:26:24