php - 为什么我的站点在 xampp 中运行但不在我的 Apache 服务器中运行,而我在 Ubuntu 上设置的 mySQL?

标签 php mysql html apache xampp

我的登录站点完全在 Windows 上的 Xampp 中运行。 但是现在我将它复制到 Apache 服务器,它运行但是当我输入 id 时,通过并提交。登陆不上,总是返回“ID或密码不匹配,重试”

我在 Ubuntu 11.10 上安装了 Apache 服务器、MySQL、PHP5,并将我的数据库从 Xampp 复制到 Ubuntu 上的 MySQL。

这是我的代码:

登录.php

<?php session_start(); ?>
<?php if(isset($_SESSION['name'])) {
header('location: member.php?id='.$_SESSION['name']);
}?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Login page</title>
<link href="resources/style.css" type="text/css" rel="stylesheet">
</head>

<body>
<?php

require "./references/BLLClass.php";

//Khoi tao doi tuong BLL
$bllObj = new BLLClass();

//Kiem tra truong du lieu trong 2 o nhap
if(isset($_POST['submit'])) {
    $errors = array();
    $required = array('account', 'password');
    foreach($required as $fieldname) {
        if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
            $errors[] = "The <strong> $fieldname </strong> was left bank.";
        }
    } // end: foreach

    if(empty($errors)) {
        $acc = mysql_real_escape_string($_POST['account']);
        $pword = mysql_real_escape_string($_POST['password']);
        //ket noi lay du lieu xac thuc
        $result = $bllObj->AuthMember($acc, $pword);
        if(mysql_num_rows($result) == 1){
            while($rows = mysql_fetch_assoc($result)){
                $_SESSION['name'] = $rows['member_name'];
                header('Location: member.php');
            }; //end while
        } else {
            $errors[] = "The ID or Password do not match, try again";
        }
    }//end if(empty($errors)

} // end isset($_POST['submit'])
?>
<div id="wrapper">

    <?php
        if(!empty($errors)) {
            echo "<ul>";
            foreach($errors as $error) {
                echo "<li>$error</li>";
            }
            echo "</ul>";
        }
    ?> <!-- Hien thi thong bao loi khi o nhap lieu rong -->

    <?php if(isset($message)) echo $message;?>
    <form action="" method="POST">
    <p>
        <label for="account or id"> Account or ID: </label>
        <input type="text" name="account"/>
    </p> <!-- o nhap ID hoac nick login-->

    <p>
        <label for="password"> Password </label>
        <input type="password" name="password"/>
    </p> <!--o nhap password-->

    <p>
        <input type="submit" name="submit" value="Login"/>
        <a href="register.php">Register</a>
    </p> <!-- nut submit va chuyen trang dang ky-->

<!--        <div class='login'>
        <a href="#">Login</a>
    </div>
--> 
    </div>
</body>
</html>

BLLClass.php

<?php

//Business Logic Layer

require_once 'DBClass.php';

class BLLClass {    

    private $dbo;

    function BLLClass(){

    }
    //Check username password
    function AuthMember($username, $pword) {
        $this->dbo =    new DBClass();    
        $hash_pw = sha1($pword);
        $query = "SELECT member_name
                  FROM member
                  WHERE member_ID='$username'
                  OR member_login_name = '$username'
                  AND member_password = '$hash_pw'
                  LIMIT 1
                  ";
        $result = $this->dbo->SqlEx($query);
        return $result;
    }

}

?>

和DBClass.php

<?php

//Data Access Layer
class DBClass{

    private $mysql_hostname = "localhost";
    private $mysql_port = "3306";
    private $mysql_user = "root";
    private $mysql_password = "mypass";
    private $mysql_database = "db.sums01";


    public function DBClass(){


    }

    //khoi tao ket noi
    public function GetConn() {
    $conn = mysql_connect($this->mysql_hostname.":".$this->mysql_port, $this->mysql_user, $this->mysql_password) or die("Could not connect to DB");
    mysql_select_db($this->mysql_database, $conn) or die("Opps some thing went wrong");

    return $conn;
    }

    //gui query len co so du lieu
    public function SqlEx($query) {
        $conn = $this->GetConn();
        $result = mysql_query($query, $conn) or die (mysql_error($conn));
        return $result;
    }
}
?>

这是我的 apache 错误日志

[Thu May 24 20:42:27 2012] [error] [client 192.168.40.1] PHP Warning:  mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in ~/site/Login.php on line 33, referer: http://192.168.40.130/Login.php
[Thu May 24 20:42:27 2012] [error] [client 192.168.40.1] PHP Warning:  mysql_real_escape_string(): A link to the server could not be established in ~/site/Login.php on line 33, referer: http://192.168.40.130/Login.php
[Thu May 24 20:42:27 2012] [error] [client 192.168.40.1] PHP Warning:  mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in ~/site/Login.php on line 34, referer: http://192.168.40.130/Login.php
[Thu May 24 20:42:27 2012] [error] [client 192.168.40.1] PHP Warning:  mysql_real_escape_string(): A link to the server could not be established in ~/site/Login.php on line 34, referer: http://192.168.40.130/Login.php
[Thu May 24 20:42:27 2012] [error] [client 192.168.40.1] File does not exist: ~/site/favicon.ico

最佳答案

mysql_real_escape_string使用当前的 MySQL 连接来确定要转义的内容:

Escapes special characters in the unescaped_string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query(). If binary data is to be inserted, this function must be used.

如果没有可用的连接,它将尝试创建一个:

If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

这就是您收到该特定警告的原因——它正在尝试连接到本地主机,因为用户 PHP 以 (www-data) 身份运行且没有密码。

为了匹配您的其余代码,您可能想要创建一个使用该类中的连接的 DBClass->escape() 函数。

关于php - 为什么我的站点在 xampp 中运行但不在我的 Apache 服务器中运行,而我在 Ubuntu 上设置的 mySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10736609/

相关文章:

javascript - 选中复选框添加到购物车

php - UTF-8贯穿始终

php - 从 MySQL 数据库获取信息 (PHP)

python - 尝试使用Scrapy将数据输出到MySQL表时返回 'encode'错误

javascript - 来自 MySQL 数组的 Google 图表中的多行

python - 查找 Python 和 MySQL 脚本中的性能瓶颈

html - 如何在下拉菜单中使用分隔线分隔元素?

java - 帮助为学术项目选择网络应用程序框架

javascript - Font Awesome 图标在 IE8 中无法正确显示

javascript - 没有插件和框架的幻灯片图像