Blog Spot!


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

PHP – Best Practises 2014

There are a number of good practises that you should follow when developing web applications in PHP. Most of these are extremely easy to pick up and some of them will even apply to web application development in general.

  1. Redirect after a successful POST request.
  2. Don’t use the mysql_* functions.
  3. Do not close your PHP tags.
  4. Guard against XSS (a.k.a. Cross-site scripting)!
  5. Don’t echo out HTML!
  6. Separate your logic from your output!
  7. Learn what DRY is.
  8. Never trust your users!
  9. Do not run queries inside loops!
  10. Hash user passwords!
  11. Use prepared statements!
  12. “or die()” needs to die…
  13. Email validation.
  14. Avoid short tags.
  15. Avoid micro-optimizations.
  16. Learn about database normalization.
  17. Be consistent.
  18. Version control.
  19. Bytecode caching.
  20. Learn about common design patterns.
  21. When in doubt, use UTF-8!
  22. Know about the advantages of using an MVC Framework.
  23. Get a grasp on some of the fundamentals of web application security.
  24. Know what database column types to use.
  25. Don’t parse HTML with regular expressions.
  26. var_dump, don’t echo.
  27. Testing your application.
  28. Storing uploaded images.
  29. Re-size your images on upload.
  30. Documentation.
  31. Understand the difference between == and ===
  32. Object Caching.
  33. HTML does NOT provide validation!
  34. JavaScript validation is not a substitute for server-side validation!
  35. Learn about error reporting in PHP.

Read the article!

Added on 25.Jan.2015
Tags: php best-practises article

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