Today, I learned about the Session and header.
Session
A Session starts when you launch a website or web app and ends when you leave the website or close your browser window.
- Session allows you to maintain state information even when cllients disable cookies in their Web browsers.
- Session cookies contain information that is stored in a temporary memony location
- Session is known as transient cookies, non-persistent cookies, or temproary cookies
In PHP, how does session state work?
Starting a session
- You must call the session_start() function before you send the Web browser any output.
- PHP checks if there is already a session ID coming from a cookie! POSTed data, or query string
- If ID exists then the data is read from the session's state file as name-value pairs and stored in the $_SESSION variable.
- If there is no session ID, then session_start() generates a unique session ID to identify the session.
<?php
session_start();
?>
- If a client's Web browser is configured. to accept cookies, the session ID is assigned to a temporary cookie names PHPSESSID.
- The session ID maybe obtained from: session_id()
$_SESSION
- Session. state information is stored in the $_SESSION autoglobal.
- When the session start() is called, PHP either initializes a new $_SESSION autoglobal or retrieves any variabels for the current session (based on the session ID) into the $_SESSION autoglobal.
<?php
include_once("ShoppingCart.class.php");
session strat();
// check for existence of session object before accessing
if (isset($_SESSION["Cart"])){
$_SESSION["Cart"] = new ShoppingCart();
}
$cart = $_SESSION["Cart"];
?>
Delete a Session
- To delete a session manually, perform the following steps:
- Execute session_strat();
- Use the session_destroy() to delete session
- Modify the Registration/Log In page so it deletes any existing user session whenever a user opens it.
<?php
session_start();
session_destroy();
?>
Session Without Cookies
- If cookie is disabled, manually pass the session ID as a query string or hidden from field to any Web pages that are called as part of the current session
- The session ID maybe obtained from: session_id()
<p><a href="sessionNoCookie.php?PHPSESSID=<?=session_id()?>">Visit again</a></p>
<p><a href="sessionNoCookie.php?<?=SID?>">Visit again</a></p>
<input type="hidden" name="PHPSESSID" value="<?= session_id() ?>"/>
'Web Programming ๐ > Back-End ๐ฅท๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
URL Redirection in PHP (0) | 2023.05.07 |
---|---|
Cookies (0) | 2023.05.03 |
PHP Exercise: GuessGame (0) | 2023.04.25 |
PHP Exercise: popcorn bill (0) | 2023.04.25 |
PHP Form (0) | 2023.04.25 |