__toString() must not throw an exception

__toString() must not throw an exception.

 

If you an exception handler even that seems fail (in itself) with __toString() must not throw an exception. There is no nice safe way to handle it. The easiest solution is to catch that error as well. It might seems to be odd, but on the other hand the exception hanler shall not fail ;)


/**
* String representation of this object
* @return string
*/
public function __toString()
{
try {
return (string) $this->name;
} catch (Exception $exception) {
return '';
}
}

MySQL UTF-8 fix Umlaute

Geht ziemlich schnell Sonderzeichen zu reparieren in MySQL! Scheiß Encoding! ;)

UPDATE `table` set
    `column`= REPLACE(`column`,"ß", "ß"),
    `column`= REPLACE(`column`, "ä", "ä"),
    `column`= REPLACE(`column`, "ü", "ü"),
    `column`= REPLACE(`column`, "ö", "ö"),
    `column`= REPLACE(`column`, 'Ä', 'Ä'),
    `column`= REPLACE(`column`, "Ãœ", "Ü"),
    `column`= REPLACE(`column`, "Ö", "Ö"),
    `column`= REPLACE(`column`, '€', '€');

Ajax Undzeichen ( &) übertragen

Normalerweise gibt es ein Problem beim Übertragen von Daten per AJAX, wenn sich ein & Zeichen in dem zuübertragenden Text / Daten befindet. Das Javascript interpretiert das Undzeichen als Trennzeichen. Alles nach dem & Zeichen wird nicht mehr übertragen. Abhilfe schafft hier die funktion encodeURIComponent

 

var save_data = 'save_text=' + encodeURIComponent($(".edit_textarea").val());
		logging(save_data);
		//return false;
		$.ajax({
			type: 'POST',
			url: 'ajax.php',
			data: save_data,
			success: function (msg){
				console.log('OK: ' + msg);
			},
			error:function (xhr, ajaxOptions, thrownError){
				alert(xhr.status);
				alert(thrownError);
			}
		});

Jetzt kommen die Daten auch an.

Zend framework lucene UTF-8 problem

I had issues with the zend framework and its implementation of lucene. It saved the values from my UTF-8 database in the lucene files with characters like UTF-8 in ISO 8859-1 like on the search result page. And I wasn’t able to search case insensitive.

I noticed that the apache header (zend server CE) wasn’t sending UTF-8. So I added AddDefaultCharset utf-8 to my httpd.conf. Didn’t help.

What helped: In the Bootstrap.php adding to the init of the search

Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());

In the model it is needed to decode it to ISO 8859-1 and than save it as UTF-8. Sounds insane, but it was the only thing that works for me.

$doc->addField(Zend_Search_Lucene_Field::Text('lucene_DB_CLOUMN_NAME',utf8_decode($db_apater_result['DB_CLOUMN_NAME']),'UTF-8'));

WTF Zend Lucene!

JQuery Ajax cross domain request

If you try to do an ajax request on a different domain. You’ll get nothing. Looking a bit deeper into the request I noticed that the status code isn’t 200 or 403 nor 404 like expected. Nope it is 0. (jquery ajax statusCode 0). This means the browser doesn’t allow a crossdomain request. The easiest way for me to solve this was to create a proxy php script. Now I can send my ajax request to a url on my server while the PHP script does the real request to the other server.

This is the php script

/**
* GetFromHost()
*
* @param string $host
* @param int $port
* @param string $path
* @param string $referer
* @param bool $keepalive
* @return
*/
function GetFromHost($host, $port, $path, $referer,$keepalive=false) {

 $fsocket = fsockopen($host, 80, $errno, $errstr, 30);

 if($fsocket){
 $request =     "GET $path HTTP/1.1\r\n";
 $request .= "Host: $host\r\n";
 $request .= "Referer: $referer\r\n";
 $request .= 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) ';
 $request .= "Gecko/20021204\r\n";
 $request .= 'Accept: text/xml,application/xml,application/xhtml+xml,';
 $request .= 'text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,';
 $request .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
 $request .= "Accept-Language: en-us, en;q=0.50\r\n";
 //$request .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
 $request .= "Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66\r\n";
 if($keepalive==true){
 $request .= "Keep-Alive: 300\r\n";
 $request .= "Connection: keep-alive\r\n";
 }
 else
 {
 $request .= "Connection: close\r\n";
 }
 //$request .= "Content-Type: application/x-www-form-urlencoded\r\n";
 //$request .= "Content-length: ". strlen($data_to_send) ."\r\n";
 $request .= "\r\n";
 //$request .= $data_to_send;

 fputs($fsocket, $request);

 $res ='';
 while(!feof($fsocket)) {
 $res .= fgets($fsocket, 1024);
 }
 fclose($fsocket);

 return $res;

 }
 else
 {
 return false;
 //echo "Fehlgeschlagen: ".$fsocket . $host .':'. $port;
 }

}

$x = GetFromHost("www.example", "80", "/deep/url/kinda/", "");
$x = explode('Content-Type: text/html',$x);
$output = preg_replace('/\s+(\r\n|\r|\n)/', '$1', $x['1']);//remove white space and tabs at the line ends
echo $output;

maybe you have to edit the explode part for server to separate the header fom the real content.

Disable AllowOverride (htaccess to httpd.conf)

AllowOverride is one of the things that slow down a lot. Disabling it makes apache faster. It is a horror to migrate all .htaccess files by hand. BUT here is a PHP script from Paul Reinheimer which makes it realy easy :-)

Get it htaccess.php

tune your server!

Changed to event mpm

I made some testing on my dev machine and liked event mpm. So I installed it here. I noticed that  the event mpm closes the connections faster than worker mpm. Some download tests sadisfied me. Less memory usage and faster serving :-) For now I keeped the settings from worker mpm. I’ll look for tweaking in the next days.

Different to the docs event mpm works fine with SSL.

sudo apt-get install apache2-mpm-event libapache2-mod-fcgid

Also the serving PHP over fcgid is nice.  The implementation of PHP over fcgid in the older post.

roundcube dislikes PHP 5.3

The current stable version ( 0.3) and the latest beta (0.4 beta) of roundcube dislike my upgrade tp PHP 5.3 All other applications run fine, but roundcube. Searching in the logs showed me that there are some PHP 4 code is used and often PHP 4 coding style. The manual says that at least PHP 5.2 must be used or greater. Super, since it does work with 5.3

Good that I’m a PHP developer myself and I found the places in the code I had to change. I had no fun to rewrite the whole code to PHP 5 style. So I just fixed it.What I have changed?

program/lib/MDB2.php on line 392
program/lib/MDB2.php on line 2614
program/lib/PEAR.php on line 563
program/lib/PEAR.php on line 566

By removing the “&”. I also searched for “=&” and replaced it with “=”. Quick and maybe dirty, but now I can check my mails again. I wonder how say you have to use PHP 5.2 or greater and still using that crap code?

Posts Tagged php

Archives by Month: