Sunday, March 15

__sleep and __wakeup in PHP language.

Meaning of ___sleep:- It returns the array of all the variables which need to be saved.
Meaning of ___wakeup:- It is used to retrieve the array of all the variables.

Here is a code is given:
    <?php
   
        $str = ‘Hello, there.nHow are you?nThanks for visiting Our Website’;
       
        print $str;
   
    ?>

   
Slowing down bruit force attacks on wrong password attempts

    <?php
        public function handle_login() {
            if($uid = user::check_password($_REQUEST['email'], $_REQUEST['password'])) {
                return self::authenticate_user($uid);
            }
            else {
                // delay failed output by 2 seconds
                // to prevent bruit force attacks
                sleep(2);
                return self::login_failed();
            }
        }
    ?>



The intended use of __wakeup() is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.

    Example #1 Sleep and wakeup:

    <?php
        class Connection
        {
            protected $link;
            private $dsn, $username, $password;
           
            public function __construct($dsn, $username, $password)
            {
                $this->dsn = $dsn;
                $this->username = $username;
                $this->password = $password;
                $this->connect();
            }
           
            private function connect()
            {
                $this->link = new PDO($this->dsn, $this->username, $this->password);
            }
           
            public function __sleep()
            {
                return array('dsn', 'username', 'password');
            }
           
            public function __wakeup()
            {
                $this->connect();
            }
        }
    ?>
 

SOURCE: php.net

No comments:

Post a Comment