PHP Frequently Asked Questions

See also: Quakenet #php FAQ.

Contents

Where can I find the manual?

The manual can be found at php.net/manual and alternative versions at php.net/docs.

Where can I find tutorials?

There's a tutorial at php.net/tut. See also the sites listed at php.net/links.

Are there PHP channels for other languages on Quakenet?

Yes. This is the list of known PHP channels on Quakenet. If you know of any others, please contact me and I'll add them (see note at the bottom of the FAQ): #php #php.cz #php.de #php.dk #php.fi #php.fr #php.hu #php.no #php.se #php-doc-da #phpfrance

Can I get the manual to default to my language?

Yes. The PHP site tries to determine your location from your IP address, however it can get this wrong. You can manually set the language to be used at php.net/my.

How can I obtain the visitors IP address?

First of all, it should be noted that there's no 100% reliable way to make sure you obtain a users individual IP address. However, the following code will obtain the IP address where it's available.

<?php
/**
 * @desc Return the IP address of the user as accurately as possible
 * @return string Users IP address or FALSE on failure (highly unlikely, but would occur if you were, for example, to use this in a CLI script)
 */
function getip() {
    
$ip FALSE;
    if ((isset (
$_SERVER['HTTP_CLIENT_IP'])) AND (!empty($_SERVER['HTTP_CLIENT_IP']))) {
        
$ip $_SERVER['HTTP_CLIENT_IP'];
    }

    if ((isset (
$_SERVER['HTTP_X_FORWARDED_FOR'])) AND (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))) {
        
$ips explode (', '$_SERVER['HTTP_X_FORWARDED_FOR']);
        if (
$ip != FALSE) {
            
array_unshift($ips,$ip);
            
$ip FALSE;
        }

        
$count count($ips);
        
// Exclude IP addresses that are reserved for LANs
        
for ($i 0$i $count$i++) {
            if (!
eregi("^(10|172\.16|192\.168)\."$ips[$i])) {
                
$ip $ips[$i];
                break;
            }
        }
    }

    if ((
$ip == FALSE) and (isset ($_SERVER['REMOTE_ADDR']))) {
        
$ip $_SERVER['REMOTE_ADDR'];
    }

    return 
$ip;
}
?>

How can I display results in a table with X results (columns) per row?

Here's an example using 3 results per row:

<?php
/*
 * SNIPPET: Display the results in a table with 3 columns (results) per row
 */

echo '<table>';
$counter 1;
while (
$row mysql_fetch_result ($result)) {
  if (((
$counter 1) % 3) == 0) {
    echo 
'<tr>';
  }
  echo 
'<td>'$row['column'] .'</td>';
  if ((
$counter 3) == 0) {
    echo 
'</tr>';
  }
  
$counter++;
}

// Deal with unclosed rows
if (($counter 3) != 0) {
  echo 
'</tr>';
}

echo 
'</table>';

If you have any additional FAQ's to add here, please contact Athon_Solo on irc.quakenet.org #php. Do NOT PM / msg him directly (those methods are on ignore). Ask for him on the channel politely.

Author: Allen Brooker
Last Updated: 2004-12-05