Archive for snippets

Javascript / jQuery get first class name of element

Need to get the first class name of an element? Simple! Imagine having a link with 3 class names

<a id="myelement" href="http://google.com" class="class1 class2 class3">Go to google</a>


$('a#myelement').attr('class').split(' ')[0]

Will result in: class1

Facebook Error Message: Requires valid next URL.

Let me start with asking you a few questions!

  1. Are you having issues with the Facebook API?
  2. Are you constantly getting the error message “API Error Code: 100 API Error Description: Invalid parameter Error Message: Requires valid next URL.“?
  3. Are you using the latest official Facebook PHP class?
  4. Are you redirecting using the PHP header() function?

Facebook error

Well,  I might have a solution for you. When calling the function getLoginUrl in the Facebook PHP class, the function will return a HTML encoded string. When outputing this string to your browser, it is rendered/decoded by a browser and this will convert all “safe” charachters to their textual equivalent.  This makes that “&amp;” becomes “&” and “%20″ becomes ” “.

However, when using the header() function in PHP, this url will not be rendered/decoded and you are basically sending out a garbled request.

Original

getLoginUrl($params);
header('Location:' . $url);

Solution:

getLoginUrl($params);
$url = str_replace('&','&',urldecode($url));
header('Location:' . $url);

xdebug trace file parser

xdebug_trace_preview

When I am trying to optimize my PHP code, one of my prefered tools is xdebug. It provides me with excellent error messages, outputs code tracing files & insight into memory usage.

By using kcachegrind or webgrind – wincachegrind crashes *all* the time – to parse the cachegrind files created by xdebug, you get a good idea of what your code is doing most of the time. It’ll show you which functions are called, by who, which arguments are passed and tons more. Check it out.

Now, there was one small thing I couldn’t find anywhere. A program to parse the trace files that xdebug generates. True, xdebug already provides the option to output this information into HTML, but it’s not really what I was looking for. I want to get quick information in the blink of an eye! That’s why I have created my own trace file parser, based on PHP of course.

Read more »

Drupal 5 Views Module Breaks Block Administration

poor little kitten with big round eyes While working on a new Drupal 5.19 powered website that has the latest view module enabled, my coworker noticed a strange feature. Once you activated more than one template and tried to switch over to manage the blocks on an other template than garland you will see it switching to garland 50% of the time. Read more »

memcache_connect() [function.memcache-connect]: Can’t connect to localhost:11211

Encountered this error? It could be as simple as switching the host from localhost to 127.0.0.1

before:

<?php
$memcache_obj = memcache_connect('localhost', 11211);

after:

<?php
$memcache_obj = memcache_connect('127.0.0.1', 11211);

« Older Entries