PHP Authentication Basics
In this page:
$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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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));
?>
Login to try C/C++/Java/PHP code in the editor
Basic Session-Based Login
एक successful login के बाद एक common pattern user की ID को $_SESSION में save करना है, ताकि बाद के page loads हर request पर visitor को फिर login करने के लिए कहने के बजाय उस value को check कर सकें।
उदाहरण: Basic Session-Based Login
<?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'];
?>
Login to try C/C++/Java/PHP code in the editor
Pages को Guard करना
एक private page protect करने के लिए, script के top पर expected session variable check करें और अगर यह missing हो तो कोई sensitive content render होने से पहले login page पर redirect करें।
उदाहरण: Guarding Pages
<?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'];
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
password_hashइस्तेमाल करने के बजाय passwords को plain text में store करना।password_verifyके बजाय==से passwords compare करना।- 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: