PHP Sessions Introduction
In this page:
session_start(); // first line, before any output
$_SESSION["key"] = value; // store
$value = $_SESSION["key"]; // read
एक Session क्या है?
एक session आपके app को एक visitor के बारे में information याद रखने देता है -- जैसे उनका username या shopping cart contents -- जब वे pages के बीच move करते हैं, भले ही HTTP खुद को पिछली requests की कोई memory न हो।
Cookies के उलट, actual data server पर stored है; browser सिर्फ एक छोटी session ID रखता है।
उदाहरण: What is a Session?
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['username']` to "Alice"
$_SESSION['username'] = "Alice";
// Print "Only the session ID is stored in the browser's cookie" to the output
echo "Only the session ID is stored in the browser's cookie";
?>
Login to try C/C++/Java/PHP code in the editor
Session Data Store करना
आप $_SESSION superglobal array में सीधे key-value pairs लिखकर session data store करते हैं, उदाहरण के लिए $_SESSION[user_id] = 5। PHP उस data को behind the scenes automatically visitor की session ID से associate करता है।
उदाहरण: Storing Session Data
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['user_id']` to `5`
$_SESSION['user_id'] = 5;
// Print "Stored user_id in the session" to the output
echo "Stored user_id in the session";
?>
Login to try C/C++/Java/PHP code in the editor
Session Data Retrieve करना
जब तक session_start() page के top पर चलता है, आप अपने app में किसी भी script से पहले $_SESSION में store की गई कोई भी चीज़ वापस पढ़ सकते हैं, जो एक login को पूरी site में persist रखने देता है।
उदाहरण: Retrieving Session Data
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['user_id']` to `5`
$_SESSION['user_id'] = 5;
// Print `"Logged in as user #" . $_SESSION['user_id']` to the output
echo "Logged in as user #" . $_SESSION['user_id'];
?>
Login to try C/C++/Java/PHP code in the editor
Session Keys हटाना
unset($_SESSION[key]) call करना user द्वारा store की गई बाकी किसी भी चीज़ को disturb किए बिना session data का एक specific हिस्सा हटाता है, एक single cart item clear करने जैसी चीज़ों के लिए उपयोगी।
उदाहरण: Deleting Session Keys
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['cart']` to `["item1", "item2"]`
$_SESSION['cart'] = ["item1", "item2"];
// Call `unset($_SESSION['cart'][0])`
unset($_SESSION['cart'][0]);
// Print a human-readable dump of `$_SESSION['cart']`
print_r($_SESSION['cart']);
?>
Login to try C/C++/Java/PHP code in the editor
Sessions को Destroy करना
किसी user को पूरी तरह log out करने का मतलब है session_unset() से सारा session data clear करना और फिर session_destroy() से खुद session destroy करना, ताकि किसी shared browser पर अगला visitor कोई leftover state reuse न कर सके।
उदाहरण: Destroying Sessions
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['user_id']` to `5`
$_SESSION['user_id'] = 5;
// Call `session_unset()`
session_unset();
// Call `session_destroy()`
session_destroy();
// Print `isset($_SESSION['user_id']) ? "Still logged in" : "Logged out completely"` to the output
echo isset($_SESSION['user_id']) ? "Still logged in" : "Logged out completely";
?>
Login to try C/C++/Java/PHP code in the editor
$_SESSIONइस्तेमाल करने से पहलेsession_start()भूल जाना।- output भेजे जाने के बाद
session_start()call करना, जो एक headers-already-sent warning का कारण बनता है। - इसे secure किए बिना किसी session में sensitive data store करना।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: