PHP Session Management
In this page:
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
Session Status और States
session_status() आपको बताता है कि sessions disabled, inactive, या पहले से active हैं, जो session_start() को दो बार call करने से बचने या sensitive actions perform करने से पहले session state check करने के लिए उपयोगी है।
उदाहरण: Session Status and States
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// Call `session_start()`
session_start();
// Print `session_name() . ": " . session_id()` to the output
echo session_name() . ": " . session_id();
?>
Login to try C/C++/Java/PHP code in the editor
- login के बाद
session_regenerate_id(true)call न करना, जो site को session fixation के लिए खुला छोड़ देता है। - logout पर
$_SESSIONclear करना और session destroy करना भूल जाना, ताकि user logged in रहे। - timeouts के लिए सिर्फ cookie lifetime पर भरोसा करना, जबकि server को last-activity time भी check करना चाहिए।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: