Category Archives: Uncategorized

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 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 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: Then browse to this 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="//"></script> <link rel="stylesheet" href="//" /> <script>hljs.initHighlightingOnLoad();</script><script src="//"></script> <link rel="stylesheet" href="//" /> <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: If the logo image is too on top, just add this code in the .CSS file: .navbar-default .navbar-left img […]