Submitted by: PHP code exchange px@sklar.com To get this file, in the web-browser, save this file as 'Text' type as pgsql_largeobj.lib
PX: PHP Code Exchange -
<url name="PostgreSQL" url="http://www.geocities.com/alavoor/HOWTO/pgsql/PostgreSQL-HOWTO.html">
large object access
<?
$database = pg_Connect ( "", "", "", "", "jacarta");
pg_exec ($database, "BEGIN");
$oid = pg_locreate ($database);
echo ( "$oid\n");
$handle = pg_loopen ($database, $oid, "w");
echo ( "$handle\n");
pg_lowrite ($handle, "foo");
pg_loclose ($handle);
pg_exec ($database, "COMMIT");
pg_close ($database);
?>
To get this file, in the web-browser, save this file as 'Text' type as user_pw.lib
From the PHP 3 Manual: Works only if PHP is an Apache module. Instead of simply printing out the $PHP_AUTH_USER and $PHP_AUTH_PW, you would probably want to check the username and password for validity. Perhaps by sending a query to a database, or by looking up the user in a dbm file.
<?php
if (!$PHP_AUTH_USER)
{
Header("WWW-authenticate: basic realm=\"My Realm\"");
Header("HTTP/1.0 401 Unauthorized");
echo "Text to send if user hits Cancel button\n";
exit;
}
else
{
echo "Hello $PHP_AUTH_USER.<P>";
echo "You entered $PHP_AUTH_PW as your password.<P>";
}
?>
To get this file, in the web-browser, save this file as 'Text' type as network.lib
PHP: network adminstrator's best friend from http://www.phpWizard.net
As a web-developer, you're probably used to such lovely tools as ping, whois, nslookup etc. But what when you need one of those utilities at a client's office and have no access to telnet? Good guess. Time to look up the functions in the "Network" section of the PHP manual.
Socket operations:
The most important function there is fsockopen(). Using this function, you can connect to any open port on a server and establish a socket connection with it. The function's syntax is as following:
int fsockopen(string hostname, int port, int [errno], string [errstr]);
So, you could use this function to open a connection to a webserver and print out the headers:
function get_headers($host, $path = "/")
{
$fp = fsockopen ("$host", 80, &$errnr, &$errstr) or die("$errno: $errstr");
fputs($fp,"GET $path HTTP/1.0\n\n");
while (!$end)
{
$line = fgets($fp, 2048);
if (trim($line) == "")
$end = true;
else
echo $line;
}
fclose($fp);
}
Finger: Naturally, you can also open connections to other ports. Writing a small finger client with PHP is trivial therefore. Let's change the example from above to query a finger daemon:
function finger ($host, $user)
{
$fp = fsockopen($host, 79, &$errno, &$errstr) or die("$errno: $errstr");
fputs($fp, "$user\n");
while (!feof($fp))
echo fgets($fp, 128);
fclose($fp);
}
Whois: Querying a whois server uses the same concept:
// domain is like "phpwizard.net"
function whois($domain, $server="whois.internic.net")
{
$fp = fsockopen ($server, 43, &$errnr, &$errstr) or die("$errno: $errstr");
fputs($fp, "$domain\n");
while (!feof($fp))
echo fgets($fp, 2048);
fclose($fp);
}
Blocking and non-blocking operations: But there's a problem with all those functions. They work fine if
$fp = fsockopen($host, 79, &$errno, &$errstr) or die("$errno: [ ] $errstr");
set_socket_blocking($fp, 0);
fputs($fp, "$user\n");
$stop = time() + $timeout;
while (!feof($fp) && time() < $stop )
echo fgets($fp, 128);
fclose($fp);