PlainSQLiteBlog/pages/user/signin.php

73 lines
1.8 KiB
PHP

<?php
global $c;
try
{
$intUserCount = $c->query("SELECT count(*) as c from users")[0]["c"];
if ($intUserCount < 1)
Respond::redirect("/user/register");
if (Request::posts("username", "password"))
{
$strUsername = Request::getPosted("username");
$strPassword = Request::getPosted("password");
$strHash = sha1($strPassword);
$varUsers = $c->query(
"SELECT *
from users
where
username like ?
and hash = ?",
$strUsername,
$strHash);
if (count($varUsers) !== 1)
throw new Exception("Zero or more than one user returned for credentials provided");
$strToken = sha1(microtime());
$c->query(
"INSERT into sessions (username, token) values (?, ?)",
$strUsername,
$strToken);
Cookie::set("token", $strToken);
PageRender::message(
"Successfully signed in",
"info");
Respond::redirect("/user");
}
}
catch (Exception $x)
{
PageRender::message($x->getMessage(), "danger");
}
?>
<?php PageRender::message(); ?>
<form method="post">
<table>
<tbody>
<tr>
<td><label>Username</label></td>
<td><input type="text" name="username" value="" /></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input type="password" name="password" value="" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Go" /></td>
</tr>
</tbody>
</table>
</form>