php 简单的认证类。不处理权限或类似的东西。只是把事情放在一起真实快速和肮脏。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 简单的认证类。不处理权限或类似的东西。只是把事情放在一起真实快速和肮脏。相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Auth
 *
 * Verifies authentication credentials
 *
 * @author Mark LaDoux <mark.ladoux@gmail.com>
 */

 class Auth
 {
   /**
    * Create new credentials.
    *
    * @access public
    * @param  string $email    email address for user.
    * @param  string $password password to hash.
    * @return array            Array of values to store in database.
    */
   public function create(string $email, string $password)
   {
     // Create a UTC Timestamp of now for the created field.
     $now = new DateTime;
     $now->setTimezone(new DateTimeZone("UTC"));
     $created = $now->format('Y-m-d H:i:s');

     // check inputs ( Will build better error handling later. )
     if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
       throw new \Exception("Error Processing Request: Invalid E-Mail address.", 1);
     }

     // Create password hash
     $hash = password_hash($password, PASSWORD_DEFAULT);

     return [
       'created'  => $now,
       'email'    => $email,
       'password' => $hash
     ];
   }

   /**
    * Verify password against stored hash.
    *
    * @access public
    * @param  string $password    cleartext password.
    * @param  string $stored_hash stored password hash.
    * @return array               results.
    */
   public function verify(string $password, string $stored_hash)
   {
     // check if passord needs rehash
     $rehash  = password_needs_rehash($stored_hash, PASSWORD_DEFAULT);
     $valid   = password_verify($password, $stored_hash);

     return [
       'rehash' => $rehash,
       'valid'  => $valid
     ];
   }

   /**
    * Create new password hash using current standards.
    *
    * @access public
    * @param  string $password cleartext password.
    * @return string           hashed password.
    */
   public function update(string $password)
   {
     return password_hash($password, PASSWORD_DEFAULT);
   }
 }
 

以上是关于php 简单的认证类。不处理权限或类似的东西。只是把事情放在一起真实快速和肮脏。的主要内容,如果未能解决你的问题,请参考以下文章

关于PHP位运算的简单权限设计

提醒:涉及数据库这类的东西一定需要注意长短链接问题

PHP___认证___访问权限设置

drf--认证,权限,限制

PHP支付库[关闭]

php(模糊)搜索匹配