note 56918 deleted from function.mysql-connect by bjori
| From: | bjori@php.net | Date: | Sat, 15 Apr 2006 18:38:58 +0000 |
| Subject: | note 56918 deleted from function.mysql-connect by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108192@lists.php.net to get a copy of this message | ||
Note Submitter: contact at sonium dot org
----
I had to use php4 (without mysql.connect_timeout) but wanted to check the availability of a database
(without having to wait for 30 seconds).
if you use fsockopen(with timeout) and just close the connection after a few attempts the server
will block your client.
this script logs you on to mysql and gets disconnected by the server (access denied) but it
doesn't block the client for future connections:
<?php
$host = "127.0.0.1";
$port = 3306;
$timeout = 5;
echo "trying to connect to $host on port $port\n";
$testcon = @fsockopen ($host, $port, $errno, $errstr, $timeout);
if(!$testcon)
die("database unavailable: $errno: $errstr\n");
else
{
echo "connection successful under $timeout seconds\n";
echo "quit connection\n";
$in = fread($testcon,4096);
echo "server says:$in\n";
//login string
$out = array( 0x26,0x00,0x00,0x01, //packet length 38 bytes (without this 4 byte header // packet
no.1
0x85,0xa6,0x03,0x00,
0x00,0x00,0x00,0x01,
0x08,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,
0x66,0x69,0x66,0x69,0x00, //username 'fifi'
0x00); //no password
foreach($out as $o)
{
fputs($testcon,chr($o),1);
}
$in = fread($testcon,4096);
echo "server says:$in\n";
//logout command
$out = array(0x01,0x00,0x00,0x00,0x01);
foreach($out as $o)
fputs($testcon,chr($o),1);
fclose($testcon);
}
?>