Skip to content

Commit 652c77f

Browse files
committed
Redirect to previous uri after login
1 parent 6cb1cc8 commit 652c77f

File tree

3 files changed

+66
-43
lines changed

3 files changed

+66
-43
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
.vagrant/
1+
.vagrant/
2+
.idea
3+
4+
key.pem

html/befuncs/snips.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ function genNavBar(){
2828
<nav id="main_nav">
2929
<div>
3030
<?php
31+
$uri = array_key_exists('redirect_to',$_GET) ?
32+
$_GET['redirect_to'] :
33+
$_SERVER['REQUEST_URI'];
34+
$params = http_build_query(array(
35+
'redirect_to' => $uri,
36+
));
3137
//TODO: replace login btn with account btn when logged in
3238
if($_SESSION['userid']==0){
33-
echo '<a href="/login/" class="btn" id="loginbtn">Login</a>';
39+
echo '<a href="/login/?' . $params . '" class="btn" id="loginbtn">Login</a>';
3440
}else{
3541
echo '<a href="/login/" class="btn" id="loginbtn">Logout</a>';
3642

html/login/index.php

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,66 @@
11
<?php
2-
require_once('../befuncs/snips.php');
3-
require_once('../befuncs/db_user.php');
4-
$db=new accountdb();
5-
6-
$username_cache = '';
7-
$passwd_cache = '';
8-
$login_result = null;
2+
require_once('../befuncs/snips.php');
3+
require_once('../befuncs/db_user.php');
4+
$db = new accountdb();
95

10-
if( array_key_exists('user',$_POST)
11-
&& array_key_exists('passwd',$_POST) ){
12-
$login_result = $db->login($_POST['user'],$_POST['passwd']);
13-
if(!$login_result){
14-
$username_cache = $_POST['user'];
15-
$passwd_cache = $_POST['passwd'];
16-
}
17-
}else if(array_key_exists('logout',$_POST)){
18-
$db->logout();
19-
}
20-
21-
genUsual('Riedler\'s Login Site',['/style/login.css'],'');
6+
$username_cache = '';
7+
$passwd_cache = '';
8+
$login_result = null;
9+
10+
if (
11+
array_key_exists('user', $_POST)
12+
&& array_key_exists('passwd', $_POST)
13+
) {
14+
$login_result = $db->login($_POST['user'], $_POST['passwd']);
15+
if ($login_result) {
16+
if (array_key_exists('redirect_to', $_POST)) {
17+
$url = parse_url($_POST['redirect_to']);
18+
if (is_array($url)) {
19+
$uri = $url['path'] . '?' . $url['query'] . '#' . $url['fragment'];
20+
header('Location: ' . $uri, true, 303);
21+
die();
22+
}
23+
}
24+
header('Location: /login/');
25+
die();
26+
} else {
27+
$username_cache = $_POST['user'];
28+
$passwd_cache = $_POST['passwd'];
29+
}
30+
} else if (array_key_exists('logout', $_POST)) {
31+
$db->logout();
32+
}
33+
34+
genUsual('Riedler\'s Login Site', ['/style/login.css'], '');
2235
?>
36+
2337
<body>
2438
<?php
25-
genNavBar();
39+
genNavBar();
2640
?>
2741
<form id="loginform" method="POST" action="/login/">
28-
<?php if($_SESSION['userid']){
42+
<?php if ($_SESSION['userid']) {
2943
$user = $db->get_user_by_id($_SESSION['userid']);
3044
?>
31-
32-
<h2>LOGOUT</h2>
33-
<span>Logged in as <?= $user['type']?> "<?= $user['name'] ?>"</span>
34-
<input type="submit" value="Logout" class="btn" name="logout" id="i_submit"/>
35-
36-
<?php }else{ ?>
37-
38-
<h2>LOGIN</h2>
39-
<label for="i_user">Username:</label>
40-
<input type="text" name="user" id="i_user" class="input__text" value="<?= $username_cache ?>" required/>
41-
<label for="i_passwd">Password:</label>
42-
<input type="password" name="passwd" id="i_passwd" class="input__text" value="<?= $passwd_cache ?>" required/>
43-
<input type="submit" value="Login" class="btn" id="i_submit"/>
44-
<?php
45-
if($login_result===false){
45+
46+
<h2>LOGOUT</h2>
47+
<span>Logged in as <?= $user['type'] ?> "<?= $user['name'] ?>"</span>
48+
<input type="submit" value="Logout" class="btn" name="logout" id="i_submit" />
49+
50+
<?php } else { ?>
51+
52+
<h2>LOGIN</h2>
53+
<label for="i_user">Username:</label>
54+
<input type="text" name="user" id="i_user" class="input__text" value="<?= $username_cache ?>" required />
55+
<label for="i_passwd">Password:</label>
56+
<input type="password" name="passwd" id="i_passwd" class="input__text" value="<?= $passwd_cache ?>" required />
57+
<input type="hidden" name="redirect_to" value="<?= array_key_exists('redirect_to', $_GET) ? $_GET['redirect_to'] : '' ?>" <input type="hidden" name="signature" value="<?= array_key_exists('signature', $_GET) ? $_GET['signature'] : '' ?>" <input type="submit" value="Login" class="btn" id="i_submit" />
58+
<?php
59+
if ($login_result === false) {
4660
echo '<span>incorrect password or username</span>';
4761
}
48-
?>
49-
<?php } ?>
50-
</form>
51-
<?php genFooter(); ?>
52-
</body>
62+
?>
63+
<?php } ?>
64+
</form>
65+
<?php genFooter(); ?>
66+
</body>

0 commit comments

Comments
 (0)