<?php /* login.php */
$conn = new mysqli('localhost', 'username', 'password'); // Database connection
if (!$conn) { die('Connection failed: ' . mysqli_connect_error()); }
if (isset($_POST['submit'])) { // Check form submit
$user = $_POST['user'];
$pass = $_POST['pass'];
$result = $conn->query(
"SELECT * FROM user WHERE username='{$user}' AND password='{$pass}'"
);
header('Location:' . (mysqli_num_rows($result) > 0 ? 'ok.php' : 'fail.php'));
exit;
}
?>
<html>
<head><title>My Website</title></head>
<body>
<form>
Username: <input type="text" name="user"><br />
Password: <input type="password" name="pass"><br />
<input type="submit" />
</form>
</body>
</html>
<?php /* login.php */
include 'connection.php';
include 'functions.php';
if (isset($_POST['submit'])) { // Check form submit
$user = $_POST['user'];
$pass = $_POST['pass'];
header('Location: ' . (isLoginValid($user, $pass) ? 'ok.php' : 'fail.php'));
exit;
}
?>
<html>
<?php include 'head.php'; ?>
<body>
<form>
Username: <input type="text" name="user"><br />
Password: <input type="password" name="pass"><br />
<input type="submit" />
</form>
</body>
</html>
/* module/App/src/App/UserMapper.php */
class UserMapper extends \Zend\Db\TableGateway\AbstractTableGateway { }
/* module/App/src/App/User.php */
class User { }
/* module/App/src/App/LoginForm.php */
class LoginForm extends \Zend\Form
{
public function init()
{
$this->add(array(
'name' => 'username',
'type' => 'Zend\Form\Element\Text',
'options' => array('label' => 'Username'),
));
$this->add(array(
'name' => 'password',
'type' => 'Zend\Form\Element\Password',
'options' => array('label' => 'Password'),
));
}
}
Model
/* module/App/view/layout/layout.phtml */
<html>
<head>
<title>My Website</title>
</head>
<body>
<?php echo $this->content; ?>
</body>
</html>
/* module/App/view/app/auth/login.phtml */
<?php
$form->prepare();
echo $form;
?>
View
namespace App\Controller; /* module/App/src/App/Controller/AuthController.php */
class AuthController extends \Zend\Mvc\Controller\AbstractActionController
{
public function loginAction
{
$form = new LoginForm();
$request = $this->getRequest();
if ($this->getRequest()->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$data = $form->getData();
$mapper = new UserMapper();
$user = $mapper->fetchByCredentials($data['user'], $data['pass']);
if ($user) {
return $this->redirect()->toRoute('dashboard');
}
}
$this->flashMessenger()->addErrorMessage('The form has errors');
}
return array('form' => $form);
}
}
Controller
Practice software craftsmanship; Don't be a code monkey