1<?php
2//Something to write to txt log
3$log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
4 "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
5 "User: ".$username.PHP_EOL.
6 "-------------------------".PHP_EOL;
7
8//Save string to log, use FILE_APPEND to append.
9file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
1<?php
2session_start();// come sempre prima cosa, aprire la sessione
3include("db_con.php"); // Include il file di connessione al database
4$_SESSION["username"]=$_POST["username"]; // con questo associo il parametro username che mi è stato passato dal form alla variabile SESSION username
5$_SESSION["password"]=$_POST["password"]; // con questo associo il parametro username che mi è stato passato dal form alla variabile SESSION password
6$query = mysql_query("SELECT * FROM users WHERE username='".$_POST["username"]."' AND password ='".$_POST["password"]."'") //per selezionare nel db l'utente e pw che abbiamo appena scritto nel log
7or DIE('query non riuscita'.mysql_error());
8// Con il SELECT qua sopra selezione dalla tabella users l utente registrato (se lo è) con i parametri che mi ha passato il form di login, quindi
9// Quelli dentro la variabile POST. username e password.
10if(mysql_num_rows($query)>0){ //se c'è una persona con quel nome nel db allora loggati
11$row = mysql_fetch_assoc($query); // metto i risultati dentro una variabile di nome $row
12$_SESSION["logged"] =true; // Nella variabile SESSION associo TRUE al valore logge
13header("location:prova.php"); // e mando per esempio ad una pagina esempio.php// in questo caso rimanderò ad una pagina prova.php
14}else{
15echo "non ti sei registrato con successo"; // altrimenti esce scritta a video questa stringa di errore
16}
17?>
1<?php
2session_start();
3
4// initializing variables
5$username = "";
6$email = "";
7$errors = array();
8
9// connect to the database
10$db = mysqli_connect('localhost', 'root', '', 'registration');
11
12// REGISTER USER
13if (isset($_POST['reg_user'])) {
14 // receive all input values from the form
15 $username = mysqli_real_escape_string($db, $_POST['username']);
16 $email = mysqli_real_escape_string($db, $_POST['email']);
17 $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
18 $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
19
20 // form validation: ensure that the form is correctly filled ...
21 // by adding (array_push()) corresponding error unto $errors array
22 if (empty($username)) { array_push($errors, "Username is required"); }
23 if (empty($email)) { array_push($errors, "Email is required"); }
24 if (empty($password_1)) { array_push($errors, "Password is required"); }
25 if ($password_1 != $password_2) {
26 array_push($errors, "The two passwords do not match");
27 }
28
29 // first check the database to make sure
30 // a user does not already exist with the same username and/or email
31 $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
32 $result = mysqli_query($db, $user_check_query);
33 $user = mysqli_fetch_assoc($result);
34
35 if ($user) { // if user exists
36 if ($user['username'] === $username) {
37 array_push($errors, "Username already exists");
38 }
39
40 if ($user['email'] === $email) {
41 array_push($errors, "email already exists");
42 }
43 }
44
45 // Finally, register user if there are no errors in the form
46 if (count($errors) == 0) {
47 $password = md5($password_1);//encrypt the password before saving in the database
48
49 $query = "INSERT INTO users (username, email, password)
50 VALUES('$username', '$email', '$password')";
51 mysqli_query($db, $query);
52 $_SESSION['username'] = $username;
53 $_SESSION['success'] = "You are now logged in";
54 header('location: index.php');
55 }
56}
57
58// ...
59
1<!DOCTYPE html>
2<html>
3<head>
4 <title>Login</title>
5 <script>
6 firebase.initializeApp(firebaseConfig);
7 const auth = firebase.auth();
8 function signUp(){
9 var email = document.getElementById("email");
10 var password = document.getElementById("password");
11 const promise = auth.createUserWithEmailAndPassword(email.value, password.value);
12 promise.catch(e => alert(e.message));
13 alert("Signed Up");
14 }
15 function signIn(){
16 var email = document.getElementById("email");
17 var password = document.getElementById("password");
18 const promise = auth.signInWithEmailAndPassword(email.value, password.value);
19 promise.catch(e => alert(e.message));
20 }
21 function signOut(){
22 auth.signOut();
23 alert("Signed Out");
24 }
25
26auth.onAuthStateChanged(function(user){
27 if(user){
28 var email = user.email;
29 alert("Signed in as " + email);
30 //Take user to a different or home page
31
32 //is signed in
33 }else{
34 alert("No Active User");
35 //no user is signed in
36 }
37 });g
38 </script>
39<style type="text/css">
40 body{
41 background-color: #55d6aa;
42}
43h1{
44 background-color: #ff4d4d;
45 margin: 10px auto;
46 text-align: center;
47 color: white;
48}
49#formContainer{
50 background-color: white;
51 box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
52
53 width: 25%;
54 height: 45;
55 margin: 10px auto;
56}
57#header{
58 width: 100%;
59 height: 10px;
60 background: black;
61}
62#email{
63 width: 70%;
64 height: 40px;
65 display:block;
66 margin: 25px auto;
67 border: none;
68 outline: none;
69 border-bottom: 2px solid black;
70}
71#password{
72 width: 70%;
73 height: 40px;
74 display: block;
75 margin: 10px auto;
76 border: none;
77 outline: none;
78 border-bottom: 2px solid black;
79}
80#signUp{
81 background-color: #ff4d4d;
82 color: white;
83 border: none;
84 font-weight: bold;
85 padding: 15px 32px;
86 border-radius: 10px;
87 text-align: center;
88 text-decoration: none;
89 display: inline-block;
90 font-size: 13px;
91 margin-top: 20px;
92 margin-left: 50px;
93}
94#signIn{
95 background-color: #32ff7e;
96 color: white;
97 font-weight: bold;
98 border: none;
99 padding: 15px 35px;
100 border-radius: 10px;
101 text-align: center;
102 text-decoration: none;
103 font-size: 13px
104}
105#signOut{
106 background-color: #FFA500;
107 color: white;
108 border: none;
109 padding: 12px 32px;
110 border-radius: 10px;
111 text-align: center;
112 text-decoration: none;
113 display: inline-block;
114 font-size: 13px;
115 margin-top: 9px;
116 margin-left: 74px;
117 font-weight: bold;
118}
119button: hover{
120box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 7px 50px 0 rgba(0,0,0,0,.19);
121}
122</style>
123</head>
124<body>
125 <h1>Login Here</h1>
126 <div id="formContainer">
127 <div id="header"> </div>
128 <input type="email" placeholder="Email" id="email">
129 <input type="password" placeholder="Password" id="password">
130
131 <button onclick="signUp()" id="signUp"> Sign Up </button>
132 <button onclick="signIn()" id="signIn"> Sign In </button>
133 <button onclick="signOut()" id="signOut"> Sign Out </button>
134Continue</a>
135</body>
136</html>
1<html>
2<head>
3<title>PHP User Registration Form</title>
4<link href="./css/style.css" rel="stylesheet" type="text/css" />
5</head>
6<body>
7 <form name="frmRegistration" method="post" action="">
8 <div class="demo-table">
9 <div class="form-head">Sign Up</div>
10
11<?php
12if (! empty($errorMessage) && is_array($errorMessage)) {
13 ?>
14 <div class="error-message">
15 <?php
16 foreach($errorMessage as $message) {
17 echo $message . "<br/>";
18 }
19 ?>
20 </div>
21<?php
22}
23?>
24 <div class="field-column">
25 <label>Username</label>
26 <div>
27 <input type="text" class="demo-input-box"
28 name="userName"
29 value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>">
30 </div>
31 </div>
32
33 <div class="field-column">
34 <label>Password</label>
35 <div><input type="password" class="demo-input-box"
36 name="password" value=""></div>
37 </div>
38 <div class="field-column">
39 <label>Confirm Password</label>
40 <div>
41 <input type="password" class="demo-input-box"
42 name="confirm_password" value="">
43 </div>
44 </div>
45 <div class="field-column">
46 <label>Display Name</label>
47 <div>
48 <input type="text" class="demo-input-box"
49 name="firstName"
50 value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>">
51 </div>
52
53 </div>
54 <div class="field-column">
55 <label>Email</label>
56 <div>
57 <input type="text" class="demo-input-box"
58 name="userEmail"
59 value="<?php if(isset($_POST['userEmail'])) echo $_POST['userEmail']; ?>">
60 </div>
61 </div>
62 <div class="field-column">
63 <div class="terms">
64 <input type="checkbox" name="terms"> I accept terms
65 and conditions
66 </div>
67 <div>
68 <input type="submit"
69 name="register-user" value="Register"
70 class="btnRegister">
71 </div>
72 </div>
73 </div>
74 </form>
75</body>
76</html>
1<?php
2session_start();
3if(!isset($_POST['pass'])){
4 header("Location: index.html");
5 exit();
6}
7
8$login = $_POST['login'];
9$pass = $_POST['pass'];
10$login = htmlentities($login, ENT_HTML5, "UTF-8");
11$pass = htmlentities($pass, ENT_HTML5, "UTF-8");
12require_once "../../includes/connect.php";
13try{
14 $db = new mysqli($host, $db_user,$db_pass, $db_name);
15 if(!$db->connect_errno == 0){
16 throw new Exception("connection error");
17 }else{
18 $query = "SELECT * FROM users WHERE user = ?";
19 if(!$exec = $db->prepare($query)){
20 throw new mysqli_sql_exception("Query prepare error");
21 }else{
22 $exec->bind_param("s", $login);
23 $exec->execute();
24 $res = $exec->get_result();
25 $assoc = $res->fetch_assoc();
26 if($res->num_rows != 0){
27 if(!password_verify($pass,$assoc['pass'])){
28 $_SESSION['error'] = "incorrect login or pass";
29 header("Location: ../../index.html");
30 }else{
31 $_SESSION['name'] = $assoc['name'];
32 $_SESSION['surname'] = $assoc['surname'];
33 $_SESSION['desription'] = $assoc['opis'];
34 $_SESSION['role'] = $assoc['role'];
35 if($assoc['isAdmin']){
36 $_SESSION['admin'] = true;
37 header("Location: ../../AdminPanel.php");
38 }else{
39 $_SESSION['loged'] = true;
40 header("Location: ../../User.php");
41 }
42 }
43 }else{
44 $_SESSION['error'] = "Invalid login or Pass";
45 header("Location: ../../index.html");
46 }
47 }
48 }
49}catch(Exception $e){
50 echo $e;
51}catch(mysqli_sql_exception $e){
52 echo $e;
53}