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

Unable to extract and upload PackageInfo.xml

If you get the following error on Microsoft Partner Center: Unable to extract and upload PackageInfo.xmlUnable to extract and upload PackageInfo.xml You just need to repackage the HLKX file to solve the issue. -Rename the current .hckx/hlkx file to .zip -Extract to a folder -Open the folder -Select all items, then right-click and select Send […]

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

DEB.SURY.ORG Updated the Signing Key (March 2019)

On 18 March 2019 OndÅ™ej announced that he changed the signing key: https://www.patreon.com/posts/dpa-new-signing-25451165 I was informed that the current DPA signing key DF3D585DB8F0EB658690A554AC0E47584A7A714D was present on the server maintained by an other user of the repositories that got compromised. I do not believe that the users of the packages.sury.org repositories are at any risk because […]

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"])), […]

Redirect 404 Errors to Homepage with Nginx

You can redirect 404 errors to homepage with Nginx. To do so, just add this inside server {}: # Redirect 404 errors to homepage error_page 404 = @myownredirect; # My custom 302 redirect to homepage location @myownredirect { return 302 /; }# Redirect 404 errors to homepage error_page 404 = @myownredirect; # My custom 302 […]