Home  |  Linux  | Mysql  | PHP  | XML
From:Nathan Wallace Date:Mon May 15 21:59:29 2000
Subject:[FAQTS] PHP Knowledge Base Update -- May 15th, 2000
Thanks to everyone who has been contributing to the Knowledge Base.  We
now have complete, human edited, categorized, searchable answers to more
than 1000 PHP questions!!!

    http://php.faqts.com

cheers,

Nathan


## Unanswered Questions ########################################


-------------------------------------------------------------
PHP under Linux to IBM db2 on Linux
http://www.faqts.com/knowledge-base/view.phtml/aid/2846
-------------------------------------------------------------
Denis Liard



-------------------------------------------------------------
PHP under Linux to IBM db2 on Linux how to connect ? Using odbc or --with-ibm-db option ?
http://www.faqts.com/knowledge-base/view.phtml/aid/2847
-------------------------------------------------------------
Denis Liard



-------------------------------------------------------------
Is there a port of PHP for OpenVMS?
http://www.faqts.com/knowledge-base/view.phtml/aid/2863
-------------------------------------------------------------
Istvan Banfi



-------------------------------------------------------------
Searching via keywords through text stored in BLOBs
http://www.faqts.com/knowledge-base/view.phtml/aid/2872
-------------------------------------------------------------
Jeff Liu



## New Entries #################################################


-------------------------------------------------------------
What is PHP?
http://www.faqts.com/knowledge-base/view.phtml/aid/2881
-------------------------------------------------------------
Nathan Wallace
Andrei Zmievski

PHP is a general-purpose scripting language that is especially suited
for the Web development and can be embedded into HTML. Its syntax is
similar to C, Java, and Perl. PHP runs on many different platforms and
can be used as a standalone executable as well as under many web
servers.  It has excellent support for databases, various Internet
protocols, and general data manipulation. PHP can be extended through
its powerful API to include additional functionality.


-------------------------------------------------------------
Why do I get the error "Maximum execution time exceeded"?
How can I change the max execution time in PHP?
http://www.faqts.com/knowledge-base/view.phtml/aid/2882
-------------------------------------------------------------
Nathan Wallace
Michael Dearman, Richard Lynch

PHP scripts are aborted after the maximum execution time has been
exceeded.  The default is 30 seconds.  You can change this setting in
your .ini file (if unix or GNU/Linux) for

    max_execution_time = 50

Or, in your PHP script you can use:

    set_time_limit(50);


-------------------------------------------------------------
Does configure cache options?
How can I get rid of the cached configure options?
http://www.faqts.com/knowledge-base/view.phtml/aid/2883
-------------------------------------------------------------
Nathan Wallace
Richard Lynch

Configure does cache some options.  This can lead to confusion if you
are not careful to remove the cache in certain instances.

To remove the cache:

    rm config.cache

If you see something like:

    "... GD support... (cached) no"

you forgot to remove the cache.


-------------------------------------------------------------
How can I create DOS style new lines in PHP?
Why are all my new lines messed up when I create a file for DOS / Windows?
http://www.faqts.com/knowledge-base/view.phtml/aid/2884
-------------------------------------------------------------
Nathan Wallace
Mark Roedel, Richard Lynch

If you're just ending each line with a "\n", you might try changing that
to "\r\n".  Just like in C, the \n is a newline, or linefeed (LF), while
the \r is a carriage return (CR).

So, you'll need to use something like:

    fwrite("...\r\n");

or

    fputs("...\r");

to get the \r in there for DOS.


-------------------------------------------------------------
Can I sort a 3 dimensional array in PHP?
http://www.faqts.com/knowledge-base/view.phtml/aid/2885
-------------------------------------------------------------
Nathan Wallace
Richard Lynch

Sort it how?...

You may need to iterate through the first dimension and sort each
sub-array, or perhaps you could write a custom comparison function for
usort() that will evaluate and sort the sub-array...

There's no real way to answer this question unless you explain which
dimension you want to sort, and what to do with the other two
dimensions.


-------------------------------------------------------------
What does --enable-dmalloc do?
http://www.faqts.com/knowledge-base/view.phtml/aid/2886
-------------------------------------------------------------
Nathan Wallace
Richard Lynch

The PHP FAQ links to http://www.dmalloc.com/ which states:

"The debug memory allocation or dmalloc library has been designed as a
drop in replacement for the system's malloc, realloc, calloc, free and
other memory management routines while providing powerful debugging
facilities configurable at runtime. These facilities include such things
as memory-leak tracking, fence-post write detection, file/line number
reporting, and general logging of statistics."


-------------------------------------------------------------
What are the benefits of using --enable-sysvsem and --enable-sysvshm?
http://www.faqts.com/knowledge-base/view.phtml/aid/2887
-------------------------------------------------------------
Nathan Wallace
Richard Lynch

These two allow the usage of "shared memory":  One can then put some
data into a chunk of RAM associated with a key, and then other processes
(including other PHP/httpd processes) can access this memory using the
same key.

It's not widely used, since the average PHP scripter will have a hard
time keeping correct track of what's where and cleaning up after
themselves with this technology.  It also is really only useful for
smallish chunks of data.

It is up to you to guarantee uniqueness of "keys" for lookups across
*ALL* processes using shared memory on your computer, as well as
cleaning up data you no longer need.

While it's really cool technology, it's not for the beginner.


-------------------------------------------------------------
Is there a newsgroup form of the PHP mailing list?
Is there a PHP newsgroup?
http://www.faqts.com/knowledge-base/view.phtml/aid/2888
-------------------------------------------------------------
Nathan Wallace
Rasmus Lerdorf

Connect your favourite newsreader to news.php.net


## Edited Entries ##############################################


-------------------------------------------------------------
How can I check a users login and password on every page visit?
How can I maintain data across pages for a visitor?
Does PHP have support for sessions?
How can I force a member to login?
http://www.faqts.com/knowledge-base/view.phtml/aid/56
-------------------------------------------------------------
Nathan Wallace, Jeff Wilcox
Anand Raman, Nathan Wallace,Zend Technologies (Tutorials Area)

HTTP is a stateless protocol.  Each request to the server is completely
independent of the other requests from the same browser.  It is possible
however to support user sessions through the use of persistent data on
the server side and cookies or form variables on the client side.

PHPLIB is a great set of classes wihch has already completed solved the
problem of session management and user authentication.  It is free for
use in your scripts.

    http://phplib.netuse.de

PHP 4 / ZEND USERS 
================== 

If you're using PHP 4 on your server, especially one of the more recent 
beta versions of RC1, then you're in luck!  By simply editing your 
php.ini file, you can setup file-based session management.  It's also 
possible to extend this to save sessions in any way.

If you go to 

http://www.zend.com/zend/tut/session.php 

You can read a good tutorial on using the built-in session capabilities 
in PHP4, and learn about how the system is setup.

PHPLIB may still be more suitable for you if you have written a complex 
site around it already, or if you're doing more advanced things with 
sessions.

A good quick example of the PHP4-sessions way of doing things is their 
counter example:

session_start(); 
print($counter); 
$counter++;
session_register("counter");


-------------------------------------------------------------
How can I copy a remote file without going through the fopen, fputs stuff?
How can I copy a remote file?
How can I copy a remote binary file?
http://www.faqts.com/knowledge-base/view.phtml/aid/388
-------------------------------------------------------------
Nathan Wallace, Sacul
Robert Aden

You have to use fopen and fputs to copy a remote file:

    function copyFromRemote($remote,$local){
        $fp1 = fopen($remote,"r");
        $fp2 = fopen($local,"w");

        while(!feof($fp1)) {
            $line = fgets($fp1, 1024);
            fputs($fp2,$line,strlen($line));
        } 
    }

I think that would work but i haven't tested it .....

U should use the following for binary files:

$line = fread($fp1,1024);


-------------------------------------------------------------
Can PHP do "reference" but not "copy" on variables in classes?
http://www.faqts.com/knowledge-base/view.phtml/aid/957
-------------------------------------------------------------
Robert Ames, Alan Tam
http://www.php.net/manual/functions.arguments.php

You might try:
<pre>
function increment_variable( &$counter ) {
  $counter ++;

}
$number_of_hits = 0;
while( $number_of_hits < 100 ) {
  increment_variable( $number_of_hits );
}
</pre>

The key is to use the ampersand in the function name.


-------------------------------------------------------------
How do you pass a function name as an argument to another function and then call the passed function? This would be similar to a function pointer in C
http://www.faqts.com/knowledge-base/view.phtml/aid/2275
-------------------------------------------------------------
Robert Ames, xendra .com
http://www.php.net/manual/functions.variable-functions.php

Believe it or not, this is really simple:
function do_something(){
  echo "done";
}
function something_else(){
  echo "we did something else";
}
function test_dynamic_functions() {
  $which_function = "do_something";
  $which_function();
  $which_function = "something_else";
  $which_function();
}

Of course you can pass command line parameters and stuff, if 
necessary.  The trick is to use the form "$variable_name()", making 
sure to have parenthesis after the variable name, and make sure that 
variable name has a valid function name.


-------------------------------------------------------------
How can I randomly shuffle the contents of an array?
http://www.faqts.com/knowledge-base/view.phtml/aid/2806
-------------------------------------------------------------
Nathan Wallace, Andrey Zmievski
Richard Lynch

Here's an easy one off the top of my head:

<?php
  function shuffle($array){
    //Randomly arranges elements in the array.
    //Returns the shuffled array.
    //This particular algorithm is O(n)
    
    //Take care to call srand once, and only once
    //in any given script.
    static $srand;
    if (!isset($srand) || !$srand){
      $srand = 1;
      srand((double) microtime() * 1000000);
    }
    
    $count = count($array);
    for ($i = 0; $i < $count; $i++){
      //Your version of PHP may need a different syntax to
      //generate a random number between $i and $count
      //Perhaps rand() % ($count - $i) + $i
      //More details in the PHP manual under 'rand' function
      $rindex = rand($i, $count);
      $temp = $array[$i];
      $array[$i] = $array[$rindex];
      $array[$rindex] = $temp;
    }
    
    return $array;
  }
?>

Or you can use built-in shuffle() function and don't forget to call
srand() beforehand.


-------------------------------------------------------------
How do I add hypertext links (such as 1-10,11-20,next,back etc) using PHP/MySQL to serve portions of query results ?
http://www.faqts.com/knowledge-base/view.phtml/aid/2825
-------------------------------------------------------------
Sam Leibowitz, Niketan Pandit


First, you need to get a count of all the records that meet your 
requirements. Let's say you have a really simple query: "SELECT id, 
name FROM mytable".   You could pass that to mysql_query(), then use 
mysql_num_rows() to get a count of all the rows.  Or, you could put the 
counting function right into the SQL query ("SELECT count(id) FROM 
mytable").

After that, you can loop through the results using the LIMIT specifier 
in your SQL query. When use in SELECTs, LIMIT takes two arguments: the 
first (optional) specifies the offset of the first row to return, while 
the second (mandatory) specifies the number of rows to return. So, to 
get rows 21 through 30, you'd use "SELECT id, name FROM mytable LIMIT 
20,10".  

Finally, you can use GET arguments (or whatever) to control which set 
of results the viewer gets. For example, if you're viewing results 11-
20, you could use the following two links:

<a href="thispage.php?limit=0,10">last 10 results</a>
<a href="thispage.php?limit=20,10">next 10 results</a>

To make this work, the LIMIT in the SQL query would have to be set 
dynamically: 

$query = "SELECT id, name FROM mytable LIMIT ".$limit.",10";

In all likelihood, you'll want to check to make sure that you don't 
offer a "next 10 results" option whne you've reached the end - 
actually, that's the only reason you need to count the results as 
described at the beginning of this post.  But that's a pretty simple 
operation, and left as an exercise for the reader. ;)


-------------------------------------------------------------
How can I save a variable to a file on the client (without cookies) and retrieve its value later?
How can I save a value to a file on the server?
http://www.faqts.com/knowledge-base/view.phtml/aid/2135
-------------------------------------------------------------
Greg Billock, George Federuik, Nathan Wallace


There is no way to access client side filesystems without cookies (thank 
goodness!) Cookies provide a very restricted way to store information on 
the user's machine. (See cookie() in the PHP manual.)

To write a variable to a file on the server, you can simply do

$f = fopen("file","w");
fwrite($f,$variable);
fclose($f);

and then 
$f = fopen("file","r");
$variable = fgets($r);
fclose($f);


-------------------------------------------------------------
How do I pass a sting in a url eg. $MyString='Have a nice day'?
http://www.faqts.com/knowledge-base/view.phtml/aid/2780
-------------------------------------------------------------
William Holt, Arcady Novosyolov, Christian Hope, Richard Heyes, Nathan Wallace
%20

Spaces should be replaced by pluses in query strings. To achieve this 
use the following code:

<?php // main file
$MyString = 'Have a nice day';
$s = strtr($MyString,' ','+');
echo "<a href=gr.php3?var=$s> Click here</a>";
?>

with called file gr.php3:

<?php // file gr.php3
echo $QUERY_STRING."<br>";
echo $var."<br>";
?>


I'm afraid this will not work. Actually, please remember that "space" 
is not allowed in a URL. You should change the string into "Have%20a%
20nice%20day". I think this will work.

Sincerely,
Bill


------

Alternatively, a better way would be the urlencode() function. Like so:

echo '<A HREF="greeting.php3?greeting='.urlencode($MyString).'">';

This will not only deal with spaces, but other questionable characters 
too.
HTH
Richard Heyes



Navigate in group php.kb at sever news.php.net
Previous Next




  
© No Copyright
You are free to use Anything
Site Maintained by PHP Developer
Powered By PHP Consultants