AJAX JSON PHPSession Cookie
AJAX JSON PHPSession Cookie
AJAX JSON PHPSession Cookie
12-Oct-19
AJAX – Intro..
Ajax (sometimes capitalized as AJAX) stands for
Asynchronous JavaScript And XML
Ajax is a technique for creating “better, faster, more
responsive web applications”
Web applications with Ajax are supposed to replace all
our traditional desktop applications
These changes are so sweeping that the Ajax-enabled
web is sometimes know as “Web 2.0”
AJAX applications are Browser and Platform
Independent.
How Ajax works
You do something with an HTML form in your browser
JavaScript on the HTML page sends an HTTP request
to the server
The server responds with a small amount of data, rather
than a complete web page
JavaScript uses this data to modify the page
function getXMLHttpRequest( ) {
try { request = new XMLHttpRequest(); }
catch(err1) {
try { request = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(err2) {
try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(err3) {
request = null;
}
}
}
if (request == null) alert("Error creating request object!");
}
Preparing the XMLHttpRequest object
Once you have an XMLHttpRequest object, you have
to prepare it with the open method
request.open(method, URL, asynchronous)
The method is usually 'GET' or 'POST'
This is the state before you make the request, so your callback
function should not actually see this number
1 -- The connection has been initialized
2 -- The request has been sent, and the server is
(presumably) working on it
3 -- The client is receiving the data
4 -- The data has been received and is ready for use
The magic number 200
A ready state of 4 tells you that you got a response--it
doesn’t tell you whether it was a good response
The http_request.status tells you what the server
thought of your request
404 Not found is a status we are all familiar with
200 OK is the response we hope to get
Using response data
When you specify the callback function,
request.onreadystatechange = someFunction;
you can’t specify arguments
Two solutions:
Your function can use the request object as a global variable
This is a very bad idea if you have multiple simultaneous requests
You can assign an anonymous function:
request.onreadystatechange = function() { someFunction(request); }
Here the anonymous function calls your someFunction with the
request object as an argument.
Displaying the response
http_request.onreadystatechange =
function() { showContentsAsAlert(http_request); };
http_request.open('GET', url, true);
http_request.send(null);
function showContentsAsAlert(http_request) {
if (http_request.readyState == 4) { /* 4 means got the response */
if (http_request.status == 200) {
alert(http_request.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
The readyState property
The readyState property defines the current state of the
XMLHttpRequest object.
Here are the possible values for the readyState property:
readyState=0 after you have created the XMLHttpRequest object, but
before you have called the open() method.
readyState=1 after you have called the open() method, but before you
have called send().
readyState=2 after you have called send().
readyState=3 after the browser has established a communication with
the server, but before the server has completed the response.
readyState=4 after the request has been completed, and the response
data have been completely received from the server.
Not all browsers use all states
Usually you are only interested in state 4
Property Description
onreadystatechange Event handler for an event that
fires at every state change
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favclass"] = “D12A";
echo "Session variables are set.";
?>
</body>
</html> demo1.php
Eg. Display Session Variables in PHP
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favclass"] . ".";
?>
</body>
</html>
demo2.php
Eg. To Reset Session Variable in PHP
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>
</body>
</html>
Demo3.php
Eg. To Remove and Destroy Session
Variable in PHP
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
</body>
</html>
PHP Cookies
What is a Cookie?
A cookie is often used to identify a user.
Syntax :
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Eg. To Delete Cookie in PHP
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Check if Cookies are Enabled
<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>