php login system
<?php
//Something to write to txt log
$log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
"Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
"User: ".$username.PHP_EOL.
"-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
3.8
5
<?php
session_start();
if(!isset($_POST['pass'])){
header("Location: index.html");
exit();
}
$login = $_POST['login'];
$pass = $_POST['pass'];
$login = htmlentities($login, ENT_HTML5, "UTF-8");
$pass = htmlentities($pass, ENT_HTML5, "UTF-8");
require_once "../../includes/connect.php";
try{
$db = new mysqli($host, $db_user,$db_pass, $db_name);
if(!$db->connect_errno == 0){
throw new Exception("connection error");
}else{
$query = "SELECT * FROM users WHERE user = ?";
if(!$exec = $db->prepare($query)){
throw new mysqli_sql_exception("Query prepare error");
}else{
$exec->bind_param("s", $login);
$exec->execute();
$res = $exec->get_result();
$assoc = $res->fetch_assoc();
if($res->num_rows != 0){
if(!password_verify($pass,$assoc['pass'])){
$_SESSION['error'] = "incorrect login or pass";
header("Location: ../../index.html");
}else{
$_SESSION['name'] = $assoc['name'];
$_SESSION['surname'] = $assoc['surname'];
$_SESSION['desription'] = $assoc['opis'];
$_SESSION['role'] = $assoc['role'];
if($assoc['isAdmin']){
$_SESSION['admin'] = true;
header("Location: ../../AdminPanel.php");
}else{
$_SESSION['loged'] = true;
header("Location: ../../User.php");
}
}
}else{
$_SESSION['error'] = "Invalid login or Pass";
header("Location: ../../index.html");
}
}
}
}catch(Exception $e){
echo $e;
}catch(mysqli_sql_exception $e){
echo $e;
}
Thank you!
5
0
3.5
2
<?php
session_start();// come sempre prima cosa, aprire la sessione
include("db_con.php"); // Include il file di connessione al database
$_SESSION["username"]=$_POST["username"]; // con questo associo il parametro username che mi è stato passato dal form alla variabile SESSION username
$_SESSION["password"]=$_POST["password"]; // con questo associo il parametro username che mi è stato passato dal form alla variabile SESSION password
$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
or DIE('query non riuscita'.mysql_error());
// 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
// Quelli dentro la variabile POST. username e password.
if(mysql_num_rows($query)>0){ //se c'è una persona con quel nome nel db allora loggati
$row = mysql_fetch_assoc($query); // metto i risultati dentro una variabile di nome $row
$_SESSION["logged"] =true; // Nella variabile SESSION associo TRUE al valore logge
header("location:prova.php"); // e mando per esempio ad una pagina esempio.php// in questo caso rimanderò ad una pagina prova.php
}else{
echo "non ti sei registrato con successo"; // altrimenti esce scritta a video questa stringa di errore
}
?>
Thank you!
2
0
3.57
7
<?php
session_start();
// initializing variables
$username = "";
$email = "";
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}
// ...
Thank you!
7
0
0
0
<?php
session_start();
$errorMsg = "";
$validUser = $_SESSION["login"] === true;
if(isset($_POST["sub"])) {
$validUser = $_POST["username"] == "admin" && $_POST["password"] == "password";
if(!$validUser) $errorMsg = "Invalid username or password.";
else $_SESSION["login"] = true;
}
if($validUser) {
header("Location: /login-success.php"); die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Login</title>
</head>
<body>
<form name="input" action="" method="post">
<label for="username">Username:</label><input type="text" value="<?= $_POST["username"] ?>" id="username" name="username" />
<label for="password">Password:</label><input type="password" value="" id="password" name="password" />
<div class="error"><?= $errorMsg ?></div>
<input type="submit" value="Home" name="sub" />
</form>
</body>
</html>
Thank you!
0
0
0
0
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<script>
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
function signUp(){
var email = document.getElementById("email");
var password = document.getElementById("password");
const promise = auth.createUserWithEmailAndPassword(email.value, password.value);
promise.catch(e => alert(e.message));
alert("Signed Up");
}
function signIn(){
var email = document.getElementById("email");
var password = document.getElementById("password");
const promise = auth.signInWithEmailAndPassword(email.value, password.value);
promise.catch(e => alert(e.message));
}
function signOut(){
auth.signOut();
alert("Signed Out");
}
auth.onAuthStateChanged(function(user){
if(user){
var email = user.email;
alert("Signed in as " + email);
//Take user to a different or home page
//is signed in
}else{
alert("No Active User");
//no user is signed in
}
});g
</script>
<style type="text/css">
body{
background-color: #55d6aa;
}
h1{
background-color: #ff4d4d;
margin: 10px auto;
text-align: center;
color: white;
}
#formContainer{
background-color: white;
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
width: 25%;
height: 45;
margin: 10px auto;
}
#header{
width: 100%;
height: 10px;
background: black;
}
#email{
width: 70%;
height: 40px;
display:block;
margin: 25px auto;
border: none;
outline: none;
border-bottom: 2px solid black;
}
#password{
width: 70%;
height: 40px;
display: block;
margin: 10px auto;
border: none;
outline: none;
border-bottom: 2px solid black;
}
#signUp{
background-color: #ff4d4d;
color: white;
border: none;
font-weight: bold;
padding: 15px 32px;
border-radius: 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 13px;
margin-top: 20px;
margin-left: 50px;
}
#signIn{
background-color: #32ff7e;
color: white;
font-weight: bold;
border: none;
padding: 15px 35px;
border-radius: 10px;
text-align: center;
text-decoration: none;
font-size: 13px
}
#signOut{
background-color: #FFA500;
color: white;
border: none;
padding: 12px 32px;
border-radius: 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 13px;
margin-top: 9px;
margin-left: 74px;
font-weight: bold;
}
button: hover{
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 7px 50px 0 rgba(0,0,0,0,.19);
}
</style>
</head>
<body>
<h1>Login Here</h1>
<div id="formContainer">
<div id="header"> </div>
<input type="email" placeholder="Email" id="email">
<input type="password" placeholder="Password" id="password">
<button onclick="signUp()" id="signUp"> Sign Up </button>
<button onclick="signIn()" id="signIn"> Sign In </button>
<button onclick="signOut()" id="signOut"> Sign Out </button>
Continue</a>
</body>
</html>
Thank you!
0
0
Are there any code examples left?
New code examples in category PHP