This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
login.php
138 lines (129 loc) · 5.58 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
<?php
/*
* Codejudge
* Copyright 2012, Sankha Narayan Guria (sankha93@gmail.com)
* Licensed under MIT License.
*
* Codejudge Login page
*/
require_once('functions.php');
if(loggedin())
header("Location: index.php");
else if(isset($_POST['action'])) {
$username = array_key_exists('username', $_POST) ? mysql_real_escape_string(trim($_POST['username'])) : "";
if($_POST['action']=='login') {
if(trim($username) == "" or trim($_POST['password']) == "") {
header("Location: login.php?derror=1"); // empty entry
} else {
// code to login the user and start a session
connectdb();
$query = "SELECT salt,hash FROM users WHERE username='".$username."'";
$result = mysql_query($query);
$fields = mysql_fetch_array($result);
$currhash = crypt($_POST['password'], $fields['salt']);
if($currhash == $fields['hash']) {
$_SESSION['username'] = $username;
header("Location: index.php");
} else
header("Location: login.php?error=1");
}
} else if($_POST['action']=='register') {
// register the user
$email = array_key_exists('email', $_POST) ? mysql_real_escape_string(trim($_POST['email'])) : "";
if(trim($username) == "" and trim($_POST['password']) == "" and trim($email) == "") {
header("Location: login.php?derror=1"); // empty entry
} else {
// create the entry in the users table
connectdb();
$query = "SELECT salt,hash FROM users WHERE username='".$username."'";
$result = mysql_query($query);
if(mysql_num_rows($result)!=0) {
header("Location: login.php?exists=1");
} else {
$salt = randomAlphaNum(5);
$hash = crypt($_POST['password'], $salt);
$sql="INSERT INTO `users` ( `username` , `salt` , `hash` , `email`, `status` ) VALUES ('".$username."', '$salt', '$hash', '".$email."', '1')";
mysql_query($sql);
header("Location: login.php?registered=1");
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title><?php echo(getName()); ?> Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="css/bootstrap.css" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
.footer {
text-align: center;
font-size: 11px;
}
</style>
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="http://twitter.github.com/bootstrap/assets/ico/favicon.ico">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="http://twitter.github.com/bootstrap/assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="http://twitter.github.com/bootstrap/assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="http://twitter.github.com/bootstrap/assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="http://twitter.github.com/bootstrap/assets/ico/apple-touch-icon-57-precomposed.png">
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#"><?php echo(getName()); ?></a>
</div>
</div>
</div>
<div class="container">
<?php
if(isset($_GET['logout']))
echo("<div class=\"alert alert-info\">\nYou have logged out successfully!\n</div>");
else if(isset($_GET['error']))
echo("<div class=\"alert alert-error\">\nIncorrect username or password!\n</div>");
else if(isset($_GET['registered']))
echo("<div class=\"alert alert-success\">\nYou have been registered successfully! Login to continue.\n</div>");
else if(isset($_GET['exists']))
echo("<div class=\"alert alert-error\">\nUser already exists! Please select a different username.\n</div>");
else if(isset($_GET['derror']))
echo("<div class=\"alert alert-error\">\nPlease enter all the details asked before you can continue!\n</div>");
?>
<h1><small>Login</small></h1>
<p>Please login to continue.</p><br/>
<form method="post" action="login.php">
<input type="hidden" name="action" value="login"/>
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/><br/>
<input class="btn" type="submit" name="submit" value="Login"/>
</form>
<hr/>
<form method="post" action="login.php">
<input type="hidden" name="action" value="register"/>
<h1><small>New User? Register now</small></h1>
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
Email: <input type="email" name="email"/><br/><br/>
<input class="btn btn-primary" type="submit" name="submit" value="Register"/>
</div> <!-- /container -->
<?php
include('footer.php');
?>