PHP
PHP Date() & Time Function: How to Get Current Timestamp?
PHP date() Function PHP date function is an in-built function that simplify working with date data...
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.guru99.com can not be read from the domain career.guru99.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", "Guru99", 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] => Guru99 )
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", "Guru99", 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.
PHP date() Function PHP date function is an in-built function that simplify working with date data...
What is a control structure? Code execution can be grouped into categories as shown below...
What is XAMPP? XAMPP is an open-source, cross-platform web server that consists of a web server,...
Why use Comments? If you don’t work on the source code for some time, it’s easy to forget what the code...
In this tutorial, you will learn- PHP Data Types PHP Variable Use of variables Variable type...
Web development is the process of building and maintenance of websites. Web development has a wide...