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

PHP Session Management

Session management उस visitor ticket को thieves से safe रखने के बारे में है। आप important moments पर ticket को एक fresh वाले से swap करते हैं और visitor के जाने पर इसे सही से बंद कर देते हैं।
Syntax
php
session_start();
session_regenerate_id(true);                 // new ID after login

session_set_cookie_params(lifetime, path, domain, secure, httponly);   // before session_start()
$_SESSION["last_activity"] = time();

Session Security Checks

Session hijacking तब होता है जब कोई attacker एक valid session ID चुराकर user बनने का नाटक करता है; login के तुरंत बाद session_regenerate_id() से session ID regenerate करना किसी भी ID को invalid कर देता है जो attacker ने शायद पहले से capture किया हो।

उदाहरण: Session Security Checks

php
<?php
// Call `session_start()`
session_start();
// Call `session_regenerate_id(true)`
session_regenerate_id(true);
// Print "Session ID regenerated after login" to the output
echo "Session ID regenerated after login";
?>

Session Timeout Limits Set करना

एक simple timeout pattern $_SESSION में एक last-activity timestamp store करता है और हर page load पर इसे check करता है, बहुत ज़्यादा समय बीत जाने पर एक logout force करते हुए -- shared या public computers पर खुले छोड़े गए accounts को protect करते हुए।

उदाहरण: Setting Session Timeout Limits

php
<?php
// Call `session_start()`
session_start();
// Declare `$timeout`, set to `1800`
$timeout = 1800;
// Set `$_SESSION['last_activity']` to `time() - 2000`
$_SESSION['last_activity'] = time() - 2000;
// Check whether `time() - $_SESSION['last_activity'] > $timeout`
if (time() - $_SESSION['last_activity'] > $timeout) {
    // Print "Session expired -- logging out" to the output
    echo "Session expired -- logging out";
// Otherwise, run this branch
} else {
    // Set `$_SESSION['last_activity']` to `time()`
    $_SESSION['last_activity'] = time();
}
?>

Custom Session Configuration

session_start() call करने से पहले, आप session_set_cookie_params() से cookie lifetime, path, और security flags configure कर सकते हैं, आपको control देते हुए कि एक session cookie कितनी देर survive करती है और कहाँ valid है।

उदाहरण: Custom Session Configuration

php
<?php
// Call `session_set_cookie_params(3600, '/', '', true, true)`
session_set_cookie_params(3600, '/', '', true, true);
// Call `session_start()`
session_start();
// Print "Session cookie configured before starting" to the output
echo "Session cookie configured before starting";
?>

Session Status और States

session_status() आपको बताता है कि sessions disabled, inactive, या पहले से active हैं, जो session_start() को दो बार call करने से बचने या sensitive actions perform करने से पहले session state check करने के लिए उपयोगी है।

उदाहरण: Session Status and States

php
<?php
// Print `session_status() === PHP_SESSION_NONE ? "No session yet\n" : "Session active\n"` to the output
echo session_status() === PHP_SESSION_NONE ? "No session yet\n" : "Session active\n";
// Call `session_start()`
session_start();
// Print `session_status() === PHP_SESSION_ACTIVE ? "Now active" : "Still inactive"` to the output
echo session_status() === PHP_SESSION_ACTIVE ? "Now active" : "Still inactive";
?>

Session Name और ID

session_name() और session_id() आपको PHP के under the hood इस्तेमाल किए identifiers inspect या override करने देते हैं, जो तब मायने रखता है जब same domain पर कई independent apps चल रहे हों जिन्हें session data share नहीं करना चाहिए।

उदाहरण: Session Name and ID

php
<?php
// Call `session_start()`
session_start();
// Print `session_name() . ": " . session_id()` to the output
echo session_name() . ": " . session_id();
?>
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. login के बाद session_regenerate_id(true) call न करना, जो site को session fixation के लिए खुला छोड़ देता है।
  2. logout पर $_SESSION clear करना और session destroy करना भूल जाना, ताकि user logged in रहे।
  3. timeouts के लिए सिर्फ cookie lifetime पर भरोसा करना, जबकि server को last-activity time भी check करना चाहिए।
🔒

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.