<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Installing a proxy</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-uh.quickstart.how-it-works.html">How it works</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-uh.quickstart.query-monitoring.html">Basic query monitoring</a></div> <div class="up"><a href="mysqlnd-uh.quickstart.html">Quickstart and Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mysqlnd-uh.quickstart.proxy-installation" class="section"> <h2 class="title">Installing a proxy</h2> <p class="para"> The extension provides two built-in classes: <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a> and <a href="class.mysqlnduhpreparedstatement.html" class="classname">MysqlndUhPreparedStatement</a>. The classes are used for hooking <em>mysqlnd</em> library calls. Their methods correspond to <em>mysqlnd</em> internal functions. By default they act like a transparent proxy and do nothing but call their <em>mysqlnd</em> counterparts. By subclassing the classes you can install your own proxy to monitor <em>mysqlnd</em>. </p> <p class="para"> See also the <a href="mysqlnd-uh.quickstart.how-it-works.html" class="link">How it works</a> guide to learn about the inner workings of this extension. </p> <p class="para"> Connection proxies are objects of the type <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a>. Connection proxy objects are installed by <span class="function"><a href="function.mysqlnd-uh-set-connection-proxy.html" class="function">mysqlnd_uh_set_connection_proxy()</a></span>. If you install the built-in class <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a> as a proxy, nothing happens. It behaves like a transparent proxy. </p> <p class="para"> <div class="example" id="example-1873"> <p><strong>Example #1 Proxy registration, mysqlnd_uh.enable=1</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />mysqlnd_uh_set_connection_proxy</span><span style="color: #007700">(new </span><span style="color: #0000BB">MysqlndUhConnection</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"root"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> The <em>PHP_INI_SYSTEM</em> configuration setting <em><a href="mysqlnd-uh.configuration.html#ini.mysqlnd-uh.enable" class="link">mysqlnd_uh.enable</a></em> controls whether a proxy may be set. If disabled, the extension will throw errors of type <em>E_WARNING</em> </p> <p class="para"> <div class="example" id="example-1874"> <p><strong>Example #2 Proxy installation disabled</strong></p> <div class="example-contents"> <div class="inicode"><pre class="inicode">mysqlnd_uh.enable=0</pre> </div> </div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />mysqlnd_uh_set_connection_proxy</span><span style="color: #007700">(new </span><span style="color: #0000BB">MysqlndUhConnection</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"root"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> PHP Warning: MysqlndUhConnection::__construct(): (Mysqlnd User Handler) The plugin has been disabled by setting the configuration parameter mysqlnd_uh.enabled = false. You must not use any of the base classes in %s on line %d PHP Warning: mysqlnd_uh_set_connection_proxy(): (Mysqlnd User Handler) The plugin has been disabled by setting the configuration parameter mysqlnd_uh.enable = false. The proxy has not been installed in %s on line %d </pre></div> </div> </div> </p> <p class="para"> To monitor <em>mysqlnd</em>, you have to write your own proxy object subclassing <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a>. Please, see the function reference for a the list of methods that can be subclassed. Alternatively, you can use reflection to inspect the built-in <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a>. </p> <p class="para"> Create a new class <em>proxy</em>. Derive it from the built-in class <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a>. Replace the <span class="function"><a href="mysqlnduhconnection.connect.html" class="function">MysqlndUhConnection::connect()</a></span>. method. Print out the host parameter value passed to the method. Make sure that you call the parent implementation of the <em>connect</em> method. Failing to do so may give unexpected and undesired results, including memory leaks and crashes. </p> <p class="para"> Register your proxy and open three connections using the PHP MySQL extensions <a href="ref.mysqli.html" class="link">mysqli</a>, <a href="ref.mysql.html" class="link">mysql</a>, <a href="ref.pdo-mysql.html" class="link">PDO_MYSQL</a>. If the extensions have been compiled to use the <em>mysqlnd</em> library, the <em>proxy::connect</em> method will be called three times, once for each connection opened. </p> <p class="para"> <div class="example" id="example-1875"> <p><strong>Example #3 Connection proxy</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">proxy </span><span style="color: #007700">extends </span><span style="color: #0000BB">MysqlndUhConnection </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">, </span><span style="color: #0000BB">$host</span><span style="color: #007700">, </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$passwd</span><span style="color: #007700">, </span><span style="color: #0000BB">$db</span><span style="color: #007700">, </span><span style="color: #0000BB">$port</span><span style="color: #007700">, </span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">$mysql_flags</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Connection opened to '%s'\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$host</span><span style="color: #007700">);<br /> </span><span style="color: #FF8000">/* Always call the parent implementation! */<br /> </span><span style="color: #007700">return </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">, </span><span style="color: #0000BB">$host</span><span style="color: #007700">, </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$passwd</span><span style="color: #007700">, </span><span style="color: #0000BB">$db</span><span style="color: #007700">, </span><span style="color: #0000BB">$port</span><span style="color: #007700">, </span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">$mysql_flags</span><span style="color: #007700">);<br /> }<br />}<br /></span><span style="color: #0000BB">mysqlnd_uh_set_connection_proxy</span><span style="color: #007700">(new </span><span style="color: #0000BB">proxy</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"root"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$mysql </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"root"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pdo </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">"mysql:host=localhost;dbname=test"</span><span style="color: #007700">, </span><span style="color: #DD0000">"root"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Connection opened to 'localhost' Connection opened to 'localhost' Connection opened to 'localhost' </pre></div> </div> </div> </p> <p class="para"> The use of prepared statement proxies follows the same pattern: create a proxy object of the type <a href="class.mysqlnduhpreparedstatement.html" class="classname">MysqlndUhPreparedStatement</a> and install the proxy using <span class="function"><a href="function.mysqlnd-uh-set-statement-proxy.html" class="function">mysqlnd_uh_set_statement_proxy()</a></span>. </p> <p class="para"> <div class="example" id="example-1876"> <p><strong>Example #4 Prepared statement proxy</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">stmt_proxy </span><span style="color: #007700">extends </span><span style="color: #0000BB">MysqlndUhPreparedStatement </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s(%s)\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /> }<br />}<br /></span><span style="color: #0000BB">mysqlnd_uh_set_statement_proxy</span><span style="color: #007700">(new </span><span style="color: #0000BB">stmt_proxy</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"root"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT 'mysqlnd hacking made easy' AS _msg FROM DUAL"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> stmt_proxy::prepare(SELECT 'mysqlnd hacking made easy' AS _msg FROM DUAL) </pre></div> </div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-uh.quickstart.how-it-works.html">How it works</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-uh.quickstart.query-monitoring.html">Basic query monitoring</a></div> <div class="up"><a href="mysqlnd-uh.quickstart.html">Quickstart and Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>