forked from r-nikhil/petition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
83 lines (56 loc) · 1.77 KB
/
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
require '/./libs/Slim/Slim.php';
require '/./libs/rb.php';
\Slim\Slim::registerAutoloader();
R::setup('mysql:host=localhost;dbname=petition','root','');
//R::freeze(true);
$app = new \Slim\Slim();
$app->contentType('application/json');
$app->post('/', function() use ($app) {
$username = $app->request->post('username');
$password = $app->request->post('password');
$article = R::findOne('user', 'username=?', array((string)$username));
if ($article) { // if found, return JSON response
$pass_db = (string)$article->password;
$pass_request = (string)$password;
if($pass_db === $pass_request)
{
session_start(); // Starting Session
$_SESSION['id'] = $article->id;
$_SESSION['username'] = $article->username;
$app->redirect('posts.html');
}
else
{
echo "wrong password";
}
}
else {
echo "go register first";
}
});
$app->post('/register', function() use ($app) {
$username = $app->request->post('username');
$password = $app->request->post('password');
$article = R::dispense('user');
$article->password = (string)$password;
$article->username = (string)$username;
$id = R::store($article);
echo "registerd. Now go login";
// $app->redirect('/add');
});
$app->get('/add', function() use ($app){$app->redirect('posts.html');});
$app->post('/add', function() use ($app) {
session_start(); // Starting Session
if(isset($_SESSION['username'])){
$title = $app->request->post('title');
$details = $app->request->post('details');
$article = R::dispense('petition');
$article->password = (string)$title;
$article->username = (string)$details;
$id = R::store($article);
echo "petition added";
}else{echo "login first ";}
});
$app->run();
?>