I'm struggling with the project I want to make. I need to do a multi level login by address, so if new user registers with its unique address, the mysql database would update. Also, few users (1-8 people) could register to the same address and could see the same information. I have set up the login and register forms successfully, it sends the information to the database.
The thing is, the information (the html pages) which user will see after they register can't be static, they needs to be created with every new address registered.
I hope I stated my problem as clearly as possible.
My codes:
Register.php
<?php
session_start();
if(isset($_SESSION['usr_id'])) {
header("Location: index.php");
}
include_once 'Dbconnect.php';
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['signup'])) {
$username = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$cpassword = mysqli_real_escape_string($con, $_POST['cpassword']);
$city = mysqli_real_escape_string($con, $_POST['city']);
$street = mysqli_real_escape_string($con, $_POST['street']);
$number = mysqli_real_escape_string($con, $_POST['number']);
//name can contain only alpha characters and space
if (!preg_match("/^[a-zA-Z ]+$/",$username)) {
$error = true;
$username_error = "Name must contain only alphabets and space";
}
if(strlen($password) < 6) {
$error = true;
$password_error = "Password must be minimum of 6 characters";
}
if($password != $cpassword) {
$error = true;
$cpassword_error = "Password and Confirm Password doesn't match";
}
if (!$error) {
if(mysqli_query($con, "INSERT INTO login(username,password,city,street,number) VALUES('" . $username . "', '" . md5($password) . "', '" .$city . "', '" .$street . "', '" .$number . "')")) {
$successmsg = "Successfully Registered! <a href='login.php'>Click here to Login</a>";
} else {
$errormsg = "Error in registering...Please try again later!";
}
}
}
?>
Login.php
<?php
session_start();
if(isset($_SESSION['usr_id'])!="") {
header("Location: ../index.php");
}
include_once 'Dbconnect.php';
//check if form is submitted
if (isset($_POST['login'])) {
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$result = mysqli_query($con, "SELECT * FROM login WHERE username = '" . $username. "' and password = '" . md5($password) . "'");
if ($row = mysqli_fetch_array($result)) {
$_SESSION['usr_id'] = $row['id'];
$_SESSION['usr_name'] = $row['name'];
header("Location: ../index1.php");
} else {
$errormsg = "Incorrect Email or Password!!!";
}
}
?>
EDITED:
<?php
include "config.php";
if(isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password = md5($_POST['password']);
$stmt = $db->prepare("SELECT * FROM login WHERE username=? AND password=? ");
$stmt->bindParam(1, $username);
$stmt->bindParam(2, $password);
$stmt->execute();
$row = $stmt->fetch();
$user = $row['username'];
$pass = $row['password'];
$id = $row['id'];
$type = $row['type'];
if($username==$user && $pass==$password){
session_start();
$_SESSION['username'] = $user;
$_SESSION['password'] = $pass;
$_SESSION['id'] = $id;
$_SESSION['type'] = $type;
?>
<script>window.location='index.php'</script>
<?php
} else {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span></button>
<strong>Woops! </strong>Password is incorrect
</div>
<?php
}
}
?>