Posts

Showing posts from December, 2012

Top 10 articles 2012

Image
Article Published Views Using V8 Javascript engine as a PHP extension Jul 22 8458 Create PDF invoices with HTML5 and PhantomJS Dec 8 2006 How to write a really small and fast controller with PHP Jul 17 1320 MySQL or MySQLi or PDO Jul 21 957 PHP Framework Comparison Oct 19 916 Mass inserts, updates: SQLite vs MySQL Sep 13 585 How to implement really small and fast ORM with PHP Oct 16 524 PHP memory consumption with Arrays and Objects Jun 23 465 For or Foreach? PHP vs. Javascript, C++, Java, HipHop Jun 27 444 How to implement a real-time chat server in PHP using Server-Sent Events Oct 29 386 Views from 6/19/12 to 12/28/12: 23464 Top 5 Country Views United States 4680 Germany 4109 China 1268 United Kingdom 1224 Russia 982

Building PHP extensions with C++, the easy way (update: MySQL, threads)

Image
Here is an easy way to build a PHP extension with C++ and default PHP packages on a Ubuntu system. I use SWIG to wrap C++ code to the Zend API. When using loops and recursion intensively, porting a few functions to C++ can give you some extra power . First, install all required packages (tested with Ubuntu 12.10) : apt-get install php5-cli php5-dev swig g++ Second, write the code: // example.swig %{ #include <iostream> #include <vector> using namespace std; int HelloWorld(char *str) { cout << "Hello World: " << str << endl; // run some slow code vector<int> array; for (int i=0; i < 10000000; i++) array.push_back(i*2); for (int i=0; i < array.size(); i++) array[i]++; return 0; } %} %module example int HelloWorld(char *str); Third, compile the code and load the exension: swig -c++ -php5 example.swig g++ `php-config --includes` -O2 -march=native -mtune=native -std=c++11 -fPIC -c *.cpp g++ -shared *.o -o exa

Fixing ASP.NET application for IE10 compatibility

We've a stable MVC web application. However, recently there was a sprung in issues (mostly JavaScript) breaking many of the existing features. Recently, QA started to test application in IE10 browser after the analytic showing an increased traffic from it (Thanks to a lot of new Windows 8 devices). Where QA was quick enough to log defects one-after-the-another , we as a developer, were a bit panicked. This post is all about fixing your application for IE 10. I'm in progress of fixing my application and will keep posting any new gimmick. (I appreciate if you could share your experiences too) 1st Fix (Master Stroke) Issue: Application working in cookieless mode. I.e. Adding session to URL. For example: upon login (Forms Authentication), http://mysite.com/welcome.aspx, it shows http://mysite.com/welcome.aspx(A(ikRoEoqwOieH_FyADedeEbit-_uDNadHNudahUar-HaUsU99DiasIadsjKAsjJiaojdJaJadijAQBR0))/Welcome.aspx The first fix I did, was with minutest change but fixed more than 80% issues i

Create PDF invoices with HTML5 and PhantomJS

Image
Creating invoices in PDF is always a bit tricky: there are many libraries to create PDF documents with PHP, but most can't handle complex layouts and require a lot of memory and CPU time. Things like Unicode characters and line/page breaks are often difficult to program and the source of many bugs and memory problems. Using logos as vector graphics or embedding TrueType fonts is often required, but not possible with most libraries. The good thing is that all these features are included in HTML5. Since most companies offer their invoices also in HTML, it would be great to convert them directly to PDF. Tools like html2ps and ps2pdf can produce PDFs with 200+ pages without problems, but only support HTML4 and some very limited CSS. Also, the layout is never the same as in a web browser. So we need a real web browser to convert HTML pages to PDFs. From testing web applications, we know that PhantomJS is a headless WebKit browser that runs on the commandline and can automate things

Exporting events to google calendar Link HtmlHelper

Following is a ASP.NET MVC HtmlHelper to generate an anchor link for exporting an event to Google Calendar. public static MvcHtmlString ExportEventToGoogleLink(this HtmlHelper htmlHelper, string title, DateTime startDateTime, DateTime endDateTime, string description, string location, string linkText, IDictionary<string, object> htmlAttributes = null) { const string dateFormat = "yyyyMMddTHHmmssZ"; Uri url = System.Web.HttpContext.Current.Request.Url; StringBuilder sb = new StringBuilder(); sb.Append("http://www.google.com/calendar/event?action=TEMPLATE"); sb.Append("&text=" + title); sb.Append("&dates=" + startDateTime.ToUniversalTime().ToString(dateFormat)); sb.Append("/"); sb.Append(endDateTime.ToUniversalTime().ToString(dateFormat)); sb.Append("&details=" + description); sb.Appe

Shrink qcow2 file with Windows guest

Remove unnecessary file and empty the recycle bin in windows guest. De-fragment your disk in windows guest. You may do it several times for good compact status. Free unused disk space in windows guest. sdelete -c c: Convert the disk image itself. qemu-img convert -c -O qcow2 win.qcow2 win-shrinked.qcow2

The power of column stores

using column stores instead of row based stores can reduce access logs from 10 GB to 130 MB of disk space reading compressed log files is 4 times faster than reading uncompressed files from hard disk column stores can speed up analytical queries by a factor of 18-58 Normally, log files from a web server are stored in a single file. For archiving, log files get compressed with gzip. A typical line in a log file represents one request and looks like this: 173.15.3.XXX - - [30/May/2012:00:37:35 +0200] "GET /cms/ext/files/Sgs01Thumbs/sgs_pmwiki2.jpg HTTP/1.1" 200 14241 "http://www.simple-groupware.de/cms/ManualPrint" "Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0" Compression speeds up reading the log file: $start = microtime(true); $fp = gzopen("httpd.log.gz", "r"); while (!gzeof($fp)) gzread($fp, 8192); gzclose($fp); echo (microtime(true)-$start)."s\n"; // 26s $start = microtime(true); $fp = fopen("