/    Sign up×
Community /Pin to ProfileBookmark

PHP Login script failing to connect PLEASE HELP

Hi All. I am new to PHP and have slowly created a PHP login script however it is failing to connect. Please can someone advise. I am using a freenas hosting apache and using phpmyadmin.

The error i am getting is

“Warning: mysqli_connect(): (HY000/1130): Host ‘HostServer’ is not allowed to connect to this MySQL server in /usr/local/www/apache24/data/php/login.php on line 9
Database connection failed!Host ‘HostServer’ is not allowed to connect to this MySQL server”

[code=php]

$cookie_name = “loggedin”;

$servername = “192.168.0.2”;
$username = “root”;
$password = “XXXXXXXXXXXXXXX”;
$database = “homedb”;

$conn = mysqli_connect($servername, $username, $password, $database);

if (!$conn) {
die(“Database connection failed!”.mysqli_connect_error());
}

if (isset($_POST[‘login’]))
{
$user = $_POST[‘username’];
$pass = $_POST[‘password’];

$phash = sha1(sha1($pass.”salt”).”salt”);
$sql = “SELECT * FROM users WHERE username=’$user’ AND password=’$phash’;”;

$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);

if ($count == 1)
{
$cookie_value = $user;
setcookie($cookie_name, $cookie_value, time() + (180), “/”);
header(“Location: homepage.php”);
}

Else
{
echo “Username or Password is incorrect!”;
}
}

else if (isset($_POST[‘register’]))
{
$user = $_POST[‘username’];
$pass = $_POST[‘password’];

$phash = sha1(sha1($pass.”salt”).”salt”);
$sql = “INSERT INTO users (id, username, password) VALUES (”, ‘$user’, ‘$phash’);”;

$result = mysqli_query($conn, $sql);

}
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 03.2017 — You'll have to check with your hosting account or whoever takes care of the network config to make sure that you are using the correct IP or server name for the DB server (and possibly also a port number?).

If you are testing this from your personal PC and trying to connect to a DB server on your hosting site, it probably will not allow that -- in which case you may want to set up your own local MySQL server on your PC while developing/testing.
Copy linkTweet thisAlerts:
@rootJun 03.2017 — I would also agree with the IP issued, it will either be a specific IP address, localhost or the loopback address 127.0.0.1 as the connection and also there may be one issue of having to append a port number to the address as I once had to do with one hosting company who will remain nameless...
Copy linkTweet thisAlerts:
@benanamenJun 03.2017 — For the sake of anyone reading your cross posted question I will repeat my response to you on this forum.


You have many other problems. This is very poorly coded. You are creating variables for nothing, putting variables in your query and not using prepared statements, you are selecting everything instead of specific column names and using SHA1 on your passwords instead of properly using password_hash and you are outputting internal system errors to the user that is only good for hackers and you don't stop the script after the header redirect and you are depending on the name of a button to be submitted for the script to work which will completely fail in certain cases.

I recommend you use PDO with prepared statements. Here is a good tutorial https://phpdelusions.net/pdo
Copy linkTweet thisAlerts:
@crustametJun 07.2017 — I see here 2 problems

1st problem is that you try to connect to the Database trough another server

please follow this link to solve your problem.

https://stackoverflow.com/questions/17386171/php-connection-error-not-allowed-to-connect-to-mysql-server

The 2nd problem is not really a problem if you save the pass twice sha1(sha1()) in your database.

You can use only one time sha1().

if any problems are occuring with the first problem when you try to connect check if the server lets you connect remotely to the database.

I advice you to work on localhost then move it to your remote host.
Copy linkTweet thisAlerts:
@benanamenJun 07.2017 — I see here 2 problems

1st problem is that you try to connect to the Database trough another server

please follow this link to solve your problem.

https://stackoverflow.com/questions/17386171/php-connection-error-not-allowed-to-connect-to-mysql-server

The 2nd problem is not really a problem if you save the pass twice sha1(sha1()) in your database.

You can use only one time sha1().
[/QUOTE]


  • 1. There is nothing wrong with the way he is connecting. It is a configuration issue and the IP could very well be the same server the script is on.


  • 2. Do not use SHA anything for passwords. The correct method is password_hash.
  • ×

    Success!

    Help @jRogers spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...