How can i change my code to work when REGISTERED GLOBALS is no longer supported?

dayiku
dayiku used Ask the Experts™
on
I recently inherited some php code connecting to a mysql database.

There were some initial problems of connecting to the database but these problems have been solved.

However there is a chat system and users have been complaining about how slow it is.

I checked the logs and i see this message multiple times, "Cannot connect to database. Please, check config.php..."

 

This error message reminds me that when i took over the application, the hosting company released a statement saying they

no longer support "REGISTERED GLOBALS"

I wonder if this is affecting the chat system and generating some of the errors.

 

My questions,

1. Does anybody know why i am getting this error "Cannot connect to database. Please, check config.php..." intermittently?

   Note: Some users are still able to connect to the database

2. How can i check the code to make sure it has been modified to work with the hosting company settings?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
You have two very different questions here.  Please post the code that connects to the data base.  We can start on that first.
Most Valuable Expert 2011
Top Expert 2016

Commented:
You can learn about register globals here:
http://php.net/manual/en/security.globals.php

In essence, if the code depends on register-globals, it needs to be rewritten.  You can simulate register-globals by using extract() on the superglobal array $_REQUEST.  More information here:
http://www.php.net/manual/en/language.variables.external.php
http://us2.php.net/manual/en/function.extract.php

My advice is to refactor the code and not use extract().

Author

Commented:
Please see the code i am using to connect to the database below.
<?php

/* PLEASE DO NOT ALLOW EVEN ONE BLANK SPACE/LINE IN THIS FILE OUTSIDE '<?php' AND '?>' */

error_reporting(1);import_request_variables('gpc');error_reporting(8);


/* ------ BLAB SETTINGS ------  */


$db_type='mysql';                    // database type, *lowercase* ( options: mysql, mysqli, postgre, sqlite, pdo_sqlite )

$db_host='MYSQL';                // database host ( in most cases 'localhost' )
$db_user='username';                         // database user (not used with sqlite)
$db_pass='password';                         // database password  (not used  with sqlite)
$db_name='Chat3';                         // Database [mysql, postgre]. Note that the installation script cannot create a database for you!
$db_sqlite='sqlite/blab.dat';        // Database [sqlite]: 'path/filename', a 0-byte file CHMODed to 777 )

$skin_dir='skin4x-a';                // skin directory, no trailing slashes


/* ------- INTEGRATION -------  */


$intg_bb_cms='blab';                 // integration with: *lowercase*
                                     // [available options: blab, ipb1, ipb2, phpbb2, phpbb3, vb, smf1, smf2, wbb, wcf, punbb, e107, mambo, joomla, phpfusion, phpnuke, postnuke, eengine, phorum, mybb, vanilla] (blab == standalone)

$intg_cookie='';                     // Session coookie [or other cookie] set by your phpBB, IPB, vB, Joomla etc. 
                                     // example: http://hot-things.net/cs/article.php?aid=122

$intg_prefix='';                     // Table prefix of your phpBB, IPB, vB, Joomla etc database tables 
                                     // The prefix must include the sign _ if exists { e.g. $intg_prefix='phpbb_'; }

$intg_suffix=0;                      // [1 or 0] displays the sign ^ after phpBB, IPB, vB, Joomla etc usernames
$intg_showex=0;                      // [1 or 0] show or hide the logout link, 0 when embeded in an iframe, otherwise leave it 1 and set $intg_exitlnk

$intg_moders=array();                // Moderators to import. This should be an array, e.g. array(1,6,432,8839); where each number is the user ID of the user from your phpBB, vB, IPB etc user table

$intg_extlnk='../';                  // logout link [URL or relative path], default one level up: $intg_exitlnk='../';
$intg_rlpath='../';                  // URL or relative path to your phpBB, IPB, vB, Joomla etc, default one level up: $intg_rlpath='../'; [must end with a trailing slash]

$intg_nolglk='login.php';            // where to redirect users that are not logged onto your phpBB, IPB, vB, Joomla etc. 
                                     // 'register.php' - register blab account [if allowed: Admin CP -> Settings]
                                     // 'login.php' - login as guest [if allowed: Admin CP -> Settings]

$encoding='iso-8859-1';              // the encoding used in your phpBB, IPB, vB, Joomla etc.

$cookie_seed='';                     // PunBB only!  Can be found in 'punbb/config.php'


/* DO NOT TOUCH ANYTHING BELOW THIS LINE UNLESS YOU ARE ABSOLUTELY SURE WHAT YOU ARE DOING! */


$default_timezone=0;                 // default timezone: 0=GMT [from -12 to 12]
$default_timeform=0;                 // default time format [0-5] default:0, no timestamp: 5
$default_language=0;                 // default language: an array element # from the array $lang_files() in 'lang/languages.inc' [PHP arrays start from 0]
$default_emoticon=1;                 // default smilies & colors: off/on [0-1]
$default_soundenb=1;                 // default sound: off/on [0-1]

$administrators=array(1);            // Administrators. This should be an array, e.g. array(1,2,3,55); where each number is the user ID of the user from the 'blab_users' table

$error_log='errors.txt';             // CHMODed to 777 file to store sql errors if any ( it is strongly recommended to rename this file )
$salt='RandOM_StRiNG_123';           // Salt. 
$prefix='blab';                      // Table prefix for BlaB! tables
                                     // You may change both salt & table prefix before running the installation.
                                     // You MUST NOT change salt & table prefix later!

$pconnection=0;                      // [0 or 1] Establishes a persistent connection to the SQL server. If you are not sure leave it 0.
$update=6;                           // seconds [6-20]
$session=48;                         // hours to remember the login cookie [6-8544, 8544 hours = 1 year]
$history=168;                        // hours to keep messages in db [168 hours = 1 week, 8544 = 1 year]
$lat_msgs=15;                        // number of messages to be shown when entering a room [5-20]
$ads_fqcy=0;                         // ads frequency. [0-100] 0 = never, 100 = after each message [acceptable level 5-10]
?>

Open in new window

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Most Valuable Expert 2011
Top Expert 2016

Commented:
Those are data definitions.  We would want to be looking for something that contained the functions mysql_connect() and mysql_select_db() or equivalent.

Author

Commented:
Please see below
function neutral_dbconnect(){
global $db_host,$db_user,$db_pass,$db_name;
mysql_connect($db_host,$db_user,$db_pass) or process_error('Cannot connect to database. Please, check config.php...');
mysql_select_db($db_name) or process_error(mysql_error());}

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
OK, it looks like the mysql_connect statement is failing, but the code does not print the reason for failure.  You will need to know that.  Here is how I do that connect / select sequence.  It tells me about either function error and dies if the DB is not ready for use.

HTH, ~Ray
// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB CONNECTION: ";
   echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB SELECTION: ";
   echo "<br/> $errmsg <br/>";
   die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES

Open in new window

Author

Commented:
So i should place the code you have above in the function neutral_dbconnect(), correct?
I am asking because the site is live now and i need to make sure.
Most Valuable Expert 2011
Top Expert 2016

Commented:
@dayiku: The code I posted there is a teaching example; you can use it as a guideline to create your own code modifications.  A "best practices" suggestion, going forward, would be to have a mirror image of the production site somewhere so you can test and do not have to put a live site at risk of a typographical error.  Best, ~Ray

Author

Commented:
Thanks

Author

Commented:
Just an update, i received the error message below after i used the debugging guide you gave me.
I have been googling the errors but so far no good hints for me. Do you have any thoughts on what could be causing this?

2010-02-02 01:29:03 99.235.109.100 Connect failed
2010-02-02 02:34:02 68.148.237.157 2013 Lost connection to MySQL server during query
2010-02-02 02:34:03 208.78.58.9 2003 Can't connect to MySQL server on 'MYSQL' (61)
Most Valuable Expert 2011
Top Expert 2016

Commented:
Error #2013 is pretty self explanatory.  MySQL apparently died.  See the note here:
http://lists.mysql.com/mysql/217668

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial