我的PHP脚本存在问题,似乎无法绕过它。我正在测试登录/注册模拟站点脚本,并收到以下错误:
严格标准:在第25行的C:\ vhosts \ goodgirls1 \ core \ database \ db.php中为类GoodGirls1Database重新定义已定义的构造函数
这是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,第41行的C:\ vhosts \ goodgirls1 \ core \ database \ db.php中给出的字符串
无法使用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*fMhb!@$%&^%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脚本,我必须编辑和分解,并且会非常感谢我为什么会遇到障碍而知道#&#; 39;击中警告。我知道有一些mysql
部分应该有mysqli
,但我试图在错误的同时逐步通过一个部分。感谢。
答案 0 :(得分:1)
严格标准:在第25行的C:\ vhosts \ goodgirls1 \ core \ database \ db.php中为类GoodGirls1Database重新定义已定义的构造函数
此错误是因为您已将类名声明为GoodGirls1Database,并且您还在该类中实现了名为GoodGirls1Database的方法。尝试更改方法名称或类名。
警告:mysqli_select_db()期望参数1为mysqli,第41行的C:\ vhosts \ goodgirls1 \ core \ database \ 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());
}
}