dtvgo28624
2013-09-10 14:20
浏览 75
已采纳

在WAMP localhost上运行但在WebServer上不起作用的PHP代码

So, I built a website from scracth in PHP and used WAMP server for testing.

Everything is working on localhost, but after uploading the code to the server everything that uses connection to database doesn't work.

I've checked if the POST in php is working on server, and it is working, I changed some php.ini configurations after googling and I've done this:

register_globals = off; (was default)

allow_url_fopen = on; (was default)

magic_quotes_gpc = off; (was default)

Form Code

http://i.imgur.com/k5kMx9N.png

The login function inside class

function LoginUser($UserName, $Password)
{
    //Protect from SQL Injection
    $clientusername = stripslashes(mysql_real_escape_string($UserName));
    $clientpassword = stripslashes(mysql_real_escape_string($Password));

    $clientusername = trim($clientusername);
    $clientpassword = trim($clientpassword);

    if($clientusername != "" && $clientusername != "Username" && $clientpassword != "" && $clientpassword != "Password")
    {           
        $this->ConnectToMySQL();
        $sql = "SELECT * FROM Users WHERE UserName = '" . $clientusername . "' AND Password = '" . $clientpassword . "'";
        $result = mysqli_query($this->DBConnection, $sql);
        mysqli_close($this->DBConnection);      
        // Mysql_num_row is counting table row      
        $count = mysqli_num_rows($result);
        // If result matched $myusername and $mypassword, table row must be 1 row
        if($count == 1){
            if(!isset($_SESSION))
                session_start();
            $row = mysqli_fetch_array($result);
            $_SESSION['ID'] = session_id();
            $_SESSION['timeout'] = time();
            session_write_close();
            return("O seu login foi feito com sucesso.</br></br><a href='privatemenu.php'>Ir para o Menu</a>");
        }
        else return("Não foram encontrados utilizadores com os dados fornecidos.</br></br><a href='login.php'>Voltar á pagina de Login</a>");

    }
    else return("Ocorreu um erro com a entrada dos seus dados.</br></br><a href='login.php'>Voltar á pagina de Login</a>");
}

So, the problem is this, the function always returns the text "Ocorreu um erro com a entrada dos seus dados" in the login function.

I write the login data, push the button Login, the post is working I've tested with the same form and echo the post from the form on another page, but the function never goes trough that first if.

Any help would be very appreciated because I don't think that is from the function, I have another one that only does insert on the database and does exactly the same thing. And whats weird is the data is passing trough the pages correctly.

If anything on this is confuse please ask me so I can explain better.


So thanks to Paddyd here is the correct code totally working:

function LoginUser($UserName, $Password)
{
    //Protect from SQL Injection
            $this->ConnectToMySQL();
    $clientusername = stripslashes(mysqli_real_escape_string($this->DBConnection, $UserName));
    $clientpassword = stripslashes(mysqli_real_escape_string($this->DBConnection, $Password));

    $clientusername = trim($clientusername);
    $clientpassword = trim($clientpassword);

    if($clientusername != "" && $clientusername != "Username" && $clientpassword != "" && $clientpassword != "Password")
    {           
        $sql = "SELECT * FROM Users WHERE UserName = '" . $clientusername . "' AND Password = '" . $clientpassword . "'";
        $result = mysqli_query($this->DBConnection, $sql);
        mysqli_close($this->DBConnection);      
        // Mysql_num_row is counting table row      
        $count = mysqli_num_rows($result);
        // If result matched $myusername and $mypassword, table row must be 1 row
        if($count == 1){
            if(!isset($_SESSION))
                session_start();
            $row = mysqli_fetch_array($result);
            $_SESSION['ID'] = session_id();
            $_SESSION['timeout'] = time();
            session_write_close();
            return("O seu login foi feito com sucesso.</br></br><a href='privatemenu.php'>Ir para o Menu</a>");
        }
        else return("Não foram encontrados utilizadores com os dados fornecidos.</br></br><a href='login.php'>Voltar á pagina de Login</a>");

    }
    else return("Ocorreu um erro com a entrada dos seus dados.</br></br><a href='login.php'>Voltar á pagina de Login</a>");
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanbiao4035 2013-09-10 15:07
    已采纳

    Warning: mysql_real_escape_string(): Access denied for user 'apache'@'localhost' (using password: NO) in /home/httpd/vhosts/jaimevale.com/httpdocs/config/configurations.php on line 461

    These are the lines causing your problem:

    $clientusername = stripslashes(mysqli_real_escape_string($UserName));
    $clientpassword = stripslashes(mysqli_real_escape_string($Password));
    

    You are attempting to use mysql_real_escape_string without having established an sql connection. Either establish a connection before making these calls or use an alternative to mysql_real_escape_string.

    Try calling $this->ConnectToMySQL(); at the beginning of your function.

    Edit: Changed mysql_real_escape_string to mysqli_real_escape_string

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题