database : mysql
tool: phpmyadmin

today my supervisor set up the web on the companies server. when its all settled, we try out the login page(index.php) but we get fatal error and the line its pointing at refers to the connection.php page which connects to the database.

Fatal error: Call to a member function quote() on a non-object = $dbh->quote($email); in index.php

connection.php:

<?php
$username = "username"; 
$password = "password";  

try {
$dbh = new PDO('mysql:host=11.22.33.66;dbname=dbname', $username, $password);
$dbh->setAttribute(PDO::ERRMODE_WARNING, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
    echo "I'm sorry, I'm afraid i can't do that.";
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
    $dbh = null;
}
?>

of course before this i was using xampp,localhost, so ive changed what needed to be changed: username, password, host and dbname.

i checked the PDOErrors.txt:

SQLSTATE[28000] [1045] Access denied for user 'username'@'11.22.33.61' (using password: YES)

^
why is it 11.22.33.61 and not 11.22.33.66 ? is that what is causing the error? or is there something else?

help. TIA

Your username is not allowed to remotely connect to that machine. If it's freshly setup, ask your supervisor if he has added all the right priviliges correctly, and whether the machine's IP has changed.

the ip bit is bothering me though. because in the script i literally typed in the ip address and the result is a different ip address. like it gets redirected or something and no the ip isnt/hasnt changed.

now it says:

Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '111.222.111.121' (110)Fatal error: Call to a member function prepare() on a non-object in /home//ems/index.php on line 12

the ip address is correct though. as in its the same as in the connection.php just that it can't connect. dont know why though.

code:

<?php
$username = "username"; 
$password = "password"; 
$connection = "mysql:host=ipaddress;dbname=dbname"; 

try {
    $dbh = new PDO($connection, $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

but im still getting the fatal error. what is wrong w the php?

index.php:

<?php session_start();   ?>
<?php
if(isset($_POST['login']))
{
    require "connection.php";

    $email = $_POST['email'];
    $password = $_POST['pass'];

    $query = "SELECT id,name,email,password FROM users WHERE email = :email";

    $result = $dbh->prepare($query);//LINE 12
    $result->bindParam(':email', $email, PDO::PARAM_STR);

    $userData = $result->fetch(PDO::FETCH_ASSOC);
    $userEmail = $userData['email'];
    $hash = $userData['password'];

    if(!password_verify($password, $hash) && $email != $userEmail)
    {
        echo "<script>alert(\"Incorrect Email and/or Password!\")</script>";
    }else{ 
        session_start();
        $_SESSION['sess_user_id'] = $userData['id'];
        $_SESSION['sess_name'] = $userData['name'];
        header('Location: home.php');
    }

    $result->closeCursor();
    $dbh = null;
}
else{header ("location : index.php");}
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8">
    <title>EMS</title>
    <link rel="stylesheet" type="text/css" media="all" href="css/style.css">
</head>

<body>

    <div id="page-wrap">
        <h2>Event Management System<a href="#" id="login">Login</a><label>|</label><a href="#" id="sign">Sign Up</a></h2>
    </div>

<div id="s-overlay" class="s-overlay"></div>
<div id="tsu-wrap">
    <form name="register" class="register" method="post" action="index.php">
    <table id="tsu">
        <thead><tr><th>Sign Up</th></tr></thead>
        <tbody>
            <tr><td class="rd"><input type="tex" name="fulln" title="Please use letters only" placeholder="Norin Nong" required></td></tr>
            <tr><td class="rd"><input type="email" name="em" placeholder="norin@gmail.com" required></td></tr>
            <tr><td class="rd"><input type="text" name="tel" placeholder="012-9635874" required></td></tr>
            <tr><td class="rd"><input type="password" name="pass" title="Follow format" placeholder="Something Secure" required></td></tr>
            <tr><td><label>Minimum 8;at least 1 number;at least 1 letter;special characters(optional):!@#$%</label></td></tr>
            <tr><td class="r"><input type="password" name="cpass" placeholder="Confirm Password" required></td></tr>
            <tr><td><input type="submit" name="signup" class="signup">
            <input type="button" class="cancel" value="Cancel" /></td></tr>
        </tbody>
    </table>
    </form>
</div>

<div id="l-overlay" class="l-overlay"></div>
<div id="tl-wrap">
    <form name="login" method="post" action="index.php" id="login-form">
    <table id="tl">
        <thead><tr><th style="padding:5px 5px 5px 5px">Login</th></tr></thead>
        <tbody>
            <tr><td class="log"><input type="text" name="email" placeholder="EMAIL" size="30" autofocus ></td></tr>
            <tr><td class="lg"><input type="password" name="pass" placeholder="PASSWORD" class="pass"></td></tr>
            <span class="errormsg"></span>
            <tr><td><input type="submit" name="login" class="login">
            <input type="submit" class="close" value="Cancel" /></td></tr>
        </tbody>
    </table>
    </form>
</div>

<script type="text/javascript">
jQuery(function(){
        $("#signup").click(function(){
        $(".error").hide();
        var hasError = false;
        var passwordVal = $(".pass").val();
        var checkVal = $(".cpass").val();
        if (passwordVal != checkVal ) {
            $(".cpass").after('<br><span class="error">Passwords do not match.</span>');
            hasError = true;
        }
        if(hasError == true) {return false;}
    });
});
</script>

</body>
</html>
<?php
if(isset($_POST['signup']))
{
    require "connection.php";

    $name = $_POST['fulln'];
    $email = $_POST['em'];
    $password = $_POST['pass'];
    $number = $_POST['tel'];

    $hash = password_hash($password, PASSWORD_DEFAULT);

    $sql = $dbh -> prepare("INSERT INTO users (name, email, number, password) VALUES(:name, :email, :number, :password)");
    $sql->bindParam('name',$name);
    $sql->bindParam('email',$email);
    $sql->bindParam('number',$number);
    $sql->bindParam('password',$hash);

    if($sql->execute())
    { echo "<script>alert(\"Sign Up successfull.\");window.location = \"index.php\"</script>";}
    else
    {echo "<script>alert(\"Problem with server.\");window.location = \"index.php\"</script>";}
}
?>

okay so im getting

This web page has a redirect loop

when i try to load the page. i have already added exit; after headers. there are only two headers.

index.php:

<?php session_start();   ?>
<?php
if(isset($_POST['login']))
{
    require "connection.php";

    $email = $_POST['email'];
    $password = $_POST['pass'];

    $query = $dbh->prepare("SELECT id,name,email,password FROM users WHERE email = :email");
    $query->bindParam(':email', $email, PDO::PARAM_STR);

    $userData = $query->fetch(PDO::FETCH_ASSOC);
    $userEmail = $userData['email'];
    $hash = $userData['password'];

    if(!password_verify($password, $hash) && $email != $userEmail)
    {
        echo "<script>alert(\"Incorrect Email and/or Password!\")</script>";
    }else
    { 
        session_start();
        $_SESSION['sess_user_id'] = $userData['id'];
        $_SESSION['sess_name'] = $userData['name'];
        header('Location: home.php');//HERE
        exit;
    }
}
else
{
    header ("location : index.php"); //HERE
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8">
    <title>EMS</title>
    <link rel="stylesheet" type="text/css" media="all" href="css/style.css">
    <script type="text/javascript" src="js/jquery 1.8.2.js"></script>
</head>

<body>

    <div id="page-wrap">
        <h2>Event Management System<a href="#" id="login">Login</a><label>|</label><a href="#" id="sign">Sign Up</a></h2>
    </div>

<div id="s-overlay" class="s-overlay"></div>
<div id="tsu-wrap">
    <form name="register" class="register" method="post" action="index.php">
    <table id="tsu">
        <thead><tr><th>Sign Up</th></tr></thead>
        <tbody>
            <tr><td class="rd"><input type="text" name="fulln" placeholder="Norin Nong" required></td></tr>
            <tr><td class="rd"><input type="email" name="em" placeholder="norin@gmail.com" required></td></tr>
            <tr><td class="rd"><input type="text" name="tel" placeholder="012-9635874" required></td></tr>
            <tr><td class="rd"><input type="password" name="pass" placeholder="Something Secure" required></td></tr>
            <tr><td><label>Minimum 8;at least 1 number;at least 1 letter;special characters(optional):!@#$%</label></td></tr>
            <tr><td class="r"><input type="password" name="cpass" placeholder="Confirm Password" required></td></tr>
            <tr><td><input type="submit" name="signup" class="signup">&nbsp;&nbsp;<input type="button" class="cancel" value="Cancel" /></td></tr>
        </tbody>
    </table>
    </form>
</div>

<div id="l-overlay" class="l-overlay"></div>
<div id="tl-wrap">
    <form name="login" method="post" action="index.php" id="login-form">
    <table id="tl">
        <thead><tr><th>Login</th></tr></thead>
        <tbody>
            <tr><td class="log"><input type="text" name="email" placeholder="EMAIL" autofocus ></td></tr>
            <tr><td class="lg"><input type="password" name="pass" placeholder="PASSWORD"></td></tr>
            <span class="errormsg"></span>
            <tr><td><input type="submit" name="login" class="login">
                        <input type="submit" class="close" value="Cancel" /></td></tr>
        </tbody>
    </table>
    </form>
</div>

<script type="text/javascript">
jQuery(function(){
        $("#signup").click(function(){
        $(".error").hide();
        var hasError = false;
        var passwordVal = $(".pass").val();
        var checkVal = $(".cpass").val();
        if (passwordVal != checkVal ) {
            $(".cpass").after('<br><span class="error">Passwords do not match.</span>');
            hasError = true;
        }
        if(hasError == true) {return false;}
    });
});
</script>

</body>
</html>
<?php
if(isset($_POST['signup']))
{
    require "connection.php";

    $name = $_POST['fulln'];
    $email = $_POST['em'];
    $password = $_POST['pass'];
    $number = $_POST['tel'];

    $hash = password_hash($password, PASSWORD_DEFAULT);

    $sql = $dbh -> prepare("INSERT INTO users (name, email, number, password) VALUES(:name, :email, :number, :password)");
    $sql->bindParam('name',$name);
    $sql->bindParam('email',$email);
    $sql->bindParam('number',$number);
    $sql->bindParam('password',$hash);

    if($sql->execute())
    { echo "<script>alert(\"Sign Up successfull.\");window.location = \"index.php\"</script>";}
    else
    {echo "<script>alert(\"Problem with server.\");window.location = \"index.php\"</script>";}
}
?>

connection.php:

<?php
$username = "username"; 
$password = "password"; 
$connection = "mysql:host=1.2.3.4;dbname=dbname"; 

try {
    $dbh = new PDO($connection, $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>

TIA!

now im getting this when i try to login:

Connection failed: SQLSTATE[HY000] [2002] Connection timed out

connection.php:

<?php
try {
    $username = "username"; 
    $password = "password"; 
    $connection = "mysql:host=ipaddress;dbname=dbname"; 

    $dbh = new PDO($connection, $username, $password, array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} 
catch (PDOException $e)
{
    echo 'Connection failed: ' . $e->getMessage();
}
?>

login.php:

<?php 
if(isset($_POST['login']))
{
    require 'connection.php';

    $email = $_POST['email'];
    $password = $_POST['pass'];

    $query = $dbh->prepare("SELECT id,name,email,password FROM users WHERE email = :email");
    $query->bindParam(':email', $email, PDO::PARAM_STR);

    $userData = $query->fetch(PDO::FETCH_ASSOC);
    $userEmail = $userData['email'];
    $hash = $userData['password'];

    if(!password_verify($password, $hash) && $email != $userEmail)
    {
        $error = "";
        $error .= "Incorrect email and/or password";
    }
    else
    { 
        session_start();
        $_SESSION['sess_user_id'] = $userData['id'];
        $_SESSION['sess_name'] = $userData['name'];
        echo "<script type='text/javascript>window.location.href = 'home.php'; </script>";
    }
}
?>

Im really at a loss here. and its getting really frustrating.

Did you contact the network administrator? A timeout could be anything from some setting to firewall/router setup.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.