PHP Knowledge Base Update -- July 1st, 1999

[email protected] (Nathan Wallace) Fri, 02 Jul 1999 15:33:36 +0000
Newsgroups php.kb
Organization Synop Software
Message-ID <[email protected]>
I'm no expert on most of these things, I just try to determine the best
answer from the information given on the list. Please make corrections
to any entries that need work.  Just follow the link to the entry and
click Edit (top right hand corner).  It's that easy!

Cheers,

Nathan


------------------------------------------------------------
How can I generate and update an Apache htpasswd from PHP?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/312
------------------------------------------------------------
M.Brands

Take a look at src/support/htpasswd.c in the Apache distro. Especially
the function mkrecord is interesting. Also, take a look at
src/ap/ap_md5c.c for more information on how to encode a password. You
should be able to figure it out and do it completely in PHP, since PHP
has all the necessary functions (MD5 encryption and the standard crypt()
function). You probably need to encode the password in apache's own
format (the to64 function in both ap_md5c.c and htpasswd.c).


------------------------------------------------------------
How can I send mail notifying the webmaster from a 404 Not Found error
page?
What environment variable holds the missing url when Apache redirects to
the 404 page?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/313
------------------------------------------------------------
Chris Lott, Peter, Ron Smith

When Apache cannot find the page specified by the URL it redirects the
request to the error document as specified in the httpd.conf file.  When
this happens the environment variable REQUEST_URI (accessible as
$REDIRECT_URI in PHP) holds the requested (but not found) URL.

Be careful though, because if the error document directive in the
httpd.conf points to an http:// doc, the redirect variables are lost.
Use a local file ref and everything should work fine, returning redirect
environment variables as expected.

Sending mail to the webmaster notifying them of the error is simple if
you just specify your error document to be a PHP script.  To report the
URL that is missing just include a reference to $REDIRECT_URI in the
message.

Sending mail on every 404 request can end up placing a fair burden on
the inbox of the webmaster.  An effective work around is to create a
page where the user can just click a button to send a message to the web
master reporting the error.  That way, requests for missing images and
so on will not be reported.  Here is the code to return the button and
mail when clicked:

<script language="php">
if (eregi("(form)",$state)) {
    echo "<font size=\"5\">Thank You!</font>\n";
    mail(
        "[email protected]",
        "Busted Link!",
        "Link: $referer\n Using: $useragent\n IP:$ip\n Who: $user");
}
else {
    echo "<form method=\"post\" action=\"notfound.phtml\">\n";
    echo "<input type=hidden name=state value=form>\n";
    echo "<input type=hidden name=referer value=\"$HTTP_REFERER\">\n";
    echo "<input type=hidden name=useragent
value=\"$HTTP_USER_AGENT\">\n";
    echo "<input type=hidden name=ip value=\"$REMOTE_ADDR\">\n";
    echo "<input type=submit value=\"Click here to notify the Webmaster
about the missing page\">\n";
    echo "</form>\n";
}
</script>


------------------------------------------------------------
How can I send a 204 No Content header back to the client?
What is the 204 No Content header used for?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/315
------------------------------------------------------------
Steven Champeon, Stefan Paletta, Alex Belits

Try using the following code, before outputting anything else back to
the client.

    <?php
    header("HTTP/1.0 204 No Content");
    ?>

For more information on the header() function:

    http://www.php.net/manual/function.header.php3

An example of the use of the header 204 No Content would be to allow a
multi-screen DHTML form to be submitted without refreshing the browser
window.


------------------------------------------------------------
How do I use Oracle Reference Cursors in PHP?
Does PHP support the use of Oracle Cursors?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/317
------------------------------------------------------------
Thies C. Arntzen

Oracle Reference Cursors work with the oci functions.

    http://www.php.net/manual/ref.oci8.php3

For an example, suppose your stored proc 

    info.output 

returns a refcursor in 

    :data 

Here is the PHP code:

    <?php
    $conn = OCILogon("digicol","digicol");

    $curs = OCINewCursor($conn);

    $stmt = OCIParse($conn,"begin info.output(:data); end;");

    ocibindbyname($stmt,"data",&$curs,-1,OCI_B_CURSOR);

    ociexecute($stmt);

    ociexecute($curs);

    while (OCIFetchInto($curs,&$data)) {
        var_dump($data);
    }

    OCIFreeCursor($stmt);
    OCIFreeStatement($curs);
    OCILogoff($conn);
    ?>

to use cursors in selects do:

    <?php
    //ociinternaldebug(1);
    $conn = OCILogon("digicol","digicol");

    //$curs = OCINewCursor($conn);

    $stmt = OCIParse($conn,
        "select user_id,user_name, CURSOR(
            select count(ses_id) from sessions where
            ses_user_id = user_id) as tubu from users");

    //ocidefinebyname($stmt,"tubu",&$curs,-1,OCI_B_CURSOR);

    ociexecute($stmt);
    while (OCIFetchInto($stmt,&$data,OCI_ASSOC)) {

        echo $data[ "USER_NAME" ]." id=".$data[ "USER_ID" ]."\n";

        ociexecute($data[ "TUBU" ]);
        while (OCIFetchInto($data[ "TUBU" ],&$subdata,OCI_ASSOC)) {
            echo "    ".$subdata[ "SES_ID" ]." - ".
                                          $subdata[ "SES_HOST" ]."\n";
        }
        echo "\n";

    }

    //OCIFreeCursor($stmt);
    //OCIFreeStatement($curs);
    //OCILogoff($conn);
    ?>


------------------------------------------------------------
Which one is faster to connect to the database, connect or pconnect?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/319
------------------------------------------------------------
Chad Cunningham, Kristian Köhntopp

If you're in a situation where pconnect offers no benefit at all (i.e.
using php cgi) then there is more overhead when connecting which will
never pay off (pconnect must first check for an existing connection
before creating a new one, and if there is never an existing connection,
you're doing a step that doesn't need to be done).

That overhead is just a single hash table lookup. And after looking at
it, it's just a matter of checking the persistent list vs the normal
list.

So there shouldn't be any performance difference at all.  Because of the
benefits that pconnect brings by not having to reconnect to the database
each time (where possible) there is no reason why it shouldn't be used
all the time.

For more information see:

    http://www.php.net/manual/features.persistent-connections.php3

    http://www.php.net/manual/function.mysql-connect.php3

    http://www.php.net/manual/function.mysql-pconnect.php3


------------------------------------------------------------
Can I rely on mysql_insert_id when using persistent database
connections?
Are there any problems to be aware of when coding for persistent
database connections?
Do I need to treat persistent database connections differently?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/320
------------------------------------------------------------
Mattias Nilsson

Persistent database connections are allocated to a httpd child process. 
They are not shared between processes.  So you are guarenteed that a
single database connection will only be used by a single executing
script at any time.  Of course, the same process and connection may be
used by a number of scripts one after the other.

This means that you can always rely on the results of functions like
mysql_insert_id().

Basically there is absolutely no difference in PHP between the use of
database connections and persistent database connections.


------------------------------------------------------------
What are some good PHP editors for Linux?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/323
------------------------------------------------------------
Keith Edmunds, Chris Lott, Mattias Nilsson, Chris Schwan

Try Vi IMproved.  It has syntax highlighting which can even handle HTML,
PHP and SQL inside strings all in the same file!  It has command line or
graphical modes.

    http://www.vim.org

nedit is another worth trying.

Emacs works wonderfully, with C-mode or HTML-helper-mode.


------------------------------------------------------------
How can I change the HTTP header that is sent back to the client?
Can I send a HTTP header code other than 200 from PHP?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/324
------------------------------------------------------------
Mattias Nilsson

Just use the header() function.

    http://www.php.net/manual/function.header.php3

Make sure that you call header() before any data (even whitespace) has
been sent back to the client.


------------------------------------------------------------
Which one is faster for looping, for or while?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/326
------------------------------------------------------------
Rasmus Lerdorf

While is marginally faster than for.  But it probably isn't measurable
so I wouldn't worry about it if I were you.


------------------------------------------------------------
Why does my browser say "Data Missing" when I use the back button?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/327
------------------------------------------------------------
matthew mcglynn

The "Data Missing" error is caused by HTTP expires and no-cache headers.

Perhaps a better solution is to set them so that data is cached for a
short period of time -- longer than "zero" but not permanent either.

Of course, for a dynamic site in which individual customized pages are
served to individual browsers, you can't allow caching or you risk that
surfer B sees surfer A's pages because those pages were cached by a
proxy somewhere.

If your problem is that a script that receives a form shows error
messages and ends with "now click BACK to correct the form," the problem
is that that is just a lame design. The solution is to always move
forward, never back, and then you don't have caching problems such as
you've described. Instead of showing error messages with a request that
the user back up, just show the form again and highlight the fields that
have problems. This provides a superior user experience anyway.


------------------------------------------------------------
Why is my source code patch file rejected?
What is the patch program?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/328
------------------------------------------------------------
Kim Shrier

The patch program looks at the patch file and makes sure that it matches
the file being patched.  

For example, a patch file might contain 2 changes where the first one
changes a CVS revision ID line, and the second one deletes a line
containing "free(field_name);".

The lines that change are preceeded by a + or - to indicate if the
specified line should be inserted or deleted.

If your source code patch file is being rejected, you should look at the
patch file and then look at the source file and see if they match up.


------------------------------------------------------------
Which is better to use fsockopen() or mail()?
When should I use fsockopen() instead of mail()?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/330
------------------------------------------------------------
Chad Cunningham

Using fsockopen() to send mail is really just a different way of doing
the same thing. mail() opens a pipe to sendmail and feeds it the
message. fsockopen would probably have more overhead, but people
have said it works better for sending large quantites of messages.

    http://www.php.net/manual/function.mail.php3

    http://www.php.net/manual/function.fsockopen.php3


------------------------------------------------------------
How can I turn all URL's in a string into working links?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/332
------------------------------------------------------------
Michael Stearne

The following regular expression will replace all URL's in a string
($msg) with working links.

$msg = eregi_replace(
    "(http|https|ftp)://([[:alnum:]/\n+-=%&:_.~?]+[#[:alnum:]+]*)",
    "<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>",
    $msg);

Note that this regular expression allows for URL's to extend over more
than one line (\n).  In some cases you might like to remove this
capability since if the URL finishes exactly at the end of a line and 
there are no spaces before the next word that word will be enabled.  For
example, "Enabled" below will be part of the links when \n is included. 
Line breaks are denoted with <CR>.

http://www.somewhere.com<CR>
<CR>
Enabled


------------------------------------------------------------
How do I compile PHP with support for T1Lib?
Why isn't T1Lib automatically installed with PHP if it is detected on
the system?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/333
------------------------------------------------------------
Mike Robinson, Jouni Ahto

T1Lib is not automatically installed if detected, you need to set the
flag in ./configure:

    --enable-t1lib


There is a document in your distribution dir called "README.t1lib".
I strongly urge you to read it. It concerns t1lib versions and required
patches.

There is a *very good* reason for not enabling it by default, but asking
you enable it by writing '--enable-t1lib'. If you are using version 0.9
of t1lib, you *are going to get a serious warning*, and please *fix it*,
compile t1lib after fixing it, or you will have serious problems. Like
your httpd processes just growing and growing in size, until they just
use all the memory and swap you have got on your machine and finally,
depending on your os, either crashing, panickin or just making the
system not responding to anything.


------------------------------------------------------------
What are .afm files used for?
Does t1lib use font metrics files?
Do t1lib fonts use kerning information?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/335
------------------------------------------------------------
Jouni Ahto

It is very important to have .afm files for those PostScript fonts you
are using (it's mentioned in t1lib's docs and everybody using this
interface of PHP3 should really read those doc's too). Unless there are
font metrics files the library can find, it must construct them itself.
It takes some time (my testing says, between 1000-5000 more). And, if
there is kerning information in that .afm file, it will be automagically
used. In fact, there is no such option as 'turn kerning off'.