<!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>Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.socket-get-option.html">socket_get_option</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-getsockname.html">socket_getsockname</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.socket-getpeername" class="refentry"> <div class="refnamediv"> <h1 class="refname">socket_getpeername</h1> <p class="verinfo">(PHP 4 >= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">socket_getpeername</span> — <span class="dc-title">Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type</span></p> </div> <div class="refsect1 description" id="refsect1-function.socket-getpeername-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>socket_getpeername</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$address</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$port</code></span> ] )</div> <p class="para rdfs-comment"> Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type. </p> </div> <div class="refsect1 parameters" id="refsect1-function.socket-getpeername-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">socket</code></em></span> <dd> <p class="para"> A valid socket resource created with <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span> or <span class="function"><a href="function.socket-accept.html" class="function">socket_accept()</a></span>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">address</code></em></span> <dd> <p class="para"> If the given socket is of type <strong><code>AF_INET</code></strong> or <strong><code>AF_INET6</code></strong>, <span class="function"><strong>socket_getpeername()</strong></span> will return the peers (remote) <em class="emphasis">IP address</em> in appropriate notation (e.g. <em>127.0.0.1</em> or <em>fe80::1</em>) in the <em><code class="parameter">address</code></em> parameter and, if the optional <em><code class="parameter">port</code></em> parameter is present, also the associated port. </p> <p class="para"> If the given socket is of type <strong><code>AF_UNIX</code></strong>, <span class="function"><strong>socket_getpeername()</strong></span> will return the Unix filesystem path (e.g. <em>/var/run/daemon.sock</em>) in the <em><code class="parameter">address</code></em> parameter. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">port</code></em></span> <dd> <p class="para"> If given, this will hold the port associated to <em><code class="parameter">address</code></em>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.socket-getpeername-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. <span class="function"><strong>socket_getpeername()</strong></span> may also return <strong><code>FALSE</code></strong> if the socket type is not any of <strong><code>AF_INET</code></strong>, <strong><code>AF_INET6</code></strong>, or <strong><code>AF_UNIX</code></strong>, in which case the last socket error code is <em class="emphasis">not</em> updated. </p> </div> <div class="refsect1 notes" id="refsect1-function.socket-getpeername-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> <span class="function"><strong>socket_getpeername()</strong></span> should not be used with <strong><code>AF_UNIX</code></strong> sockets created with <span class="function"><a href="function.socket-accept.html" class="function">socket_accept()</a></span>. Only sockets created with <span class="function"><a href="function.socket-connect.html" class="function">socket_connect()</a></span> or a primary server socket following a call to <span class="function"><a href="function.socket-bind.html" class="function">socket_bind()</a></span> will return meaningful values. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> For having <span class="function"><strong>socket_getpeername()</strong></span> to return a meaningful value, the socket it is applied upon must of course be one for which the concept of "peer" makes sense. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.socket-getpeername-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.socket-getsockname.html" class="function" rel="rdfs-seeAlso">socket_getsockname()</a> - Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type</span></li> <li class="member"> <span class="function"><a href="function.socket-last-error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - Returns the last error on the socket</span></li> <li class="member"> <span class="function"><a href="function.socket-strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - Return a string describing a socket error</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.socket-get-option.html">socket_get_option</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-getsockname.html">socket_getsockname</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>