{"id":411,"date":"2021-06-10T14:49:34","date_gmt":"2021-06-10T13:49:34","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/?page_id=411"},"modified":"2021-06-10T15:51:41","modified_gmt":"2021-06-10T14:51:41","slug":"session-variables","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/php\/session-variables\/","title":{"rendered":"Session Variables"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-196\" src=\"https:\/\/blogs.glowscotland.org.uk\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/05211152\/sqa-ah.png\" alt=\"\" width=\"100\" height=\"97\" \/><a href=\"https:\/\/www.w3schools.com\/pHp\/php_sessions.asp\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"image-59 alignright\" src=\"https:\/\/glow-prod-sh.s3.eu-west-1.amazonaws.com\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/03210120\/w3schools-150x150.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&#038;X-Amz-Security-Token=IQoJb3JpZ2luX2VjEP7%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJIMEYCIQDpkb9sUuUELuu1PNkpe%2F3pBUywOdkabIkOGOhGgY5RxQIhAOiup64wv4Vumap%2Fxw4w%2FKDSYSx%2FKjQIF2SAMSlYDsxuKsIFCMb%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEQBRoMMjczNTcwMTk1NDMzIgzu%2B36sRj1V70pDcssqlgWc%2Bg%2BfAwaVSrEuCLUVbrFI%2FS5MjzlnAF9qXtILgLS0ibySTEM2oLfPhlrPQdQH2ECCkyn3lxO1X%2BW%2B%2B6EaGkn4ueWpKt3meVtGTrZPeqKEUq6JLUIO9SRhoUind8pyEVE96gH8FdUXMmEJK4DV4DwCSTIH6r%2F6X5ihy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=ASIAT7MQN47UTWHW6KOB%2F20260609%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260609T053037Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=4fd101bdc3ec24a51fc9491c05d1b6a78d57897e494d7b36475e6022e1ba87ca\" alt=\"\" width=\"50\" height=\"47\" \/><\/a>When a browser loads a new web page, it forgets all the information from the previous page. A PHP session is a way of storing information within a website, so that it can be retained and used across multiple pages.<br \/>\nExamples of session use are:<\/p>\n<ul>\n<li>displaying a user\u2019s id on multiple pages, following a successful login<\/li>\n<li>retaining selected items in a shopping cart, as the user navigates from page to page<\/li>\n<li>retaining values, such as a user\u2019s quiz Score, when each new question page loads<\/li>\n<\/ul>\n<h1>Starting a session<\/h1>\n<p>The following PHP function is used to start a session. This should be placed at the top of a page, before any HTML code. If data is being passed between multiple pages, each page that requires access to the session should contain the PHP code below.<\/p>\n<p>When a new session starts, a user key is stored on the user\u2019s computer. The session_start() function looks to see if a user key exists. If it does, the current session is continued. If no user key exists, a new session is started.<\/p>\n<pre class=\"brush: php; highlight: [1,2,3]; title: PHP; notranslate\" title=\"PHP\">&lt;?php\r\n   session_start();\r\n?&gt;\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h1>Session Variables<\/h1>\n<p>Session variables can be assigned values, or have their values checked in any file that contains the session_start() function.<\/p>\n<pre class=\"brush: php; highlight: [2,5]; title: PHP; notranslate\" title=\"PHP\">\r\nif (!isset() {$_SESSION&#x5B;'loggedin']) {\r\n    $_SESSION&#x5B;'loggedin'] = False;\r\n}\r\n...\r\n$_SESSION&#x5B;'username'] = $username;\r\n<\/pre>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\nif ($_SESSION&#x5B;'loggedin'] == True) {\r\n    echo &quot;&lt;p&gt;Logged in as: &quot; . $_SESSION&#x5B;'username'] . &quot;&lt;\/p&gt;&quot;;\r\n    \/\/ display rest of page\r\n} else {\r\n    echo &quot;&lt;p&gt;Not logged in&lt;\/p&gt;&quot;;\r\n    \/\/ display page with a login button\r\n}\r\n<\/pre>\n<h1>Ending a session<\/h1>\n<p>The PHP function session_destroy() is used to end a session. In this example, logout.php is ran when a &#8220;Log Out&#8221; button on other pages is clicked. The code below destroys the session and then reloads the home page.<\/p>\n<pre class=\"brush: php; title: logout.php; notranslate\" title=\"logout.php\">\r\n&lt;?php\r\n    session_start();\r\n?&gt;\r\n \r\n&lt;?php\r\n    session_destroy();\r\n    include &quot;index.php&quot;;\r\n    die;\r\n\/php&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When a browser loads a new web page, it forgets all the information from the previous page. A PHP session is a way of storing information within a website, so that it can be retained and used across multiple pages. Examples of session use are: displaying a user\u2019s id on multiple pages, following a successful login retaining selected items in a shopping cart, as the user navigates from page to<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/php\/session-variables\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":343,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-411","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/comments?post=411"}],"version-history":[{"count":11,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/411\/revisions"}],"predecessor-version":[{"id":425,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/411\/revisions\/425"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/343"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/media?parent=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}