Category Archives: Uncategorized

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: […]

PHP Check if XML String is Valid

Simple PHP function to check if a XML string is valid: function isValidXML($xml) { libxml_use_internal_errors(true);   $doc = new DOMDocument(‘1.0’, ‘utf-8’); $doc->loadXML($xml);   $errors = libxml_get_errors();   libxml_clear_errors();   if(empty($errors)){ return array("valid" => true); }   $error = $errors[0];   if($error->level < 3){ return array("valid" => true); }   $explodedxml = explode("r", $xml); $badxml = […]

PHP Securely Save Passwords in Your Database

To securely save passwords in your database you should use password_hash()! When the user registers, you hash the password and then save it to the database: // Create the hashed password from the user submitted password // Notice that we hash() the password using sha256 before creating the hashed password   $hashed_password = password_hash(hash("sha256", trim($_POST["password"])), […]

How to block CuteStat from crawling your website

CuteStat shows traffic statistics, but it is bad for your website! It steals your page title and thus also your position on Google searches! To block CuteStat from crawling your website, add this on robots.txt: User-agent: * Allow: /   User-agent: CuteStat Disallow: /User-agent: * Allow: / User-agent: CuteStat Disallow: / In case your website […]

How to Test Robots.txt via Google Search Console

First register your website on Google Search Console: https://search.google.com/search-console/about Then browse to this Robots Testing Tool: https://www.google.com/webmasters/tools/robots-testing-tool Select the property (website) and test your Robots.txt. You can test if a web page is blocked by Googlebot.

PHP Generate Secure Random Passwords

This simple PHP function can generate secure random passwords: function generate_strong_password($length = 15, $symbols = array("!", "#", "=", "@", "+", "(")) { $characters = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;   $charactersLength = strlen($characters);   $randomString = ”;   // Generate the random alphanumeric string for ($i = 0; $i < $length; $i++) $randomString .= $characters[random_int(0, $charactersLength – 1)];   […]