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

在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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测