Apr 29

Message:

PHP Warning: Parameter 1 to stats_admin_bar_menu() expected to be a reference, value given

Error type: warning

Symptoms:

A warning is displayed even though the function call seems to have the correct number of parameters.

Sample Code:

<?php
function stats_admin_bar_menu( &$wp_admin_bar ) {
// …
}
call_user_func_array(‘stats_admin_bar_menu’, 1);
?>

Cause:

When a function that has one of the parameters defined as a reference is called with constants, this warning is issued.

Fix:

Check where parameters are defined as reference and pay attention on what parameters are sent at the function call. If a parameter is defined as a reference, it must receive a variable and not a constant.

Apr 29

Message:

PHP Warning: fsockopen(): unable to connect to domain.com:80 (Operation now in progress) in /home/user/public_html/browser.php on line 724

Error type: warning

Symptoms:

When you try to open a socket (for example load a file from an external server), you just receive a warning.

Cause:

The destination is unreachable.

Fix:

Re-establish the connection between the two servers. Most likely the location you are trying to connect to has a period of downtime.

Apr 29

Messages:

Database error: MySQL server has gone away
Database error: Query execution was interrupted
Database error: Lost connection to MySQL server during query

Error type: error

Symptoms:

The query that is executed on a MySQL database returns this error instead of the expected result.

Cause:

The MySQL server connection has been interrupted – either the TCP connection has timed out or the MySQL server has stopped.

Fix:

Check if the connection between the current server and the MySQL server is active.

Restart MySQL: /etc/init.d/mysql restart

Nov 21

Message:

PHP Warning: fopen(cache/____________.cache): failed to open stream: File name too long in test.php on line 12

Error type: warning

Symptoms:

Call to function fopen() fails for some files.

Cause:

The file system has a limit for the file names, for Linux (ext) is 256 characters, trying to create a file longer than that will fail and issue the warning above.

Fix:

Shorten the filename to have less than 256 characters. Use hash functions if you can’t just truncate the file name.

Feb 18

There are some cases when you don’t know the name of a function you need to call, or that name can depend from various cases. This is why you need to call a static method by using a custom parameter. This is how you do it:

class Class1
{
 public static function a($x)
{
 echo 'in function a: ' . $x;
}
}

$functionName = 'a';

// won't work
$str = 'Class1::' . $functionName;
$$str('4a');

// this is how you do it
call_user_func(array('Class1', $functionName), '4b'); 

That code should output: in function a: 4b

Please also check call_user_func_array if you need to send the parameters as an array.

Aug 4

Numbers are great, sure, but sometimes you need to spell them out … and if you think the possibility for that to happen is slim, invoices and checks for example need to have the ammount in both numbers and letters (in most countries).

That’s why a script to convert numbers to words (to spell numbers) can come in handy.


function number_to_words ($x)
{
     global $nwords;
     if(!is_numeric($x))
     {
         $w = '#';
     }else if(fmod($x, 1) != 0)
     {
         $w = '#';
     }else{
         if($x < 0)
         {
             $w = 'minus ';
             $x = -$x;
         }else{
             $w = '';
         }
         if($x < 21)
         {
             $w .= $nwords[$x];
         }else if($x < 100)
         {
             $w .= $nwords[10 * floor($x/10)];
             $r = fmod($x, 10);
             if($r > 0)
             {
                 $w .= ' '. $nwords[$r];
             }
         } else if($x < 1000)
         {
		
             	$w .= $nwords[floor($x/100)] .' hundred';
             $r = fmod($x, 100);
             if($r > 0)
             {
                 $w .= ' '. number_to_words($r);
             }
         } else if($x < 1000000)
         {
         	$w .= number_to_words(floor($x/1000)) .' thousand';
             $r = fmod($x, 1000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $w .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         } else {
             $w .= number_to_words(floor($x/1000000)) .' million';
             $r = fmod($x, 1000000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $word .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         }
     }
     return $w;
}

Download: numbers-words.zip (1 KB)

Demo

The zip file contains the source code to convert numbers to words in both English and Romanian (you never knew when you need 'em).

So ... enjoy!

Jul 20

You all know the default Apache directory listing .. and I bet most of you hate it. It does the job, sure, but it’s not exactly the most useful (or the best looking).

This is why you might want to use a more professional script if you want to show some files/directories.

1. phpFe – PHP File explorer

It might not be the best looking, but it sure does its job .. and quite well I might add. It also has an admin module with basic functions. This is pretty much the only script that also takes SEO into consideration.

Website: http://phpfe.vigge.net

Download: http://phpfe.vigge.net/#download

Demo: http://phpfe.vigge.net/phpFe/

Read the rest of this entry »

Jun 21

Message:

PHP Warning: Cannot modify header information - headers already sent by (output started at /home/w.php:189) in /home/start.php on line 64

Error type: warning

Symptoms:

You try to send a header value from PHP (for example a redirect) and it fails with this warning.

Sample Code:

<?php
echo ‘Redirecting: ‘;
header(‘Location: /’);
exit;
?>

Cause:

You must send headers before you output anything on that page. This means you can’t use echo, printf or any other output function before all headers are sent.

Fix:

Check where you used the header() function and remove any output instructions before that.

Please note that blank spaces before <?php are also considered part of the output and will give you lots of trouble. This also applies to files you include (for example configuration files), so make sure you don’t leave extra spaces/empty lines).

Another method to get rid of this problem is to use output buffering.

Jun 21

Message:

PHP Warning:fsockopen(): unable to connect to rpc.technorati.com:80 (Connection timed out) in /home/public_html/pinger.php on line 66

Error type: warning

Symptoms:

You try to open a socket to a remote location, but your connection is timed out (you don’t receive any response).

Sample Code:

<?php
$fs = fsockopen($ping_host, $ping_port, $errno, $errstr);

if (is_resource($fs))
{
stream_set_write_buffer($fs,0);
fwrite($fs, $http_request);
while (!feof($fs))
{
$response .= fgets($fs, 2048);
}
fclose($fs);
}
?>

Cause:

The host you are trying to connect to is either down or takes too long to respond (or from other reasons, you just can’t get a response).

Fix:

Get a beer, watch a movie, play a game … really, there’s not much you can do usually. Wait a few minutes and try again. If you still get the same response check if there is any problem with your server (for example DNS problems), eventually contact your hosting provider.

Jun 21

Message:

PHP Warning: Variable passed to each() is not an array or object in /home/functions.php on line 23

Error type: warning

Symptoms:

You try to use each, but instead of going through the loop, you just receive a warning.

Sample Code:

<?php
while (list($key, $val) = each ($myvar))
{
echo $key.’ ‘;
}
?>

Cause:

You try to use the each() function with a null, integer or string variable (or generally not an array or object). This might be caused by an uninitialized variable you try to use with each();

Fix:

Make sure the variable you try to pass exists and is not null first. You should also make sure you didn’t misspell the variable name (if you try to use a variable that has not been initialized you get the same warning).

You should try to use function is_array($array) to make sure the variable passed is indeed an array.

« Previous Entries