-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.php
91 lines (90 loc) · 2.29 KB
/
connect.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
<?php
session_start();
if (!$_REQUEST["email"]||!$_REQUEST["password"]) {
header("Location: failure.php");
}
try{
$connected=connect($_REQUEST["email"],$_REQUEST["password"]);
}catch(Exception $e){
session_unset();
header("Location: error_while_connecting.php");
}
if($connected){
if($_SESSION["uid"]==1){
header("Location: admin.php");
}else{
header("Location: choice.php");
}
}else{
session_unset();
header("Location: no_such_user.php");
}
function connect($email,$password){
$conn=oci_connect('speculapp','SPECULAPP','localhost/XE');
if (!$conn) {
$e = oci_error();
throw new Exception;
}
// Prepare the statement
$stid = oci_parse($conn, 'SELECT EMAIL, PASSWORD, USER_ID FROM USERS');
if (!$stid) {
$e = oci_error($conn);
throw new Exception;
}
// Perform the logic of the query
$r = oci_execute($stid);
if (!$r) {
$e = oci_error($stid);
throw new Exception;
}
// Fetch the results of the query
$connected=false;
while ($row = oci_fetch_array($stid, OCI_NUM)) {
if($row[0]==$email&&$row[1]==$password){
$connected=true;
$_SESSION["logged"]=true;
$_SESSION["email"]=$row[0];
$_SESSION["password"]=$row[1];
$_SESSION["uid"]=$row[2];
$sti = oci_parse($conn, 'SELECT MAX(SESION_ID) FROM SESION ');
if (!$sti) {
$e = oci_error($conn);
throw new Exception;
}
// Perform the logic of the query
$re = oci_execute($sti);
if (!$re) {
$e = oci_error($sti);
throw new Exception;
}
$ro=oci_fetch_array($sti,OCI_NUM);
$_SESSION["sesion"]=$ro[0]+1;
//print($ro[0].' '.$_SESSION["sesion"]);
oci_free_statement($sti);
$sti = oci_parse($conn, 'DECLARE
v_datestart DATE;
BEGIN
select sysdate into v_datestart from dual;
insert into Sesion values(:newsesion, :myid, v_datestart, v_datestart);
END;');
$newSesionId=$_SESSION["sesion"];
$id=$_SESSION["uid"];
oci_bind_by_name($sti,':myid',$id);
oci_bind_by_name($sti,':newsesion',$newSesionId);
if (!$sti) {
$e = oci_error($conn);
throw new Exception;
}
// Perform the logic of the query
$re = oci_execute($sti);
if (!$re) {
$e = oci_error($sti);
throw new Exception;
}
oci_free_statement($sti);
break;
}
}
return $connected;
}
?>