Category Archives: Uncategorized

PHP Correctly Get Last 12 Months (Month per Month)

PHP snippet to get the last 12 months, month per month: $dt = new DateTime(‘first day of this month’);   for ($i = 1; $i <= 12; $i++) { echo $dt->format(‘M Y’), "\n";   $dt->modify(‘-1 month’); }$dt = new DateTime(‘first day of this month’); for ($i = 1; $i <= 12; $i++) { echo $dt->format(‘M […]

PHP StrToTime() Returns Incorrect Timestamp ‘-1 MONTH’

If you use date() with strtotime() to get the last 12 months (or the last 1 month, the last 2 months, etc) you may have noticed that when you are on the last day of the current month, i.e 31 of May, you will get repeated months, for examples: echo date("M Y"); echo date("M Y", […]

PHP Multi-cURL to Run Parallel cURL Requests (Example)

Simple PHP function to run multiple cURL requests in parallel: function multicurl_urls($nodes) { $node_count = count($nodes);   $curl_arr = array(); $master = curl_multi_init();   for($i = 0; $i < $node_count; $i++) { $url =$nodes[$i]; $curl_arr[$i] = curl_init($url); curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true); curl_multi_add_handle($master, $curl_arr[$i]); }   do { curl_multi_exec($master,$running); } while($running > 0);   $results = array(); […]

Vertically align Facebook and Twitter share buttons

HTML code to vertically align Facebook and Twitter share buttons: <div class="d-inline"> <style>.fb_iframe_widget span { vertical-align: baseline !important; }</style> <div class="fb-like" data-href="http://www.yourwebsite.com/" data-layout="button_count" data-action="like" data-size="small" data-show-faces="false" data-share="true"></div> <a href="https://twitter.com/share?ref_src=twsrc%5Etfw" class="twitter-share-button" data-show-count="false">Tweet</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> </div><div class="d-inline"> <style>.fb_iframe_widget span { vertical-align: baseline !important; }</style> <div class="fb-like" data-href="http://www.yourwebsite.com/" data-layout="button_count" data-action="like" data-size="small" data-show-faces="false" data-share="true"></div> <a href="https://twitter.com/share?ref_src=twsrc%5Etfw" class="twitter-share-button" data-show-count="false">Tweet</a><script […]

nginx: [warn] the “ssl” directive is deprecated

Today I upgraded Nginx to v1.16.0 stable and I noticed this: nginx: [warn] the "ssl" directive is deprecated, use the "listen … ssl" directivenginx: [warn] the "ssl" directive is deprecated, use the "listen … ssl" directive To fix this just delete “ssl on;” from your vhost config file. For example, change this: server { listen […]

PHP GetHostByAddr() Slow? Make it Faster

A quick workaround to make gethostbyaddr() faster in PHP is: // Set timeout and retries to 1 to have a max execution time of 1 second for the DNS lookup putenv(‘RES_OPTIONS=retrans:1 retry:1 timeout:1 attempts:1’);   // Now call gethostbyaddr() function $ip = @gethostbyaddr("1.2.3.4");// Set timeout and retries to 1 to have a max execution time […]

How to Match Newline with Strpos() in PHP

Match newline with strpos() in PHP: $string = "Hello World Testing New Line";   $findme = "Hello World\r\nTesting";   if(strpos($string, $findme) !== false) { echo "String found"; } else { echo "String not found"; }$string = "Hello World Testing New Line"; $findme = "Hello World\r\nTesting"; if(strpos($string, $findme) !== false) { echo "String found"; } else […]

Use PHP Strpos() to Check if a String Starts with

Check if a string starts with a specific word in PHP with strpos(): $string = "Hello World!";   $findme = "Hello";   if(strpos($string, $findme) !== false && strpos($string, $findme) === 0) { echo "String found at pos 0"; } else { echo "String not found at pos 0"; }$string = "Hello World!"; $findme = "Hello"; […]

How to Run a Command with Time Limit in Bash Linux

You can use “timeout” command to run a program and auto-kill it after N seconds: timeout –signal=KILL 3 whois google.cdtimeout –signal=KILL 3 whois google.cd The above command will auto-kill whois if it runs for more than 3 seconds. Here is an example output in a Debian server: root@server:~# timeout –signal=KILL 3 whois google.cd Killedroot@server:~# timeout […]

Stop Bad Bots from Crawling Your Website

A robots.txt example to block bad bots from crawling your website: User-agent: * Allow: /   User-agent: CuteStat Disallow: /   User-agent: AhrefsBot Disallow: /   User-agent: SemrushBot Disallow: /   User-agent: GrapeshotCrawler Disallow: /   User-agent: WebsiteOutlook Disallow: /   User-agent: MJ12bot Disallow: /User-agent: * Allow: / User-agent: CuteStat Disallow: / User-agent: AhrefsBot Disallow: […]