Forum Moderators: coopster

Message Too Old, No Replies

Refreshing a page, or some other way to look for $ SERVER variables

         

csdude55

7:44 pm on Jan 18, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I include a "variables.php" script on every page. One of the first things it does is connect to MySQL, and restarts MySQL if there's an error connecting.

I set db_user and db_pass in Apache configuration to help hide it. So this is how I connect to MySQL:

$dbh = @mysqli_connect('localhost', user_' . $_SERVER['db_user'], $_SERVER['db_pass'], 'database');

if (mysqli_connect_errno()) {
// "mysql_restart.dat" is an empty text file, I just use it to
// limit restarts to once per minute
if (time() - filemtime('/home/account/mysql_restart.dat') > 60) {
mail('me@gmail.com',
'MySQL Restarted',
mysqli_connect_error());

exec('/etc/rc.d/init.d/mysql restart');
touch('/home/account/mysql_restart.dat');
}

include '/nodb.php';
exit;
}


Occasionally, though, Apache doesn't return the variables I need, so I'm getting an error of Access denied for user 'user_'@'localhost' (using password: NO) (meaning, db_user was empty). I'm guessing that this is happening when an SSE times out, but I'm not positive.

At that point, though, MySQL doesn't need to be restarted, the issue is Apache.

My initial thought was this modification:

if ($_SERVER['db_user'] && $_SERVER['db_pass'])
$dbh = @mysqli_connect('localhost', user_' . $_SERVER['db_user'], $_SERVER['db_pass'], 'database');

else {
if (time() - filemtime('/home/account/mysql_restart.dat') > 60) {
mail('me@gmail.com',
'Apache Failed',
'Apache failed, page refreshed');

touch('/home/account/mysql_restart.dat');
}

// refresh the page after 2 seconds
header("Refresh:2");
}

There's a concern of the page getting in a loop and constantly refreshing, though. I thought about sending a param of refresher=1 and only refreshing if that param doesn't exist, but at this point if Apache didn't respond with $_SERVER['db_user'] then I have to assume there would be no $_SERVER['QUERY_STRING'] either.

Any suggestions on how to prevent that loop of refreshing over and over?

Or other suggestions on accomplishing my goal here?

Dimitri

11:10 am on Feb 18, 2023 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



What about storing the MySQL credentials in the php.ini file? and access the values using get_cfg_var : [php.net...]

Also, you should find out why MySQL can be unreachable and needing to be restarted. This is not normal. There is no reason MySQL stops working. If it's a matter of too many connections, then MySQL should not be restarted.

lucy24

7:06 pm on Feb 18, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does MySQL live on the same server as the site itself? (No, I do not know what to do with this information, but it's the first question that occurred to me.)

csdude55

7:12 pm on Feb 18, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What about storing the MySQL credentials in the php.ini file? and access the values using get_cfg_var

I didn't realize that was an option! Thanks for that tip :-) In this case I need the data in Perl, too, but there are other areas that I'm putting variables in Apache that are strictly for PHP, so that opens up some options for me :-)

Also, you should find out why MySQL can be unreachable and needing to be restarted. This is not normal. There is no reason MySQL stops working. If it's a matter of too many connections, then MySQL should not be restarted.

My MySQL is held together with bubblegum and prayers at this point. A few years ago I woke up to the whole database having crashed, and after several hours my server provider was able to narrow it down to an issue with InnoDB, even though I didn't use InnoDB anywhere! The solution was to include innodb_force_recovery=4 in the config, and I lost a LOT of data.

Then awhile later, the database crashed again. This time the solution was to change that to innodb_force_recovery=5.

I've had a dozen techs go through it, and no one can figure out what the actual problem is. So I've been running it on a 5 for awhile. I tried changing it to a 4 again and everything crashed immediately, so I guess that's just how it is now.

I discovered a few times that I would wake up to find that MySQL had been unresponsive for several hours, and restarting it fixed it. I've never figured out why it happens, there's no apparent pattern to it! But this setup has "fixed" the issue of waking up to discover several hours of downtime, anyway.

csdude55

7:13 pm on Feb 18, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does MySQL live on the same server as the site itself?

It does. I have a second smaller server that just stores backups, but I don't access it to read anything anymore.