PHP Knowledge Base Update -- July 6th, 1999

From: Date: Wed, 07 Jul 1999 14:26:00 +0000
Subject: PHP Knowledge Base Update -- July 6th, 1999
Groups: php.kb 
Request: Send a blank email to php-kb+get-10@lists.php.net to get a copy of this message
Today, Brad Marsh pointed out http://usefulinc.com/xmlrpc/. This is a PHP implementation of the XML-RPC format devised by http://www.userland.com. Currently only client libraries are available, but according to Jim Winstead the latest CVS code for PHP adds functionality that will make server classes possible soon. Cheers, Nathan ------------------------------------------------------------ What is the OR operator in PHP? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/411 ------------------------------------------------------------ Jason Brooke You can use either || or the keyword 'or'. For more information see: http://www.php.net/manual/language.operators.logical.php3 Be careful though, these different operators have different precedence levels: http://www.php.net/manual/language.operators.precedence.php3 ------------------------------------------------------------ How can I run Apache and PHP in a chroot environment? Can I connect to MySQL when running in a chroot environment? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/412 ------------------------------------------------------------ Kim Shrier It is fairly easy to get Apache with PHP running in a chrooted environment. I give the chrooted web server its own IP address, that way, the Apache server can be bound to its own address and only serve those requests. You will need to copy all the Apache files into the chrooted area as well as any dynamic libraries you might need. Once Apache is running chrooted, it doesn't know about the real file system anymore so all the directives in httpd.conf which specify file names have to be relative to the new root. As far as connecting to mysql is concerned, you just connect to it in the normal way by specifying the IP address of the mysql server. For example, I use a setup similar to this: new root: /var/virtual Apache files: /var/virtual/usr/local/apache Apache config: /var/virtual/usr/local/apache/conf/httpd.conf Apache daemon: /var/virtual/usr/local/apache/libexec/httpd dynamic libs: /var/virtual/usr/lib sendmail: /var/virtual/usr/sbin/sendmail document root: /var/virtual/var/www/htdocs cgi-bin: /var/virtual/var/www/cgi-bin php files: /var/virtual/var/www/php icons: /var/virtual/var/www/icons If you are going to be using the exec, system, passthru, or mail functions, you will need a shell in the chrooted area: shell: /var/virtual/bin/sh You will need some files in /etc like resolv.conf, protocols, and localtime so that your chrooted programs will know how to look up domain names and know what time it is. You will also need a password file and group file, and I strongly recommend that all the password fields be set to "*" to prevent people from being able to log in to the chrooted environment. Also, I recommend that you take everything out of the passwd file you don't need so that it only has entries for root, bin, daemon, ftp, nobody, and any other users that you need. If you will be using sendmail, you will need a sendmail.cf file in /etc as well. So your chrooted /etc will have: /var/virtual/etc/group /var/virtual/etc/localtime /var/virtual/etc/passwd /var/virtual/etc/protocols /var/virtual/etc/resolv.conf /var/virtual/etc/sendmail.cf /var/virtual/etc/services So, assuming that you have apache compiled to know where his configuration file is (/usr/local/apache/conf/httdp.conf), you can start up the server with the following command: chroot /var/virtual /usr/local/apache/libexec/httpd For uploading files, use the chroot capability that is built into your FTP server. I usually set up special users that don't have real shells so they cannot telnet into the server. I also specify their home directory to be /var/virtual/var/www/htdocs. The FTP daemon will chroot them to their home directory so that they can only get access to the web pages. If you trust these users and they need to be able to upload cgi's or php scripts then you would set their home directory to /var/virtual/var/www. However, since FTP passwords go across the net in clear text, you are vulnerable to sniffers. ------------------------------------------------------------ What's the best way to return a random row from a table? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/418 ------------------------------------------------------------ Adam Whitehead, Nathan Wallace, Basil Hussain There are a few ways to choose a row at random from a table. You can: - Connect to the database and do a count(*) on the table. - Use the result of count(*) to generate a random integer between 0 and count(*)-1 in PHP3. - select * from <table> where ID=<the random value> This method needs to be in a while loop if some id's may be empty, otherwise it may return no rows. Just keep looping until you actually manage to select a row that exists. This may result in multiple selects (when no rows are found) in some cases. Alternatively you could just select 1 rows with the smallest ID >= the random one. It's important to use the smallest ID to keep results random. Otherwise your DB could just always return the biggest ID in the table. This requires a sort on the primary key (which is probably already sorted anyway). Here is a code snippet for another technique: // Seed random number generator. srand((double)microtime()*1000000); // Get number of rows returned. $rows_found = mysql_num_rows($result); if($rows_found > 0) { // Generate a random number up to the number of records returned $rand_row = rand(1, $rows_found); // Seek to randomly chosen record and get it's contents. mysql_data_seek($result, $rand_row - 1); $row = mysql_fetch_array($result); // Do stuff with randomly chosen row here... } This is for choosing a random record from any found set. It simply counts how many records that were found, and seeks to a random one using mysql_data_seek(). It doesn't depend on any kind of ID field, etc. Also, the matter of non-existant rows doesn't come into it, as the found set obviously can't contain rows that don't exist. ------------------------------------------------------------ Do included files have to have PHP tags? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/420 ------------------------------------------------------------ Keith Edmunds An included file is interpreted as a normal PHP script. It can include HTML and PHP tags. An file is included using this syntax: <?php include("myfile.inc"); ?> It is tempting to think that the included file should contain PHP code with no tags. In fact the include operation actually looks like this: <?php ?><contents of myfile.inc><?php ?> So you do have to put <?php ?> tags around your php code in the included file. ------------------------------------------------------------ How can I write to a file on the PHP server? How can I write the contents of a variable to a file? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/421 ------------------------------------------------------------ dbrogdon@sagenetworks.com, Teodor Cimpoesu Write the string $test to a file: $fp = fopen("file.txt", "w"); // Open a file pointer fputs($fp, "\$test"); // Litterally write "$test" // to file.txt fclose($fp); // Close the file pointer Write the contents of the variable $test: $fp = fopen("file.txt", "w"); // Open a file pointer fputs($fp, $test); // Write the value of $test // to file.txt fclose($fp); Make sure that the user that you web server is running as has the proper rights to write to the file. ------------------------------------------------------------ Does PHP have multiple inheritence? Can a class have two parents? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/423 ------------------------------------------------------------ Kristian Köhntopp PHP is single inheritance. That is, you cannot do parent_a parent_b | | child but you can do parent_b | parent_a | child ------------------------------------------------------------ Can I install PHP along with mod_ssl? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/425 ------------------------------------------------------------ Rasmus Lerdorf, Antoni Pàmies Olivé Make sure you add mod_ssl first and then do a fresh configure/install of PHP. The SSL stuff fiddles with the internal httpd structs and PHP needs to know about these. You will need to set up your http.conf and configure ssl (certificate) SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key the files /etc/httpd/conf/ssl.crt/server.crt /etc/httpd/conf/ssl.key/server.key must exists. Check that you have these files and check their contents. ------------------------------------------------------------ Why does my content in angle brackets disappear? How can I escape HTML so it will be displayed to the user by the browser? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/426 ------------------------------------------------------------ Manuel Lemos Your content in angle brackets does not disappear or get eaten. It's there but your browser doesn't show it because it assumes it is a an HTML tag even if it doesn't recognize it. You may use either PHP's HtmlSpecialChars() or HtmlEntities() functions to escape < > as < and > to make browsers take them literally. http://www.php.net/manual/function.htmlspecialchars.php3 ------------------------------------------------------------ Could I use PHP to send a news letter? Can PHP be used to do mass mail outs? http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/428 ------------------------------------------------------------ Sander Pilon Doing mass mail outs in PHP is a bad idea. Why? Two reasons: 1) PHP timeout limit (can be beaten using ignore_user_abort) 2) Mailing list software optimized for mass-mailing is available. (Majordomo, Listserver, whatever... should be easy to find.) Can it be done? Yes. How? Call mail() 14000 times or open a fsock() connection to a SMTP server and 'manually' deliver the mail with SMTP. Will this be slow? Yes. How slow? "I hope you send out this mailinglist only once a month"-slow. Specifically, mailing list software optimizes the order of mailing (combines mails to the same domain and delivers them in a row to the same mailserver.) What you want (well, what I'd want) if you want to send a lot of mail is a multithreaded program sending mails directly to remote mailservers, using an optimized order. 14000 times mail() in PHP sounds dreadfully slow to me. I have a small list (only 500), I wrote a php frontend and a C program to actually send out the mails. (Using MySQL as storage.) Anyway, you have to order this way: email address --> domain name. (user@domain) domain name + DNS MX records --> mailserver address group all mail that belongs at a specific mailserver. Or you could just figure out a way to add 14000 mails to the sendmail queue and let sendmail figure it out. The cute thing about doing it manually would be that you can easily detect invalid target addresses and can take appropriate action. (Remove / Flag)

« previous php.kb (#10) next »