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.
Read the article here.
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!');
});
The localStorage object has this methods:
// 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'));
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());
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.
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.