Category Archives: Uncategorized

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

How to use Highlight.js to highlight code

Using Highlight.js to highlight code is simple: First include this inside the HEAD tag of the HTML page: <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.14.2/highlight.min.js"></script> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.14.2/styles/darcula.min.css" /> <script>hljs.initHighlightingOnLoad();</script><script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.14.2/highlight.min.js"></script> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.14.2/styles/darcula.min.css" /> <script>hljs.initHighlightingOnLoad();</script> Then you can use it like this: <code class="json"> {name: "John", age: 31, city: "New York"} </code><code class="json"> {name: "John", age: 31, city: "New […]

Bootstrap 3 Navbar with Logo Image

The correct way to use a logo image on the Navbar on Bootstrap 3 is: <a href="#" class="navbar-left"><img src="/path/to/image.png"></a><a href="#" class="navbar-left"><img src="/path/to/image.png"></a> Do not use class=”navbar-brand” in A HREF because it adds text-like styles. Reference: https://stackoverflow.com/a/26333342 If the logo image is too on top, just add this code in the .CSS file: .navbar-default .navbar-left img […]

PHP GetHostByName() Slow? Make it Faster

Sometimes gethostbyname() can take up to 5 seconds and more to complete. Here are two smart tricks to make it faster: // 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’);   // Make sure the domain ends in a dot […]

Use Timestamp with new DateTime() in PHP

You can use a unix timestamp within DateTime() like this: $today = new DateTime("@".time()); $tomorrow = new DateTime("@".strtotime("+13 months"));$today = new DateTime("@".time()); $tomorrow = new DateTime("@".strtotime("+13 months")); See that we prepend “@” char before the timestamp.

Calculate Elapsed Time Between Two Dates in PHP

This function is used to calculate elapsed time between two dates: function formatDateDiff($start, $end=null, $ago=" ago", $left=" left") { if(!($start instanceof DateTime)) { $start = new DateTime($start); }   if($end === null) { $end = new DateTime(); }   if(!($end instanceof DateTime)) { $end = new DateTime($start); }   $interval = $end->diff($start);   $elapsedtype = […]