Blog Spot!


3 Ways to Define a JavaScript Class

JavaScript is a very flexible object-oriented language when it comes to syntax. In this article you can find three ways of defining and instantiating an object. Even if you have already picked your favorite way of doing it, it helps to know some alternatives in order to read other people's code.

It's important to note that there are no classes in JavaScript. Functions can be used to somewhat simulate classes, but in general JavaScript is a class-less language. Everything is an object. And when it comes to inheritance, objects inherit from objects, not classes from classes as in the "class"-ical languages.

  • Using a function
    • Methods defined internally
    • Methods added to the prototype
  • Using object literals
  • Singleton using a function

Read the article here.

Added on 29.Jan.2015
Tags: js class

Detect offline/online state in your Web Applications

You can check for the offline/online state with the following:

if(!navigator.onLine) {
    console.log("You're offline!");
} else {
    console.log("You're online!");
}

You can also attach event listeners when offline or online state occurs.

window.addEventListener('offline', function (e) {
    console.log('Offline Mode!');;
});

window.addEventListener('online', function (e) {
    console.log('Online Mode!');
});

Added on 28.Jan.2015
Tags: js web application

Introduction to localStorage, JavaScript HTML5 Storage

Usage:

The localStorage object has this methods:

  • .setItem(key, value)
  • .getItem(key)
  • .removeItem(key)
// set & get data
localStorage.setItem('data', 'hello world');
console.log(localStorage.data);

localStorage.data2 = 'Maraba';
console.log(localStorage.getItem('data2'));

// remove data
localStorage.removeItem('data');
console.log(localStorage.getItem('data'));

Test if html5 storage is supported:

function supports_html5_storage() {
    try {
        return 'localStorage' in window && window['localStorage'] !== null;
    } catch (e) {
        return false;
    }
}
console.log("Support HTML5 Storage: %s", supports_html5_storage());

Added on 28.Jan.2015
Tags: js localStorage html5

Make HTTP Requests with 'file_get_contents'

GET Request

$res = file_get_contents('http://httpbin.org/get');
echo $res;

POST Request

$data = array ('foo' => 'bar', 'bar' => 'baz');
$data = http_build_query($data);

// for post requests use header:
// Content-Type: application/x-www-form-urlencoded
$opts = [
  'http' => [
    'method' => "POST",
    'header' => "Accept: text/html,application/xhtml+xml\r\n" .
                "Content-Type: application/x-www-form-urlencoded\r\n" .
                "Content-Length: " . strlen($data) . "\r\n",
                "content" => $data
  ]
];

$context = stream_context_create($opts);
$file = file_get_contents('http://httpbin.org/post', FALSE, $context);

Get Request Headers

file_get_contents('http://httpbin.org/get');
echo '<pre>', print_r($http_response_header, TRUE), '</pre>';

Read more: file-get-contents, httpresponseheader, stream_context_create at php.net.

Added on 27.Jan.2015
Tags: php requests headers post get

PHP Best Practise - XSS (Cross-Site Scripting) Defence

XSS a.k.a. Cross-Site Scripting is a vulnerability that allows attackers to execute client-side code on your website. For example: If I enter some JavaScript into a comment form and you display that comment without sanitizing it, the code in question will execute whenever a user loads the page. To defend against this type of vulnerability, you should sanitize user-submitted data before it is printed out onto the page. To achieve this, you can use the function htmlspecialchars:

function e($data) {
    return htmlentities($data, ENT_QUOTES, 'utf-8');
} 

This function will convert special characters into their relevant HTML entities so that they are safe for display.

Added on 25.Jan.2015
Tags: php htmlspecialchars xss best-practises sanitize escape

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top