-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlcon.php
81 lines (63 loc) · 1.94 KB
/
sqlcon.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
<?php
/**
* Created by PhpStorm.
* User: smoriarty
* Date: 7/4/14
* Time: 3:41 AM
*/
$comments = array();
class database {
public function connect() {
session_start();
$connection = mysql_connect("127.0.0.1","root","");
mysql_select_db('playground');
if (!$connection) {
die("Could not connect ".mysql_error());
}
}
//Comment Functions
public function addComment($comment, $name) {
$query = "INSERT INTO comments(comment, name) VALUES ('$comment', '$name')";
mysql_query($query) or die(mysql_error());
}
public function getComments() {
$comments = array();
$query = "SELECT comment, name FROM comments ORDER BY id DESC LIMIT 10 ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$comments[] = $row;
}
print json_encode($comments);
}
//Auth functions
public function verifyUser($user, $password) {
//Hash password and check
$password = md5($password);
$query = "SELECT id FROM users WHERE username='$user' AND password='$password'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
if(!empty($row)) {
//Good login info
$this->login($user);
} else {
//Bad login info
echo $query;
}
}
public function login($user) {
//Set session variables and redirect
$_SESSION['loggedIn'] = true;
$_SESSION['username'] = $user;
//Set cookie for xss testing
$session = rand(100,999999).$_SESSION['username'].rand(0,99999).time(); //Random session ID
setcookie("id", md5($session),time()+3600);
header('Location: index.php');
}
public function checkLogin() {
//Test for valid login
if($_SESSION['loggedIn'] != true) {
header('Location: login.php');
}
}
}
?>