{"id":127917,"date":"2022-09-06T05:25:02","date_gmt":"2022-09-06T05:25:02","guid":{"rendered":"https:\/\/phppot.com\/?p=19285"},"modified":"2022-09-06T05:25:02","modified_gmt":"2022-09-06T05:25:02","slug":"php-session-destroy-after-30-minutes","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/06\/php-session-destroy-after-30-minutes\/","title":{"rendered":"PHP Session Destroy after 30 Minutes"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.1489361702128\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on September 6th, 2022.<\/div>\n<p>PHP has a core function session_destroy() to clear all the session values. It is a simple no-argument function that returns a boolean true or false.<\/p>\n<p>The PHP session ID is stored in a cookie by default. Generally that session cookie file is name PHPSESSID. The session_destroy function will not <a href=\"https:\/\/phppot.com\/php\/php-session-time-set-unset\/\">unset the session<\/a> id in the cookie.<\/p>\n<p>To destroy the session \u2018completely\u2019, the session ID must also be unset.<\/p>\n<p>This quick example uses session_destroy() to destroy the session. It uses the set_cookie() method to kill the entirety by expiring the PHP session ID.<\/p>\n<div class=\"post-section-highlight\" readability=\"43\">\n<h2>Quick example<\/h2>\n<p class=\"code-heading\">destroy-session.php<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n\/\/ Always remember to initialize the session,\n\/\/ even before attempting to destroy it. \/\/ Destroy all the session variables.\n$_SESSION = array(); \/\/ delete the session cookie also to destroy the session\nif (ini_get(\"session.use_cookies\")) { $cookieParam = session_get_cookie_params(); setcookie(session_name(), '', time() - 42000, $cookieParam[\"path\"], $cookieParam[\"domain\"], $cookieParam[\"secure\"], $cookieParam[\"httponly\"]);\n} \/\/ as a last step, destroy the session.\nsession_destroy();\n<\/code><\/pre>\n<\/div>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li>Use session_start() to reinitiate the session after the <a href=\"https:\/\/www.php.net\/manual\/en\/function.session-destroy.php\" target=\"_blank\" rel=\"noopener\">PHP session destroy<\/a>.<\/li>\n<li>Use PHP $_SESSION to <a href=\"https:\/\/phppot.com\/php\/php-unlink-vs-unset\/\">unset<\/a> a particular session variable. For an older PHP version, use session_unset().<\/li>\n<\/ol>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-19323\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-session-destroy-output-550x147.jpg\" alt=\"php session destroy output\" width=\"550\" height=\"147\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-session-destroy-output-550x147.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-session-destroy-output-300x80.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-session-destroy-output-768x206.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-session-destroy-output.jpg 1000w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<h2>About this login session_destroy() example<\/h2>\n<p>Let\u2019s create a <a href=\"https:\/\/phppot.com\/php\/php-login-script-with-session\/\">login example code to use PHP session<\/a>, session_destroy and all. It allows users to login and logout from the current session. Use this code if you are looking for a complete <a href=\"https:\/\/phppot.com\/php\/user-registration-in-php-with-login-form-with-mysql-and-code-download\/\">user registration and login in PHP script<\/a>.<\/p>\n<p>This example provides an automatic login session expiry feature.<\/p>\n<h2>Landing page with a login form<\/h2>\n<p>This form posts the username and the password entered by the user. It verifies the login credentials in PHP.<\/p>\n<p>On successful login, it stores the logged-in state into a <a href=\"https:\/\/phppot.com\/php\/session-vs-cookies\/\">PHP session<\/a>. It sets the expiry time to 30 minutes from the last login time.<\/p>\n<p>It stores the last login time and the expiry time into the PHP session. These two session variables are used to expire the session automatically.<\/p>\n<p class=\"code-heading\">login.php<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php-template\">&lt;?php\nsession_start();\n$expirtyMinutes = 1;\n?&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;PHP Session Destroy after 30 Minutes&lt;\/title&gt;\n&lt;link rel='stylesheet' href='style.css' type='text\/css' \/&gt;\n&lt;link rel='stylesheet' href='form.css' type='text\/css' \/&gt;\n&lt;\/head&gt;\n&lt;body&gt; &lt;div class=\"phppot-container\"&gt; &lt;h1&gt;Login&lt;\/h1&gt; &lt;form name=\"login-form\" method=\"post\"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Username&lt;\/td&gt; &lt;td&gt;&lt;input type=\"text\" name=\"username\"&gt;&lt;\/td&gt; &lt;\/tr&gt; &lt;tr&gt; &lt;td&gt;Password&lt;\/td&gt; &lt;td&gt;&lt;input type=\"password\" name=\"password\"&gt;&lt;\/td&gt; &lt;\/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type=\"submit\" value=\"Sign in\" name=\"submit\"&gt;&lt;\/td&gt; &lt;\/tr&gt; &lt;\/table&gt; &lt;\/form&gt;\n&lt;?php\nif (isset($_POST['submit'])) { $usernameRef = \"admin\"; $passwordRef = \"test\"; $username = $_POST['username']; $password = $_POST['password']; \/\/ here in this example code focus is session destroy \/ expiry only \/\/ refer for registration and login code https:\/\/phppot.com\/php\/user-registration-in-php-with-login-form-with-mysql-and-code-download\/ if ($usernameRef == $username &amp;&amp; $passwordRef == $password) { $_SESSION['login-user'] = $username; \/\/ login time is stored as reference $_SESSION['ref-time'] = time(); \/\/ Storing the logged in time. \/\/ Expiring session in 30 minutes from the login time. \/\/ See this is 30 minutes from login time. It is not 'last active time'. \/\/ If you want to expire after last active time, then this time needs \/\/ to be updated after every use of the system. \/\/ you can adjust $expirtyMinutes as per your need \/\/ for testing this code, change it to 1, so that the session \/\/ will expire in one minute \/\/ set the expiry time and $_SESSION['expiry-time'] = time() + ($expirtyMinutes * 60); \/\/ redirect to home \/\/ do not include home page, it should be a redirect header('Location: home.php'); } else { echo \"Wrong username or password. Try again!\"; }\n}\n?&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-19324\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/login-550x216.jpg\" alt=\"login\" width=\"550\" height=\"216\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/login-550x216.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/login-300x118.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/login-768x301.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/login.jpg 1000w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<h2>Dashboard validates PHP login session and displays login, and logout links<\/h2>\n<p>This is the target page redirected after login. It shows the logout link if the logged-in session exists.<\/p>\n<p>Once <a href=\"https:\/\/phppot.com\/php\/user-login-session-timeout-logout-in-php\/\">timeout<\/a>, it calls the destroy-session.php code to destroy all the sessions.<\/p>\n<p>If the 30 minutes expiry time is reached or the session is empty, it asks the user to log in.<\/p>\n<p class=\"code-heading\">home.php<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php-template\">&lt;?php\nsession_start();\n?&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;PHP Session Destroy after 30 Minutes&lt;\/title&gt;\n&lt;link rel='stylesheet' href='style.css' type='text\/css' \/&gt;\n&lt;link rel='stylesheet' href='form.css' type='text\/css' \/&gt;\n&lt;\/head&gt;\n&lt;body&gt; &lt;div class=\"phppot-container\"&gt;\n&lt;?php\nif (! isset($_SESSION['login-user'])) { echo \"Login again!&lt;br&gt;&lt;br&gt;\"; echo \"&lt;a href='login.php'&gt;Login&lt;\/a&gt;\";\n} else { $currentTime = time(); if ($currentTime &gt; $_SESSION['expiry-time']) { require_once __DIR__ . '\/destroy-session.php'; echo \"Session expired!&lt;br&gt;&lt;br&gt;&lt;a href='login.php'&gt;Login&lt;\/a&gt;\"; } else { ?&gt; &lt;h1&gt;Welcome &lt;?php echo $_SESSION['login-user'];?&gt;!&lt;\/h1&gt; &lt;a href='logout.php'&gt;Log out&lt;\/a&gt;\n&lt;?php }\n}\n?&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>This PHP code is used for users who want to log out before the session expiry time.<\/p>\n<p>It destroys the session by requiring the destroy-session.php code. Then, it <a href=\"https:\/\/phppot.com\/php\/php-redirect\/\">redirects the user<\/a> to the login page.<\/p>\n<p class=\"code-heading\">logout.php<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\nsession_start();\nrequire_once __DIR__ . '\/destroy-session.php';\nheader('Location: login.php');\n?&gt;\n<\/code><\/pre>\n<p>I hope this example helps to understand how to destroy PHP sessions. And also, this is a perfect scenario that is suitable for explaining the need of destroying the session.<br \/><a class=\"download\" href=\"https:\/\/phppot.com\/downloads\/php\/php-session-destroy.zip\">Download<\/a><\/p>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/php\/php-session-destroy\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on September 6th, 2022. PHP has a core function session_destroy() to clear all the session values. It is a simple no-argument function that returns a boolean true or false. The PHP session ID is stored in a cookie by default. Generally that session cookie file is name PHPSESSID. The session_destroy function [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":127918,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-127917","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127917"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127917\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/127918"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}