MG1
MG1

Reputation: 1687

Creating a Secure Connection to a Server using PHP

I am trying to query a database that's on a server that I have a Remote Desktop connection to. What is required to be able to query a database on this server using my php web application.

Upvotes: 0

Views: 813

Answers (4)

Conex
Conex

Reputation: 822

use answer by Merch member - the php code above . You need to grant privileges and to create user on server you are retrieving the database data from.

Connect to mysql : in shell type (if you are root type root or whatever username): mysql -u root -p

enter password,

when you are logged in :

create user someusername@your-client-ip identified by 'password';
grant all privileges on *.* to someusername@your-client-ip with grant option;

now you can use php code above to connect to remote server database (ip you just used to create mysql user your-client-ip)

When you are creating php variable for mysql connect - host use mysql port in the variable if just ip not connects your-client-ip:3306

good luck

Upvotes: 1

user475353
user475353

Reputation:

I use something like this, but it's far from secure. I keep it in a seperate "connection.php" that are required once by other files.

$db_host = 'INSERT IP HERE';
$db_user = 'User';
//My sql password for testing purposes
$db_pass = 'PASSWORD';
//name of the database
$db_name = 'dbname';

//connection
$conn = new mysqli($db_host, $db_user, $db_pass ,$db_name);

if (mysqli_connect_errno()) 
{
    printf("Error connecting to DB, Errorcode: %s\n", mysqli_connect_error());
    exit;
}

Upvotes: 1

Kel
Kel

Reputation: 7780

You need:

  1. Database server application configured to allow connections from external hosts

  2. Server-side network configuration tuned to allow connections from external hosts (firewall, NAT, ...)

  3. Database user, which is granted access to database you are going to use

  4. PHP application, which connects to your database server under appropriate user

Details depend on what database server are you using.

Upvotes: 1

Shoe
Shoe

Reputation: 76240

$link = mysql_connect($host, $username, $password);

There is nothing secure at all by the way.

Upvotes: 1

Related Questions