PHP
PHP Registration Form using GET, POST Methods with Example
What is Form? When you login into a website or into your mail box, you are interacting with a...
A cookie is a small file with the maximum size of 4KB that the web server stores on the client computer.
Once a cookie has been set, all page requests that follow return the cookie name and value.
A cookie can only be read from the domain that it has been issued from. For example, a cookie set using the domain www.gtupapers.com can not be read from the domain career.gtupapers.com.
Most of the websites on the internet display elements from other domains such as advertising. The domains serving these elements can also set their own cookies. These are known as third party cookies.
A cookie created by a user can only be visible to them. Other users cannot see its value.
Most web browsers have options for disabling cookies, third party cookies or both.
If this is the case then PHP responds by passing the cookie token in the URL.
The diagram shown below illustrates how cookies work.
Here,
1) A user requests for a page that stores cookies
2) The server sets the cookie on the user’s computer
3) Other page requests from the user will return the cookie name and value
In this tutorial, you will learn-
Http is a stateless protocol; cookies allow us to track the state of the application using small files stored on the user’s computer.
The path were the cookies are stored depends on the browser.
Internet Explorer usually stores them in Temporal Internet Files folder.
Personalizing the user experience – this is achieved by allowing users to select their preferences.
The page requested that follow are personalized based on the set preferences in the cookies.
Let’s now look at the basic syntax used to create a cookie.
<?php setcookie(cookie_name, cookie_value, [expiry_time], [cookie_path], [domain], [secure], [httponly]); ?>
HERE,
Note: the php set cookie function must be executed before the HTML opening tag.
Let’s now look at an example that uses cookies.
We will create a basic program that allows us to store the user name in a cookie that expires after ten seconds.
The code below shows the implementation of the above example “cookies.php”.
<?php
setcookie("user_name", "gtupapers", time()+ 60,'/'); // expires after 60 seconds
echo 'the cookie has been set for 60 seconds';
?>Output:
the cookie has been set for 60 seconds
Create another file named “cookies_read.php” with the following code.
<?php
print_r($_COOKIE); //output the contents of the cookie array variable
?>Output:
Array ( [PHPSESSID] => h5onbf7pctbr0t68adugdp2611 [user_name] => gtupapers )
Note: $_COOKIE is a PHP built in super global variable.
It contains the names and values of all the set cookies.
The number of values that the
$_COOKIE array can contain depends on the memory size set in php.ini.
The default value is 1GB.
Testing our application.
Let’s assume you have saved your PHP files in phptus folder.
Note: Only an empty array has been displayed
Wait for a minute then click on refresh button again. What results did you get?
<?php
setcookie("user_name", "gtupapers", time() - 360,'/');
?>In order to create a session, you must first call the PHP session_start function and then store your values in the $_SESSION array variable.
Let’s suppose we want to know the number of times that a page has been loaded, we can use a session to do that.
The code below shows how to create and retrieve values from sessions
<?php
session_start(); //start the PHP_session function
if(isset($_SESSION['page_count']))
{
$_SESSION['page_count'] += 1;
}
else
{
$_SESSION['page_count'] = 1;
}
echo 'You are visitor number ' . $_SESSION['page_count'];
?>Output:
You are visitor number 1
The session_destroy() function is used to destroy the whole Php session variables.
If you want to destroy only a session single item, you use the unset() function.
The code below illustrates how to use both methods.
<?php session_destroy(); //destroy entire session ?>
<?php unset($_SESSION['product']); //destroy product session item ?>
Session_destroy removes all the session data including cookies associated with the session.
Unset only frees the individual session variables.
Other data remains intact.
What is Form? When you login into a website or into your mail box, you are interacting with a...
PHP Is not fair to compare PHP vs JavaScript, as they both have different purposes for web-site...
In this tutorial, you will learn- PHP Data Types PHP Variable Use of variables Variable type...
PHP is a server-side scripting language used to develop static and dynamic websites or web...
What is XML? XML is the acronym for Extensible Markup Language. XML is used to structure, store...
What is XAMPP? XAMPP is an open source cross platform web server, MySQL database engine, and PHP...