PHP code example of pavel852 / php-auth
1. Go to this page and download the library: Download pavel852/php-auth library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pavel852 / php-auth example snippets
auth_settings("email_verification=true, numeric_password_length=6, reset_token_expiry=3600, verification_token_expiry=86400, passwd_verification=true");
print auth_version(); // Output: 1.0
$password = generateNumericPassword(6); // e.g., "482915"
$token = generateToken(); // e.g., "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6"
$user = auth_register('john_doe', 'securepassword', 1001, '[email protected] ');
$result = auth_login('john_doe', 'securepassword', $user_data);
if ($result === true) {
echo "Login successful!";
} else {
echo "Login failed: " . $result;
}
auth_logout();
echo "You have been logged out.";
$reset = auth_request_password_reset('[email protected] ', $user_data);
if (is_array($reset)) {
echo "Reset token: " . $reset['reset_token'];
} else {
echo "Error: " . $reset;
}
$result = auth_reset_password('reset_token_here', 'newpassword', $user_data);
if ($result === true) {
echo "Password reset successful!";
} else {
echo "Error: " . $result;
}
$result = auth_verify_email('verification_token_here', $user_data);
if ($result === true) {
echo "Email verified successfully!";
} elseif (is_array($result)) {
echo "Set password using token: " . $result['set_password_token'];
} else {
echo "Error: " . $result;
}
$user = auth_get_user();
echo "Logged in as: " . $user;
if (auth_is_logged_in()) {
echo "User is logged in.";
} else {
echo "User is not logged in.";
}
settings("email_verification=false, passwd_verification=false");
$user = auth_register('john_doe', 'securepassword', 1001, '[email protected] ');
if ($user === true) {
echo "Registration successful!";
} else {
echo "Registration failed: " . $user;
}