Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //User.php
- <?php
- abstract class User
- {
- protected $userName;
- protected $password;
- protected $wrongLoginCounter = 0;
- protected $age;
- abstract public function checkLogin($username, $password);
- public function setLogin($userName)
- {
- $this->userName = $userName;
- }
- abstract public function setPassword($password);
- abstract public function setAge($age);
- final public function login($username, $password)
- {
- if ($this->wrongLoginCounter >= 3) {
- return false;
- }
- return $this->checkLogin($username, $password);
- }
- }
- ////Admin.php
- <?php
- require __DIR__ . '/User.php';
- class Admin extends User
- {
- public function setAge($age)
- {
- $this->age = $age;
- }
- public function checkLogin($username, $password)
- {
- if (!$username || $username != $this->userName || $this->password != $password) {
- $this->wrongLoginCounter++;
- return false;
- }
- $addressParts = explode('.', $_SERVER['REMOTE_ADDR']);
- $validAddress = [127, 192];
- if (!in_array($addressParts[0], $validAddress)) {
- $this->wrongLoginCounter++;
- return false;
- }
- return true;
- }
- public function setPassword($password)
- {
- if (strlen($password) < 10 || !is_int($this->age) || $this->age <= 0) {
- return false;
- }
- $this->password = $password;
- }
- }
- $admin = new Admin();
- $admin->setLogin('Paweł');
- $admin->setAge(23);
- $admin->setPassword('tajneHasłoPaweła');
- echo '<pre>';
- var_dump($admin->login('Paweł', 'asd'));
- echo '</pre>';
- die;;
- //Client.php
- <?php
- require __DIR__ . '/User.php';
- class Client extends User
- {
- public function setAge($age)
- {
- $this->age = $age;
- }
- public function checkLogin($username, $password)
- {
- if (!$username || $username != $this->userName || $this->password != $password) {
- $this->wrongLoginCounter++;
- return false;
- }
- $this->wrongLoginCounter = 0;
- return true;
- }
- public function setPassword($password)
- {
- if (strlen($password) < 8 || !is_int($this->age) || $this->age <= 17) {
- return false;
- }
- $this->password = $password;
- }
- }
- $client = new Client();
- $client->setLogin('Paweł');
- $client->setAge(23);
- $client->setPassword('tajneHasłoPaweła');
- echo '<pre>';
- var_dump($client->login('Paweł', 'asd'));
- echo '</pre>';
- die;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement