PHP Knowledge Base Update -- June 30th, 1999

From: Date: Thu, 01 Jul 1999 04:33:57 +0000
Subject: PHP Knowledge Base Update -- June 30th, 1999
Groups: php.kb 
Request: Send a blank email to php-kb+get-4@lists.php.net to get a copy of this message
Sorry for everyone who was inconvenienced when the links I published yesterday weren't working for a few hours. Please be patient me while I smooth things out. Things should be working better today. I am still not distributing the recently edited knowledge base entries yet. There were quite a few today. I haven't written the code to pull them out of the database and into this mail yet. Remember, it's easy to contribute! Please share you knowledge at: http://www.e-gineer.com/phpkb Cheers, Nathan ------------------------------------------------------------ Do I have to escape square brackets in a regular expression? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/270 ------------------------------------------------------------ Teo::home The square brackets are used in a regular expression to denote a range of possible characters. For example: [gKe5] - will match any of g, K, e, 5 [^gKe5] - will match any character other than g, K, e, 5 If you want to match a square bracket itself then you must escape it in the regular expression. "\[" - will match the character [ "\[\]" - will match the character [ then the character ] One more example for clarity: "\[[gKe5][^gKe5]\]" - will match [; then one of g, K, e 5; then anything other than g, K, e, or 5; then the character ] ------------------------------------------------------------ What is the best way to store credit card information? Should I email credit card information to myself? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/271 ------------------------------------------------------------ Stephen Benjamin The best way is to store credit card information is in a database and access those results over a SECURE connection -- either through the Web or using Secure Shell (SSH). If you e-mail them to yourself unencrypted then your secure server is USELESS. E-mail is about as secure as sending credit card numbers on a post card. Encrypt your mail with PGP or maybe even the mcrypt library and decrypt it when it comes to your mailbox. ------------------------------------------------------------ How can I strip certain HTML tags from a string? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/273 ------------------------------------------------------------ Steve Edberg Check out the Message Filter class at: http://px.sklar.com/code.html?code_id=145 ------------------------------------------------------------ Can I append to a file using FTP through fopen()? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/274 ------------------------------------------------------------ Jim Winstead fopen() cannot be used to append to a file through FTP. Nobody has written the code to support append mode via ftp. The correct work-around would be to add support to PHP for it. Shouldn't be difficult, since ftp does have an APPEND command. http://www.php.net/manual/function.fopen.php3 ------------------------------------------------------------ Can I load a different php3.ini file depending on a directive from the httpd.conf file? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/275 ------------------------------------------------------------ Jim Winstead No. The ini file is only parsed at startup, so you wouldn't be able to control this on a per-virtual host or per-directory basis, anyways. ------------------------------------------------------------ How can I run all form variables through a function automatically? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/276 ------------------------------------------------------------ Steve Douville The following code will run all my_func for all POST form variables: while(list($key,$value)= each($HTTP_POST_VARS)){ $$key = $value; my_func($$key); } ------------------------------------------------------------ Can I reference arrays without quotes? Does PHP support the use of bare words? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/282 ------------------------------------------------------------ Jim Winstead, Matthew Hagerty Bare words (ie: words used in code without quotes) will work as long as none of the bare words collides with something else. This is similar to the 'bare word' support in perl. The use of bare words is not a recommended programming practice in PHP. Example: $a["one"] = 1; $a["two"] = 2; Can also be written in the bare words form as: $a[one] = 1; $a[two] = 2; You can also use bare words any time that a simple string constant is required. For example: $foo = bar; will work, but $foo = bar none; won't work. In the second case the space (or any character other than a simple letter or number) means that PHP cannot parse the bare word so quotes are required: $foo = 'bar none'; ------------------------------------------------------------ Can I make an assignment to $this in a class method? How can I reset or clear the value of an object? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/284 ------------------------------------------------------------ Mark, Steve Dicks The complete '$this' pointer should be considered read-only in methods (you are attempting to overwrite yourself, never a good thing). If you tried this is any other language it is unlikely to work either. Here is an example where the clear() method will cause problems in PHP: class Ob { var $i = 1; function set_i ($a) { $this->i = $a; } function clear () { $this = new Ob; } } If you want to clear an object, or any case where you might be tempted to assign a value to the $this pointer, you should just set the values of the variables contained in the $this object. So the correct solution for the bad class example above is: class Ob { var $i = 1; function set_i ($a) { $this->i = $a; } function clear () { $this->i = 1 } } ------------------------------------------------------------ What does the error "input in flex scanner failed" mean? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/286 ------------------------------------------------------------ Jim Winstead The "input in flex scanner failed" error is generated when a call to: require($path) is made with $path specifying a directory and not a file. http://www.php.net/manual/function.require.php3 ------------------------------------------------------------ Does PHP support COM or DCOM? Does PHP support JAVA? Does PHP support CORBA? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/287 ------------------------------------------------------------ Zeev Suraski PHP 3.0 supports a very limited aspect of COM, in a very ugly notation (it's probably not practically-useable). PHP 4.0, on the other hand, takes advantage of Zend's new OO syntax overloading to implement a full-featured COM module, that allows easy OO access to COM (or DCOM) objects that are scriptable. We'll be looking into Java and CORBA support in the future. ------------------------------------------------------------ Can I create an image by pasting together other images? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/290 ------------------------------------------------------------ Phil Harris How about something like this, it works and may give you some ideas. It basically takes three images and 'pastes' them together with some text to produce a single button, it also does some calculation to work out the correct size etc. <?php if (!isset($text)){ $text="this is a test"; } $fontsize=11; $angle=0; $fontfile="c:/winnt/fonts/tahomabd.ttf"; $leftbit=imagecreatefromgif("support_image/emptyleft.gif"); $rightbit=imagecreatefromgif("support_image/emptyright.gif"); $middlebit=imagecreatefromgif("support_image/emptymiddle.gif"); $size=imagettfbbox( $fontsize, $angle, $fontfile, ' ' . stripslashes($text) . ' '); $twidth=((int)$size[4]-(int)$size[0]); $htwidth=$twidth/2; if (($twidth)<69){ $twidth=69; } $bwidth=$twidth+11; $height=((int)$size[3]-(int)$size[1]); $target=imagecreate($bwidth,30); imagecopyresized($target,$leftbit ,0,0,0,0,6,30,6 ,30); imagecopyresized($target,$middlebit,6,0,0,0,$bwidth,30,69,30); imagecopyresized($target,$rightbit ,(int)$twidth+6,0,0,0,5,30,5 ,30); $white = ImageColorAllocate($target,255,255,255); $black = ImageColorAllocate($target,0,0,0); imagettftext( $target, $fontsize, $angle, ((int)$bwidth/2)-((int)$htwidth)+2, (int)$height/2+17, $white, $fontfile, stripslashes($text)); Header( "Content-type: image/gif"); imagegif($target); #print $bwidth . "<br>" . "$twidth"; ?> ------------------------------------------------------------ Do I need to put CR and LF after HTTP protocol elements? Do HEAD and GET HTTP requests require a CR and an LF? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/291 ------------------------------------------------------------ Jim Winstead, Jeremy Brand HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements except the entity-body (see appendix 19.3 for tolerant applications). (19.3 recommends that applications recognize a single LF.) So the answer should be "always use \r\n". Any HTTP/1.1 server that doesn't accept it violates the standard. (Essentially the same text appears in the HTTP/1.0 spec.) So the following may not work as expected: fputs($sock,"HEAD / HTTP/1.0\n"); You should use \r\n instead: fputs($sock,"HEAD / HTTP/1.0\r\n"); If you do not use \r\n then the HTTP server may not recognise the request and your script will wait for the request to the server to timeout. This may give the appearance of hanging if the HTTP server does not handle timeouts properly. ------------------------------------------------------------ How can I work out the size of an image from within PHP? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/293 ------------------------------------------------------------ Øystein Kristoffer Paulsen The size of an image is given by the getimagesize() function. http://www.php.net/manual/function.getimagesize.php3 ------------------------------------------------------------ How can I print a table of all the global variables in my PHP script? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/294 ------------------------------------------------------------ Chris Lott You can use something like the following to dump all the global variables into a HTML table: <TABLE CELLPADDING="5" BORDER="1"> <TR><TH ALIGN="LEFT">Variable</TH><TH ALIGN="LEFT">Value</TH></TR> <? while (list($key, $value) = each($GLOBALS)) { if (strtolower($key) != "globals") { echo "<TR><TD><I>" . $key . "</I></TD><TD>$value"; if (strlen($value) == 0) { echo "&nbsp;"; } echo "</TD></TR>\n"; } } ?> </TABLE> ------------------------------------------------------------ How can I get the disk space used by all files in a directory tree? What is the unix command du used for? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/296 ------------------------------------------------------------ M.Brands The unix command "du" is used to get the disk usage. Try using: du -ks <name> For example: du -ks /tmp returns: <size_of_tmp_and_all_the_stuff_below_it_in_kilobytes> On the unix command line do: man du for more information. ------------------------------------------------------------ Where is the php3.ini configuration file on a unix server? What is the path to my php3.ini file? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/298 ------------------------------------------------------------ Jon Parise It's definable at compile-time, so it could, theoretically, be anywhere. You can find out for sure by creating a simple page containing: <?php phpinfo(); ?> Below the "Configuration" heading is the path to php3.ini. ------------------------------------------------------------ Is there a PHP library for drawing graphs? Is there a PHP library for drawing charts? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/301 ------------------------------------------------------------ Robbin Zhang Try using the graph and chart library at: http://www.vhconsultants.com/graph.htm ------------------------------------------------------------ Does uniqid still generate a unique id on SMP boxes? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/303 ------------------------------------------------------------ Jim Winstead Theoretically uniqid may not generate a unique id on SMP boxes. Realistically though, it will. You could work around this by using the process id (getmypid()) as part of the prefix. Apache's mod_unique_id also uses an algorithm that avoids this problem, so that if that is enabled on your server, you can simply use $UNIQUE_ID. ------------------------------------------------------------ Can I close a persistent connection? Are persistent connections ever closed? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/304 ------------------------------------------------------------ Paul DuBois, Chad Cunningham, Nathan Wallace You cannot close a persistent connection in PHP. Closing a persistent link in a script defeats the purpose of having a persistent link. If you want to close it, just open the connection with mysql_connect() so that you can close it. One persistent connection is established for each Apache child process the first time that the database connect is called in the lifetime of that process. For more info about persistent connections see: http://www.php.net/manual/features.persistent-connections.php3 In Apache the child process only serves a certain number of requests before it is killed and a new child started. When the process is killed the connection to the database that it held will be closed. Hence, your database connections do have a finite lifetime. Here is an excerpt from: http://www.apache.org/docs/mod/core.html#maxrequestsperchild The MaxRequestsPerChild directive sets the limit on the number of requests that an individual child server process will handle. After MaxRequestsPerChild requests, the child process will die. If MaxRequestsPerChild is 0, then the process will never expire. Setting MaxRequestsPerChild to a non-zero limit has two beneficial effects: * it limits the amount of memory that process can consume by (accidental) memory leakage; * by giving processes a finite lifetime, it helps reduce the number of processes when the server load reduces. ------------------------------------------------------------ Why is odbc_num_rows always returning -1? How can I work around the problem where odbc_num_rows always returns -1? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/306 ------------------------------------------------------------ Keith Greene If you check out http://www.php.net/manual/function.odbc-num-rows.php3 you will find a note that reads: Note: Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers. This unfortunately happens to be the case with Access, and MS SQL I believe. The workaround I use is this: $connect = odbc_connect("your_db", "user", "pass"); $result = odbc_exec($connect,"select * from your_table"); $count=0; while($temp = odbc_fetch_into($result,&$counter)){ $count++; } $count will contain the number of rows for that particular select statement. You can also use ODBC_RESULT in place of the ODBC_FETCH_INTO. ------------------------------------------------------------ Is function_exists() case sensitive? Are function names case sensitive in PHP? The function MyTest exists, why does function_exists("MyTest") return false? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/308 ------------------------------------------------------------ Christopher W. Curtis, Brian Schaffner, Jim Winstead The function_exists() function looks for the function name exactly as you type it (case sensitive). Unfortunately, all function names are stored internally in lower-case. So, MyTest() doesn't exist, but mytest() does. This is a bug in PHP versions earlier than version 3.0.8. Just to illustrate, the following will not work as expected: <?php Function MyTest( ) { } $var = "MyTest"; if( function_exists( $var )) echo "yes via var<p>"; if( function_exists( "MyTest" )) echo "yes via string<p>"; echo "Done.\n"; ?> For correct results, change the above to this: <?php Function MyTest( ) { } $var = "MyTest"; if( function_exists(strtolower($var))) echo "yes via var<p>"; if( function_exists(strtolower("MyTest"))) echo "yes via string<p>"; echo "Done.\n"; ?> and you should get the expected output.

« previous php.kb (#4) next »