0

CodeIgniter error logging set to 1:

/*
|--------------------------------------------------------------------------
| Error Logging Threshold
|--------------------------------------------------------------------------
|
| If you have enabled error logging, you can set an error threshold to
| determine what gets logged. Threshold options are:
| You can enable error logging by setting a threshold over zero. The
| threshold determines what gets logged. Threshold options are:
|
|   0 = Disables logging, Error logging TURNED OFF
|   1 = Error Messages (including PHP errors)
|   2 = Debug Messages
|   3 = Informational Messages
|   4 = All Messages
|
| For a live site you'll usually only enable Errors (1) to be logged otherwise
| your log files will fill up very fast.
|
*/
$config['log_threshold'] = 1;

Is it possible to suppress error messages for functions that are preceeded by a @?

For example, I have somewhere a need to detect if a string is serialized or not. I'm using code similar to the following:

public function is_serialized($data)
{
    return (@unserialize($data) !== false);
}

Unfortunately, every time that $data is not serialized, it clogs up my error log even though I am using the @ operator.

2
Contributors
1
Reply
22
Views
4 Years
Discussion Span
Last Post by veedeoo
2

Hi Dani,

Might not be the best answer, but I think among the few PHP functions that does not go really well with the suppressor, unserialize is one of them..

The reason is that, even with the suppressor, unserialized($var) will always gives an error if $var is not serialized.

Warning: unserialize() expects parameter 1 to be string, array given

Besides the much bigger frameworks e.g zend, cake,codeIgniter, I found wordpress to also use this function to store data..

I don't mean any harm to the wordpress people, and I honestly sorry if I have to disassemble this part of the codes for the reason of GOOD cause..

So, my idea is to check if the $data is serialized before returning the method is_serialized..

Here we go !WARNING! Borrowed from Wordpress without authorization.

Let's create a new method to check and confirm that $data is serialized or not..(This returns Boolean)

private function checkIf_serialized( $var ) {

    if ( !is_string( $var ) )
        return false;
    $var = trim( $var );
    if ( 'N;' == $var )
        return true;

    if ( !preg_match( '/^([adObis]):/', $var, $s_chars ) )
        return false;

    switch ( $s_chars[1] ) {
        case 'a' :
        case 'O' :
        case 's' :
            if ( preg_match( "/^{$s_chars[1]}:[0-9]+:.*[;}]\$/s", $var ) )
                return true;
            break;
        case 'b' :
        case 'i' :
        case 'd' :
            if ( preg_match( "/^{$s_chars[1]}:[0-9.E-]+;\$/", $var ) )
                return true;
            break;
    }
    return false;
}

The is_serialized method above can be rewritten as follows

public function is_serialized($data)
    {

    //return (@unserialize($data) !== false);

       return((self::checkIf_serialized($data)? unserialize($data) : false);

    }

That should suppress every possible errors if unserialized data are encountered by the script..

Stand alone Tester codeS:

$this_array = array('PHP', 'JAVA', 'PYTHON');
$data = serialize($this_array);

 ## test if the function will detect unserialized
 echo (checkIf_serialized($data)? 'it is serialized' : false);

 ## test response if non-serialized data is given
 echo '<br/>';
  echo (checkIf_serialized($this_array)? 'it is serialized' : 'No way, this is serialized');

function checkIf_serialized( $var ) {

    if ( !is_string( $var ) )
        return false;
    $var = trim( $var );
    if ( 'N;' == $var )
        return true;

    if ( !preg_match( '/^([adObis]):/', $var, $s_chars ) )
        return false;

    switch ( $s_chars[1] ) {
        case 'a' :
        case 'O' :
        case 's' :
            if ( preg_match( "/^{$s_chars[1]}:[0-9]+:.*[;}]\$/s", $var ) )
                return true;
            break;
        case 'b' :
        case 'i' :
        case 'd' :
            if ( preg_match( "/^{$s_chars[1]}:[0-9.E-]+;\$/", $var ) )
                return true;
            break;
    }
    return false;
}    

Disclaimers and Notifications for the Worpress function, my sincere apology for using this part. I attempted to rewrite the function as I could, but the switch remains the same because the unique characters in serialized data are all the same..

Proof of the unique characters used for preg_match are all the same in all serialized data..

$data1 = serialize(array('HTML', 'JAVASCRIPT', 'RUBY AND RAILS'));
$data2 = serialize(array('PHP', 'JAVA', 'PYTHON'));

var_dump($data1) . '<br>';
var_dump($data2).'<br/>';

The above dumped will return this..

string(69) "a:3:{i:0;s:4:"HTML";i:1;s:10:"JAVASCRIPT";i:2;s:14:"RUBY AND RAILS";}" 

string(52) "a:3:{i:0;s:3:"PHP";i:1;s:4:"JAVA";i:2;s:6:"PYTHON";}" 

These are matched to confirm if it is indeed serialized.

a:3:{i:0;s:4: , i:1;s:10:, i:2;s:14: , i:0;s:3: , i:1;s:4: , i:2;s:6:

Edited by veedeoo: info added

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.