Advertisement
Guest User

Untitled

a guest
Nov 7th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.26 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. /**
  10.  * Description of AccountController
  11.  *
  12.  * @author PC
  13.  */
  14. use Phalcon\Mvc\Controller;
  15. use Phalcon\Escaper;
  16.  
  17.  
  18. class AccountController extends Controller{
  19.     //put your code here
  20.      
  21.      public function initialize()
  22.     {
  23.          
  24.     }
  25.     public function indexAction()
  26.     {
  27.         if(isset($_SESSION['auth']))
  28.         {
  29.         $id = $this->session->get('auth');
  30.          $user = User::findFirst(
  31.                  [
  32.                      "id = :id:" =>
  33.                  [
  34.                      "id" => $id['id'],
  35.                  ]
  36.                 ]);
  37.          $un = $user->name;
  38.          $this->view->un = $un;
  39.         }
  40.     }
  41.    
  42.     public function registerAction()
  43.     {
  44.         $created_at = date("Y.m.d");
  45.         $update_at = date("Y.m.d");
  46.        
  47.         if (isset($_POST['submit']))
  48.        {
  49.             $name = $_POST['name'];
  50.             $email = $_POST['email'];
  51.             $password1 = $_POST['password1'];
  52.             $password2 = $_POST['password2'];
  53.             $name = htmlspecialchars($name);
  54.             $email = htmlspecialchars($email);
  55.             if ($password1 == $password2)
  56.             {
  57.                 $password = $password1;
  58.                 $user = new User();
  59.                 $user->name = $name;
  60.                 $user->email = $email;
  61.                 $user->password = MD5($password);
  62.                 $user->created_at = $created_at;
  63.                 $user->update_at = $update_at;
  64.                 if($user->save())
  65.                 {
  66.                     echo "Вы успешно зарегистрировались!";
  67.                 }
  68.                 else
  69.                     {
  70.                         echo "Что-то пошло не так: ";
  71.  
  72.                         $messages = $user->getMessages();
  73.  
  74.                         foreach ($messages as $message) {
  75.                             echo $message->getMessage(), "<br/>";
  76.                     }
  77.                 }
  78.             }
  79.             $this->view->disable();
  80.            
  81.        }
  82.        
  83.     }
  84.     private function _registerSession($user)
  85.     {
  86.         $this->session->set(
  87.             'auth',
  88.             [
  89.                 'id'   => $user->id,
  90.                 'name' => $user->name,
  91.             ]
  92.         );
  93.     }
  94.     public function loginAction()
  95.     {
  96.         if (isset($_POST['submit']))
  97.        {
  98.             $name = $_POST['nameoremail'];
  99.             $password = $_POST['password'];
  100.             $name = htmlspecialchars($name);
  101.             $password = MD5($password);
  102.             $ras = new User;
  103.             $user = $ras::findFirst(
  104.                 [
  105.                     "(name = :name: OR email = :email:) AND password = :password:",
  106.                     'bind' => [
  107.                         'name' => $name,
  108.                         'email'=> $name,
  109.                         'password' => $password,
  110.                     ]
  111.                 ]
  112.             );
  113.                
  114.            
  115.             if ($user !== false)
  116.             {
  117.             //$this->_registerSession($user);
  118.            
  119.             $this->session->set(
  120.             'auth',
  121.             [
  122.                 'id'   => $user->id,
  123.                 'name' => $user->name,
  124.             ]
  125.         );
  126.  
  127.            
  128.                $this->view->name = $user->name;
  129.                return $this->dispatcher->forward(
  130.             [
  131.             "controller" => "account",
  132.             "action"     => "index",
  133.             ]
  134.             );
  135.             }
  136.             else
  137.                 {
  138.                 $this->view->disable();
  139.                 echo 'Нет пользователя с такими данными';
  140.                 echo "<p><a href="."/account/login".">Назад</a></p>";
  141.                 }
  142.      
  143.         }
  144.  
  145. }
  146.                
  147.    
  148.     public function logoutAction()
  149.     {
  150.        
  151.         $this->session->destroy();
  152.         $this->flash->success('Вы успешно вышли');
  153.         echo "<p><a href="."/account/index".">Главная</a></p>";
  154.        
  155.         $this->view->disable();
  156.     }
  157.        
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement