Загрузка...

Understanding PHP sessions and cookies

Both PHP Sessions and Cookies are mechanism to store and retrieve data across multiple HTTP requests. But the purpose of storing the data and locations of storing are different.

Session values are stored on server-side. These values are stored to share common information across different pages. These values are lost once the browser closes.

Cookie values are stored on client-side or browser. These values are mainly user information. These values are saved permanently until cookie values deleted from the browser.

The pages where the session values are required must start the session using function session_start().

Session values are created using PHP Super Global Variable $_SESSION with a user-defined name.
$_SESSION["username"] = "john.doe@gmail.com";

Session values can be accessed using session variables.
echo $_SESSION["username"];

Session values can be deleted using function session_destroy().

On the other hand a cookie can be created using function setcookie().
setcookie("user", "John");

Cookie value can be accessed using PHP Super Global Variable $_COOKIE with the cookie name.
echo $_COOKIE["user"];

Cookie value can be deleted by setting a cookie expiration time in seconds. This cookie will be destroyed in one day or 86400 seconds.
setcookie("user", "John", 86400);

Both session and cookie are playing important role in web development.

Видео Understanding PHP sessions and cookies канала PHP Explained
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять