Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of AccountController
- *
- * @author PC
- */
- use Phalcon\Mvc\Controller;
- use Phalcon\Escaper;
- class AccountController extends Controller{
- //put your code here
- public function initialize()
- {
- }
- public function indexAction()
- {
- if(isset($_SESSION['auth']))
- {
- $id = $this->session->get('auth');
- $user = User::findFirst(
- [
- "id = :id:" =>
- [
- "id" => $id['id'],
- ]
- ]);
- $un = $user->name;
- $this->view->un = $un;
- }
- }
- public function registerAction()
- {
- $created_at = date("Y.m.d");
- $update_at = date("Y.m.d");
- if (isset($_POST['submit']))
- {
- $name = $_POST['name'];
- $email = $_POST['email'];
- $password1 = $_POST['password1'];
- $password2 = $_POST['password2'];
- $name = htmlspecialchars($name);
- $email = htmlspecialchars($email);
- if ($password1 == $password2)
- {
- $password = $password1;
- $user = new User();
- $user->name = $name;
- $user->email = $email;
- $user->password = MD5($password);
- $user->created_at = $created_at;
- $user->update_at = $update_at;
- if($user->save())
- {
- echo "Вы успешно зарегистрировались!";
- }
- else
- {
- echo "Что-то пошло не так: ";
- $messages = $user->getMessages();
- foreach ($messages as $message) {
- echo $message->getMessage(), "<br/>";
- }
- }
- }
- $this->view->disable();
- }
- }
- private function _registerSession($user)
- {
- $this->session->set(
- 'auth',
- [
- 'id' => $user->id,
- 'name' => $user->name,
- ]
- );
- }
- public function loginAction()
- {
- if (isset($_POST['submit']))
- {
- $name = $_POST['nameoremail'];
- $password = $_POST['password'];
- $name = htmlspecialchars($name);
- $password = MD5($password);
- $ras = new User;
- $user = $ras::findFirst(
- [
- "(name = :name: OR email = :email:) AND password = :password:",
- 'bind' => [
- 'name' => $name,
- 'email'=> $name,
- 'password' => $password,
- ]
- ]
- );
- if ($user !== false)
- {
- //$this->_registerSession($user);
- $this->session->set(
- 'auth',
- [
- 'id' => $user->id,
- 'name' => $user->name,
- ]
- );
- $this->view->name = $user->name;
- return $this->dispatcher->forward(
- [
- "controller" => "account",
- "action" => "index",
- ]
- );
- }
- else
- {
- $this->view->disable();
- echo 'Нет пользователя с такими данными';
- echo "<p><a href="."/account/login".">Назад</a></p>";
- }
- }
- }
- public function logoutAction()
- {
- $this->session->destroy();
- $this->flash->success('Вы успешно вышли');
- echo "<p><a href="."/account/index".">Главная</a></p>";
- $this->view->disable();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement