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.
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.
Read the article!