-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.php
136 lines (132 loc) · 5.36 KB
/
js.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
<?php
header('Access-Control-Allow-Origin: *'); // IMPORTANTE CORS “Access-Control-Allow-Origin” mancante
header("Access-Control-Expose-Headers: Content-Length, X-JSON");
header("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token, Authorization, Accept, Accept-Language, X-Authorization");
header('Access-Control-Max-Age: 86400');
header('Content-Type: application/json; charset=UTF-8');
// print_r($_POST);
/*
# funziona perfettamente receive formData
# from javascript senza gli headers da parte di javascript
$POST = $_POST;
$json = json_encode($POST); // {"id":"1","usr":"utente1","psw":"12345_&","age":"25"}
$obj = json_decode($json); // stdClass Object
# (
# [id] => 1
# [usr] => utente1
# [psw] => 12345_&
# [age] => 25
# )
print_r($obj->usr);
*/
# funziona benissimo, queste righe di codice risolvono il problema del browser CORS
// header('Access-Control-Allow-Origin: *');
// header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
// header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
// header('Content-Type: application/json; charset=UTF-8');
try {
if($_SERVER['REQUEST_METHOD'] === 'POST') {
// throw new Exception("nuovo messaggio METHOD POST", 154);
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
// function copy
$file = $_FILES['image'];
// $temp = $file['tmp_name'];
$uid = uniqid();
$file_name = $uid."-".$_FILES['image']['name'];
$path_folder = "../avatar/".$file_name;
copy($_FILES['image']['tmp_name'], $path_folder);
chmod($path_folder, 777);
// funziona benissimo.
/*
$file = $_FILES['image'];
$temp = $file['tmp_name'];
$target_file = '../avatar//targetfilename.jpg';
move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
*/
echo '[{
"id": 101,
"email" : "[email protected]",
"usr": "Mario Rossi",
"psw": "'.sha1("oxnde7n39").'",
"age": 22,
"method" : "POST",
"avatar" : "default.png"
},
{
"id": 102,
"email" : "[email protected]",
"usr": "Mario Bianchi",
"psw": "'.sha1("123abc").'",
"age": 25,
"method" : "POST",
"avatar" : "default.png"
},
{
"id" : "'.$POST['id'].'",
"email" : "'.$POST['email'].'",
"usr": "'.$POST['usr'].'",
"psw": "'.sha1($POST['psw']).'",
"age": "'.$POST['age'].'",
"method" : "POST",
"avatar" : "'.$file_name.'"
},
{
"id" : 103,
"email" : "[email protected]",
"usr" : "batman cavaliere oscuro",
"psw" : "'.sha1("batmobile").'",
"age" : "101",
"method" : "POST",
"avatar" : "default.png"
},
{
"id" : 104,
"email" : "[email protected]",
"usr" : "spiderman",
"psw" : "'.sha1("uomoragno").'",
"age" : "98",
"method" : "POST",
"avatar" : "default.png"
}
]';
} else {
if($_SERVER['REQUEST_METHOD'] === 'GET') {
// throw new Exception("nuovo messaggio METHOD GET", 154);
$GET = filter_var_array($_GET, FILTER_SANITIZE_STRING);
echo '[
{
"id" : "'. $GET['id'] .'",
"email" : "'. $GET['email'] .'",
"usr" : "'. $GET['usr'] .'",
"psw" : "'.$GET['psw'].'",
"age" : "'.$GET['age'].'",
"method" : "GET",
"avatar" : "default.png"
}
]';
}
}
} catch (Exception $error) {
/*
echo '[
{
"messaggio" : "'. $error->getMessage() .'",
"codice" : "'. $error->getCode() .'",
"line" : "'. $error->getLine() .'",
"file" : "'. $error->getFile() .'"
}
]';
*/
echo '[
{
"id" : "'.$error->getMessage().'",
"email" : "codice ' .$error->getCode().'",
"usr" : "line '.$error->getLine().'",
"psw" : "file '.$error->getFile().'",
"age" : "nil",
"method" : "ERROR"
}
]';
}
?>