/
login.php
152 lines (128 loc) · 4.08 KB
/
login.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/*
Loginpage. Part of Runosydan.net.
Copyright (C) 2009 Aleksi Räsänen <aleksi.rasanen@runosydan.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// *********************************************
// check_post_values
//
// @brief Check if there is any POST-values. If there is,
// then user has tried to login.
//
// @param $db Database class instance
//
// @param $data POST-data
//
// *********************************************
function check_post_values( $db, $data )
{
// Is there username and password given in POST-data?
if( isset( $data['username'] )
&& isset( $data['password'] ) )
{
// No SQL-injections!
$username = mysql_real_escape_string( $data['username'] );
$password = mysql_real_escape_string( $data['password'] );
// Get user information by username
$q = 'SELECT id, username, password FROM rs_users WHERE '
. 'username="' . $username . '"';
try
{
$ret = $db->query( $q );
}
catch( Exception $e )
{
echo 'Virhe tietokantakyselyssä!';
die();
}
// If we found in database user, then check password
if( $db->numRows( $ret ) > 0 )
{
$row = $db->fetchAssoc( $ret );
// Is password correct? If it is, then set
// session variables.
if( $row[0]['password'] == sha1( $password ) )
{
$_SESSION['id'] = $row[0]['id'];
$_SESSION['username'] = $row[0]['username'];
// Icon to show
$_SESSION['message_icon'] =
'graphics/32px-Crystal_Clear_app_clean.png';
$_SESSION['message'] = 'Olet kirjautunut sisään '
. 'käyttäjätunnuksella "' . $username . '"';
$cUsers = new CUsers( $db, $_SESSION );
$_SESSION['unseen_comments'] = $cUsers->countUnseenComments( $_SESSION['id'] );
}
else
{
// Icon to show
$_SESSION['message_icon'] =
'graphics/32px-Crystal_Clear_app_logout.png';
// Someting went wrong! Show error.
$_SESSION['message'] = 'Virheellinen salasana!';
}
}
else
{
// Icon to show
$_SESSION['message_icon'] =
'graphics/32px-Crystal_Clear_app_logout.png';
// Someting went wrong! Show error.
$_SESSION['message'] = 'Käyttäjätunnusta ei löytynyt!';
}
}
}
// *********************************************
// create_login_form
//
// @brief Create login form
//
// *********************************************
function create_login_form()
{
echo '<div class="textblock">';
echo '<h3>Kirjaudu</h3>';
echo '</div>';
echo '<div class="inner_textblock">';
echo '<form action="login.php" method="post">';
echo '<table>';
echo '<tr><td>Käyttäjätunnus</td>';
echo '<td><input type="text" name="username"></td></tr>';
echo '<tr><td>Salasana</td>';
echo '<td><input type="password" name="password"></td></tr>';
echo '<tr><td colspan="2"><input type="submit" '
. 'value="Kirjaudu"></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
echo '<a href="register.php">Luo uusi käyttäjätunnus</a><br><br>';
echo '</div>';
}
session_start();
require 'general_functions.php';
// Check POST-values if there is any.
check_post_values( $db, $_POST );
// Create html start tags and top menu
create_site_top();
create_top_menu();
echo '<div class="login">';
// Show possible messages (eg. if login is succesfully made or
// if something has failed in login or something like that).
show_message();
// If we are not logged in, show login form.
if(! isset( $_SESSION['username'] ) )
create_login_form();
echo '</div>';
// Create HTML end tags.
create_site_bottom();
?>