-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.php
More file actions
80 lines (61 loc) · 1.7 KB
/
common.php
File metadata and controls
80 lines (61 loc) · 1.7 KB
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
<?php
session_start();
function registerUser($user,$pass1,$pass2){
$errorText = '';
// Check passwords
if ($pass1 != $pass2) $errorText = "Passwords are not identical!";
elseif (strlen($pass1) < 6) $errorText = "Password is to short!";
// Check user existance
$pfile = fopen("userpwd.txt","a+");
rewind($pfile);
while (!feof($pfile)) {
$line = fgets($pfile);
$tmp = explode(':', $line);
if ($tmp[0] == $user) {
$errorText = "The selected user name is taken!";
break;
}
}
// If everything is OK -> store user data
if ($errorText == ''){
// Secure password string
$userpass = md5($pass1);
fwrite($pfile, "\r\n$user:$userpass");
}
fclose($pfile);
return $errorText;
}
function loginUser($user,$pass){
$errorText = '';
$validUser = false;
// Check user existance
$pfile = fopen("userpwd.txt","r");
rewind($pfile);
while (!feof($pfile)) {
$line = fgets($pfile);
$tmp = explode(':', $line);
if ($tmp[0] == $user) {
// User exists, check password
if (trim($tmp[1]) == trim(md5($pass))){
$validUser= true;
$_SESSION['userName'] = $user;
}
break;
}
}
fclose($pfile);
if ($validUser != true) $errorText = "Invalid username or password!";
if ($validUser == true) $_SESSION['validUser'] = true;
else $_SESSION['validUser'] = false;
return $errorText;
}
function logoutUser(){
unset($_SESSION['validUser']);
unset($_SESSION['userName']);
}
function checkUser(){
if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
header('Location: login.php');
}
}
?>