Connection to database server: connection timed out

database : mysql
tool: phpmyadmin

today my supervisor set up the web on the companies server. when its all settled, we try out the site. the index.php(first page) loads then when we try to login page(index.php + login.php), we get :

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

it probably has to do with connecting to the server. and i really have no clue whats going on. unless there is something wrong with these pages then its the server configuration?

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'];
		header('Location: home.php');
		exit;
	}
}
else
{
	header ("location : index.php");
	exit;
}
?>

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();
}
?>

this is the index.php page just incase:


<!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>

TIA!

it is my pages logic that is messing everything up:

  1. load index.php
  2. test for login data
  3. login data not found => redirect to index.php
  4. load index.php
  5. test for login data
  6. login data not found => redirect to index.php
  7. load index.php

at the end of my login.php there is a header location and i replaced it with an echo"fail" instead and that echo is displayed. how do i get around that? this was how i was taught to structure the flow of process. how come its reading isset($_POST['login]) etc… when no button is pressed? help. im confused.

I removed the else redirect in the login page. im getting the connection time out.

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 at a loss.

In connection.php, try changing

$connection = "mysql:host=1.2.3.4;dbname=dbname"; 

to

$connection = “mysql:host=localhost;dbname=dbname”;

or

$connection = “mysql:host=127.0.0.1;dbname=dbname”;

Sometimes hardcoding your IP can act funny.