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

PHP Authentication Basics

Authentication यह साबित करना है कि आप वही हैं जो कहते हैं, किसी library card दिखाने जैसा। PHP किसी के type किए password को पहले save की गई एक safely scrambled copy से compare करता है।
Syntax
php
$hash = password_hash($password, PASSWORD_DEFAULT);   // store this

if (password_verify($password, $hash)) {
    $_SESSION["user_id"] = $user_id;   // logged in
}

Authentication क्या है?

Authentication यह confirm करने का process है कि एक user genuinely वही है जो वह claim करता है, आमतौर पर एक submitted username और password की तुलना securely stored records से करके बजाय claim पर पूरी तरह भरोसा करने के।

उदाहरण: What is Authentication?

php
<?php
// Declare `$storedUsername`, set to "alice"
$storedUsername = "alice";
// Declare `$submittedUsername`, set to "alice"
$submittedUsername = "alice";
// Check whether `$submittedUsername === $storedUsername`
if ($submittedUsername === $storedUsername) {
    // Print "Identity confirmed" to the output
    echo "Identity confirmed";
}
?>

Password Hashing

Plain-text passwords store करना एक serious security risk है अगर आपका database कभी breach हो जाए; password_hash() एक password को एक one-way hash में convert करता है जिसे password_verify() बाद में original store किए बिना ही check कर सकता है।

उदाहरण: Password Hashing

php
<?php
// Declare `$hash`, set to `password_hash("mypassword123", PASSWORD_DEFAULT)`
$hash = password_hash("mypassword123", PASSWORD_DEFAULT);
// Print `$hash . "\n"` to the output
echo $hash . "\n";
// Print a detailed dump (with types) of `password_verify("mypassword123", $hash)`
var_dump(password_verify("mypassword123", $hash));
?>

Basic Session-Based Login

एक successful login के बाद एक common pattern user की ID को $_SESSION में save करना है, ताकि बाद के page loads हर request पर visitor को फिर login करने के लिए कहने के बजाय उस value को check कर सकें।

उदाहरण: Basic Session-Based Login

php
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['user_id']` to `42`
$_SESSION['user_id'] = 42;
// Print `"Logged in as user #" . $_SESSION['user_id']` to the output
echo "Logged in as user #" . $_SESSION['user_id'];
?>

Pages को Guard करना

एक private page protect करने के लिए, script के top पर expected session variable check करें और अगर यह missing हो तो कोई sensitive content render होने से पहले login page पर redirect करें।

उदाहरण: Guarding Pages

php
<?php
// Call `session_start()`
session_start();
// Check whether `!isset($_SESSION['user_id'])`
if (!isset($_SESSION['user_id'])) {
    // Print "Redirecting to login page" to the output
    echo "Redirecting to login page";
// Otherwise, run this branch
} else {
    // Print `"Welcome back, user #" . $_SESSION['user_id']` to the output
    echo "Welcome back, user #" . $_SESSION['user_id'];
}
?>

Authentication Best Practices

एक successful login के तुरंत बाद session ID regenerate करना session fixation attacks के against एक key defense है, जहाँ एक attacker किसी victim को एक ऐसा session ID इस्तेमाल करने के लिए trick करता है जिसे attacker पहले से control करता है।

उदाहरण: Authentication Best Practices

php
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['user_id']` to `42`
$_SESSION['user_id'] = 42;
// Call `session_regenerate_id(true)`
session_regenerate_id(true);
// Print "Session ID regenerated to prevent session fixation" to the output
echo "Session ID regenerated to prevent session fixation";
?>
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. password_hash इस्तेमाल करने के बजाय passwords को plain text में store करना।
  2. password_verify के बजाय == से passwords compare करना।
  3. protected pages पर session check करना भूल जाना।
चैप्टर सारांश
  • Sessions requests में एक user के बारे में data store करते हैं, और cookies browser में data store करती हैं।
  • Session और cookie management यह बताते हैं कि उन्हें कैसे बनाएँ, पढ़ें, और हटाएँ।
  • Authentication basics users को identify करने के लिए sessions और cookies पर build करते हैं।
🔒

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.