Solving Uncaught CurlException: name lookup timed out in facebook.php
If you are facebook developer, and are using the latest facebook php sdk, facebook.php grabbed from github.com, and are facing the problem similiar to as defined below:
PHP Fatal error: Uncaught CurlException: 6: name lookup timed out thrown in /<location>/facebook.php on line 592
This is probably the host is not given enough time to contact with facebook server. To solve this problem, open facebook.php with your editor, and go to line 89 to find the code below:
public static $CURL_OPTS = array( CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 60, CURLOPT_USERAGENT => 'facebook-php-2.0', );
Modify CURLOPT_CONNECTION to increase its value to, 30 or higher.
CURLOPT_CONNECTTIMEOUT => 30,
and test your facebook application again to see if it works.
Suggestion: Change to a better host to resolve the problem without modifying the facebook.php script
Solving Oracle OCI problem with PHP
Have been struggling to get rid of the error OCIEnvNlsCreate() failed and ask user to check the Windows Enviroment PATH settings.
The error message appear when I am using php function oci_connect() to connect to oracle database.
When starting Apache, it also throws me windows error relating php_pdo_oci.dll, php_
I installed Oracle instant client basic lite, set the PATH and PHP extension correctly. The error just appear there and PHP script stopped executing.
If the script is using PHP PDO with OCI, it doesn't throw any message, instead return me a blank page whenever assigning new PDO() .
Some guys in the internet advise to copy whatever files reside in instant client folder to apache\bin folder, but that does not work for me either.
Finally, I downloaded Oracle instant client basic (non lite version) and my problem gone. Seem like trying to save some spaces and bandwidth isn't a good idea. (Lite version ~ 20MB, non-Lite ~ 50MB)
Instruction on installing instant client: http://www.oracle.com/technology/pub/notes/technote_php_instant.html
PHP Security
Most of the people who are new to programming are unaware of hacking. This applies too to those who are doing programming task for small organization, as there are no "hackers" interested to hacking the site they are building.
But when the site is growing bigger and popular, the website not only attracting the potential customers or clients; it also attract the attentions from hackers and opponents in the same industry.
Liberty Reserve – XML API with SSL error fix
Back to few days ago, Liberty Reserve (LR) XML API is not working in my PHP script, when I try to retrieve the transaction history. If you do not know what is LR, it is one of the largest E-currency payment gateway.
I have been using CURL to retrieve the information for since few months ago. By using curl_error function, PHP output the following error:
SSL read: error:00000000:lib(0):func(0):reason(0), errno 104
It seem like is the SSL is causing the problem. Ignoring the CURL, I use file_get_contents to fetch the URL information without luck. However, the information can be retrieved if I paste the long XML encoded URL directly into the browser. By doing some research on Google on SSL with CURL, finally I fixed the problem with my own.
The solution is just to add another line of code curl_setopt if you have not done so:
// $ch - initialized CURL resource curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"
Normally user agent is set if the site allow legit browser only. Not sure why LR is changing this all of sudden, but at least the fix is working for me.
Import large database in remote server
If you owned slow internet connection, it would be headache if you have large database to be imported in a remote server. Either you import the database through MySQL GUI or phpMyAdmin, the execution time is mostly will exceed the maximum execution time allowed, resulting in an script timeout problem. It would be same if you manually paste the queries and execute it in remote server. The server simply doesn't have enough time to capture your bulk records of "INSERT".
So is there another alternative?
Actually, it is. You can upload your sql file to your remote server, and then use the PHP script to parse the sql file and then run the query line by line.
Fortunately, there is an open source code which ease our job to write our own script. You may visit "BigDump", and download its source code, bigdump.php, which is zipped as archive sized at only 10KB zip archive.
Before using BigDump, you need to upload your sql file into your remote server. Be sure not to upload your file to somewhere people may access it, such as your /public_html folder. Normally, you would upload the file at the parent folder of /public_html, such as, /home/<username>/
Bigdump also allow import the records from CSV file. You may refer the following configuration source code, foundat the beginning of bigdump.php.
// Database configuration $db_server = 'localhost'; $db_name = ''; $db_username = ''; $db_password = ''; // Other settings (optional) $filename = ''; // Specify the dump filename to suppress the file selection dialog $csv_insert_table = ''; // Destination table for CSV files $csv_preempty_table = false; // true: delete all entries from table specified in $csv_insert_table before processing $ajax = true; // AJAX mode: import will be done without refreshing the website $linespersession = 3000; // Lines to be executed per one import session $delaypersession = 0; // You can specify a sleep time in milliseconds after each session // Works only if JavaScript is activated. Use to reduce server overrun // Allowed comment delimiters: lines starting with these strings will be dropped by BigDump $comment[]='#'; // Standard comment lines are dropped by default $comment[]='-- '; // $comment[]='---'; // Uncomment this line if using proprietary dump created by outdated mysqldump // $comment[]='CREATE DATABASE'; // Uncomment this line if your dump contains create database queries in order to ignore them $comment[]='/*!'; // Or add your own string to leave out other proprietary things // Connection character set should be the same as the dump file character set (utf8, latin1, cp1251, koi8r etc.) // See http://dev.mysql.com/doc/refman/5.0/en/charset-charsets.html for the full list $db_connection_charset = '';
Passing Object from Flash AS3 to PHP
In previous post, we learnt how to pass an JSON encoded object from PHP to AS3. How about passing an Object from AS3 to PHP?
This example will show you how to do it. Take a look at the Flash ActionScript.
import flash.net.URLRequest; import flash.net.URLLoader; import com.adobe.serialization.json.JSON; var people:Array = new Array(); var person:Object = new Object(); person.firstname = "Kobe"; person.lastname = "Bryant"; people.push(person); var url:String = "http://localhost/getJSON.php"; var request:URLRequest = new URLRequest(url); request.method = URLRequestMethod.POST; var requestVars:URLVariables = new URLVariables(); requestVars.myObject = JSON.encode(people); request.data = requestVars; var loader:URLLoader = new URLLoader(); loader.load(request);
The script above encode the Object people with JSON, and pass it to variable name "myObject". "myObject" will be passed to PHP script using POST method.
Next we will take a look at the PHP script, getJSON.php
$object = JSON_decode($_POST['myObject']);
$fp = fopen('data.txt', 'w');
fwrite($fp, $_POST['abc']);
fwrite($fp, $object[0]->firstname . " ");
fwrite($fp, $object[0]->lastname . chr(13) . chr(10));
fwrite($fp, $_POST['myObject']);
fclose($fp);
The script decode the JSON Object, $_POST['myObject'] passed from the AS3. Then it output the data to data.txt with the following output:
Kobe Bryant
[{"firstname":"Kobe","lastname":"Bryant"}]
The 2nd line is the encoded JSON object which is not decoded yet.
Remember that ascorelib is needed for JSON encoding & decoding in ActionScript. Extract the ascorelib zip file, locate the src\com folder and copy it to your Flash CS4 project folder.
Example files is provided. Download here.
Checking Account Status in LDAP
How to check if an account is disabled in LDAP?
Previously I googled and someone suggest that, the 'useraccountcontrol' with 512 would be disabled, 514 would be enabled.
But this is too unreliable. If there are other options for that particular account are set, for example "password never expired" and "user cannot change password", the status would be 66048 and 66050. Too many possbility and yet unpredictable too.
To solve this problem, we have to look at how Computer is formed at the earliest stage. It all begins with binary number (Yes, my blog motto). We have to convert the account status from decimal to binary number. Let's look at the example below for 512, 514, 66048, 66050 in binary form.
512 - 1000000000
514 - 1000000010 (disabled)
66048 - 10000001000000000
66050 - 10000001000000010 (disabled)
Note the different? The 2nd bit (count from right) would be "1" for the account to be disabled. So we just need to check the 2nd bit of the binary value of account status. "0" would be an enabled account!
Lets come back to programming. How do we check the 2nd bit of binary number? Do we use string function provided by PHP? The answer is NO! Just look at the following code:
$ac = 512;
if (($ac & 2)==2) {
//disabled
} else { // enabled }
Why is it? We use the AND operation. For example 0010 AND 0011 = 0010. Only the bits that are set in both 0010 AND 0011 are set. In this case, it would be the 2nd bit only. By using such operation, we will be able to tell if the user account control's 2nd bit of binary number is set or not. If it is set, return binary 10 which is equal to 2 in decimal. Else, return 0 for enabled account!
Importance of initialization of variable inside Function
Just a reminder for myself and those who might found this useful:
It is important to initialize the variable inside the function. As for me, I have encountered an error when calling a function for the 2nd time, before the 1st execution is finished. For example:
function call(abc) {
//some delay up to few seconds
name = abc;
}
first = call('orange');
second = call('apple');
It will end up that, first and second will have an equal value of apple. Why does this happen? It is because the name variable inside the call function does not belong to the function. It can be called outside from the function of well, therefore name will be overwritten by the second call of function, although you have make a first call with orange parameter.
Another example:
function say(something) {
word = something;
}
say('halo');
alert(word);
The alert will pop up an message of 'halo'. If you initialize variable using the following:
function say(something) {
var word = something;
}
say('halo');
alert(word);
The javascript code above will prompt you an error with undefined variable.
Although it might save a lot of time to ignore inputting var for every variable, problems might be encountered if some of the functions are to be run before the previous function execution is finished.
PHP variable performance
I ran a simple test which print out the variable $var = "" (empty string) with a loop of 100,000 times for 10 times, and the average in seconds. The table below is what I get using Core 2 Duo E6320 processor.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Avg | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| "$var" | 0.037 | 0.067 | 0.099 | 0.138 | 0.170 | 0.201 | 0.234 | 0.272 | 0.302 | 0.334 | 0.0334 |
| $var | 0.015 | 0.029 | 0.044 | 0.062 | 0.078 | 0.093 | 0.107 | 0.125 | 0.140 | 0.154 | 0.0154 |
| "{$var}" | 0.030 | 0.060 | 0.090 | 0.123 | 0.153 | 0.182 | 0.213 | 0.244 | 0.274 | 0.304 | 0.0304 |
The first case is echo "$var", the 2nd is the traditional echo $var, and the 3rd is echo "{$var}"
It seems that by using echo $var, the speed would be a lot faster than the other 2. The first case and the 3rd case is nearly the same, with 3rd case is having slightly better performance than the first case.
For convenience, we would write echo "Today is $today" or echo "Today is {$today}", however this would defeat the need of application to be processed in shortest time. Using echo "Today is " . $today is a bit troublesome, but it save time and load for the server side. Furthermore, text editor often recognize and colorize the php variable outside the double quote. If the variable is placed inside the double quote, the colour would be just the same as the normal text and we would have difficulty to differenciate it in a glance.
Conclusion? It is best to use
echo "Today is " . $variable; // best performance
Rather than
echo "Today is {$variable}"; //much slower
which is slightly better than
echo "Today is $variable"; // the slowest!
Like