Shifter
Shifter

Reputation: 1

Connecting to a database from local server

I operate a large PHP website with some pages connecting to to a MySQL database, which works fine. I have now created a local test server and have downloaded my PHP pages and my database to wamp server where the pages the don't require a database connection work fine. However, on the pages that use the database, mysql_connect() requests throw an error:

Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

I can get a connection working by changing the MySQL request on the PHP page to:

$server="localhost";
$user="root";
$password="";

leaving $database="xxxx"; (original name)

This solution would mean I have to change every page with a MySQL request, and then change them back once development is complete.

Is there a way to get the original MySQL connect requests to work while keeping the original security data on the PHP pages? For example, by changing the wamp server config.inc.php file?

I've tried a number of combinations in the config.inc.php file but none seem to work.

Upvotes: 0

Views: 2821

Answers (3)

Jguy
Jguy

Reputation: 580

As previous answers have stated, you should simply move your connection parameters into a non-human readable file (i.e. outside your webserver root directory, if possible). This way you can store them securely and not risk outside access.

Then, include your file into each page where it is needed. Examples:

config.inc.php:

<?php
$db['host'] = '127.0.0.1';
$db['user'] = 'user';
$db['pw'] = 'passwd';
$db['name'] = 'dbname';

You could even get creative in this file:

<?php
// Production settings
$db['host'] = '127.0.0.1';
$db['user'] = 'user';
$db['pw'] = 'passwd';
$db['name'] = 'dbname';

// Test/Sandbox settings
/* 
$db['host'] = '127.0.0.1';
$db['user'] = 'user';
$db['pw'] = 'passwd';
$db['name'] = 'dbname2';
*/

Then simply switch the comments everytime you switch environments.

Then, just include this file into pages where you need to connect to your database:

index.php:

<html><head><tltle>My page</title>
...HTML Code here...
<?php include_once("/path/to/config.inc.php"); // Again, store this file outside the web directory, if possible ?>
<?php mysqli_connect($db['host'],$db['user'],$db['pw'],$db['name']); ?>

Upvotes: 0

Julien Mahin
Julien Mahin

Reputation: 36

A good solution would be to save these informations in a file that you include in all your other pages that need a mysql connection.

database.php

$server="localhost";
$user="root";
$password="";
$database = "nameofyourdb"

// Might event do the mysql_connect here

index.php ou any other page

include_once('database.php');
// your code
// containing mysql_query() and so on

Then you just need to change informations in one file when yo go from local to production.

Might even use a trick like this to avoid the change:

if ($_SERVER['SERVER_ADDR'] == '127.0.0.1') {
    // local
    $server="localhost";
    $user="root";
    $password="";
    $database = "nameofyourdb";
}
else {
    // production
    $server="localhost";
    $user="...";
    $password="...";
    $database = "...";
}

Be careful using the mysql API which is deprecated. You should move to PDO for example.

Upvotes: 1

m4t1t0
m4t1t0

Reputation: 5731

As I sayed in my comment, if you have the mysql connection credentials hardcoded in every file, you only have two alternatives:

  1. Replace the credentials in every file (with a massive search and replace)
  2. Put the same credentials in your local server. If the code try to connect to a remote host, you can put that host in your host file pointing to your localhost. This way you can simulate the remote environment.

Upvotes: 2

Related Questions