我的登录站点完全在Windows上的Xampp中运行。
但是现在我把它复制到Apache服务器,它运行,但是当我输入id,pass和submit时。它不登录,总是返回“ID或密码不匹配,请重试”
我在Ubuntu11.10上安装了ApacheServer、MySQL和PHP5,并在Ubuntu11.10上将我的数据库从Xampp复制到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连接找出要转义的内容:
在考虑到连接的当前字符集的情况下,转义未转义字符串中的特殊字符,以便可以安全地将其放在mysql_query()中。如果要插入二进制数据,则必须使用此函数。
如果没有可用的连接,它将尝试创建一个:
如果未指定链接标识符,则假定是mysql_connect()打开的最后一个链接。如果找不到这样的链接,它将尝试创建一个链接,就像调用mysql_connect()时没有参数一样。如果未找到或建立连接,则会生成E U警告级别错误。
这就是为什么您会收到那个特别的警告——它试图连接到本地主机,因为用户PHP正在以(www data)的形式运行,没有密码。
为了匹配代码的其余部分,您可能需要创建一个DBClass->escape()函数,该函数使用该类中的连接。

09-26 21:58
查看更多