← Back to PHP Course | Chapter 12: Sessions & Cookies | Lesson 1 of 5

PHP Sessions Introduction

एक session किसी website को यह याद रखने में मदद करता है कि visitor click करते हुए कौन है, किसी coat check जैसा जो आपका ticket number रखता है। Site आपका data रखती है और हर page पर जानती है कि यह आप हैं।
Syntax
php
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
<?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";
?>

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
<?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";
?>

Session Data Retrieve करना

जब तक session_start() page के top पर चलता है, आप अपने app में किसी भी script से पहले $_SESSION में store की गई कोई भी चीज़ वापस पढ़ सकते हैं, जो एक login को पूरी site में persist रखने देता है।

उदाहरण: Retrieving Session Data

php
<?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'];
?>

Session Keys हटाना

unset($_SESSION[key]) call करना user द्वारा store की गई बाकी किसी भी चीज़ को disturb किए बिना session data का एक specific हिस्सा हटाता है, एक single cart item clear करने जैसी चीज़ों के लिए उपयोगी।

उदाहरण: Deleting Session Keys

php
<?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']);
?>

Sessions को Destroy करना

किसी user को पूरी तरह log out करने का मतलब है session_unset() से सारा session data clear करना और फिर session_destroy() से खुद session destroy करना, ताकि किसी shared browser पर अगला visitor कोई leftover state reuse न कर सके।

उदाहरण: Destroying Sessions

php
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. $_SESSION इस्तेमाल करने से पहले session_start() भूल जाना।
  2. output भेजे जाने के बाद session_start() call करना, जो एक headers-already-sent warning का कारण बनता है।
  3. इसे secure किए बिना किसी session में sensitive data store करना।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.