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 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了