-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.php
More file actions
107 lines (92 loc) · 4.39 KB
/
Copy pathsignup.php
File metadata and controls
107 lines (92 loc) · 4.39 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
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
<?php
require_once 'auth.php';
//Se l'utente ha già effettuato l'accesso viene reindirizzato alla home
if(checkAuth()) {
header('Location: home.php');
exit;
}
//Se sono stati inseriti i dati di registrazione vengono verificati e inviati al server
if(isset($_POST['username']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['confirm_pwd'])) {
$error = array();
$conn = mysqli_connect($dbconfig['host'], $dbconfig['user'], $dbconfig['password'], $dbconfig['name']) or die(mysqli_error($conn));
//Controllo che l'username rispetti i criteri o che non sia già utilizzato
if(!preg_match('/^[a-zA-Z0-9_]{1,15}$/', $_POST['username'])) {
$error[] = "Username non valido";
} else {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$query = "SELECT username FROM users WHERE username = '$username'";
$res = mysqli_query($conn, $query);
if (mysqli_num_rows($res) > 0) {
$error[] = "Username già utilizzato";
}
}
//Controllo che la password abbia almeno 8 caratteri
if (strlen($_POST['password']) < 8) {
$error[] = "Caratteri password insufficienti";
}
//Controllo il campo 'conferma password'
if (strcmp($_POST['password'], $_POST['confirm_pwd']) != 0) {
$error[] = "Le password non coincidono";
}
//Controllo che l'email sia valida e non utilizzata
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error[] = "Email non valida";
} else {
$email = mysqli_real_escape_string($conn, strtolower($_POST['email']));
$res = mysqli_query($conn, "SELECT email FROM users WHERE email = '$email'");
if (mysqli_num_rows($res) > 0) {
$error[] = "Email già utilizzata";
}
}
//Se validi carico i dati sul database
if (count($error) == 0) {
$password = mysqli_real_escape_string($conn, $_POST['password']);
$password = password_hash($password, PASSWORD_BCRYPT);
$query = "INSERT INTO users(username, email, password) VALUES('$username', '$email', '$password')";
if (mysqli_query($conn, $query)) {
$_SESSION['user'] = $_POST['username'];
$_SESSION['user_id'] = mysqli_insert_id($conn);
mysqli_close($conn);
header("Location: login.php");
exit;
} else {
$error[] = "Errore di connessione al Database";
}
}
mysqli_close($conn);
} else if(empty($_POST['username']) || empty($_POST['email']) || empty($_POST['password']) || empty($_POST['confirm_pwd'])) {
$error = array("Riempi tutti i campi");
}
?>
<html>
<head>
<link rel="stylesheet" href="signup.css">
<script src="signup.js" defer></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title>Registrati</title>
</head>
<body>
<h1>Inserisci i tuoi dati</h1>
<form method='post' autocomplete='off'>
<div class='username'><label for='username'>Nome utente</label>
<input type='text' name='username' <?php if(isset($_POST['username'])) {echo "value=".$_POST['username'];} ?>>
</div>
<span id='username_span'></span>
<div class='email'><label for='email'>E-mail</label>
<input type='text' name='email' <?php if(isset($_POST['email'])) {echo "value=".$_POST['email'];} ?>>
</div>
<span id='email_span'></span>
<div class='password'><label for='password'>Password</label>
<input type='password' name='password'>
</div>
<span id='password_span'></span>
<div class='confirm_pwd'><label for='confirm_pwd'>Conferma Password</label>
<input type='password' name='confirm_pwd'>
</div>
<span id='confirm_span'></span>
<input type='submit' value="Registrati" id="submit" disabled>
</form>
<div class="signup">Hai già un account? <a href="login.php">Accedi</a></div>
</body>
</html>