Search
 
SCRIPT & CODE EXAMPLE
 

PHP

login page php mysql check if user is verified in the database before user can be logged in

// call the login() function if register_btn is clicked
if (isset($_POST['login_btn'])) {
	login();
}

// LOGIN USER
function login(){
	global $db, $username, $errors;

	// grap form values
	$username = e($_POST['username']);
	$password = e($_POST['password']);

	// make sure form is filled properly
	if (empty($username)) {
		array_push($errors, "Username is required");
	}
	if (empty($password)) {
		array_push($errors, "Password is required");
	}

	// attempt login if no errors on form
	if (count($errors) == 0) {
		$password = md5($password);

		$query = "SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1";
		$results = mysqli_query($db, $query);

		if (mysqli_num_rows($results) == 1) { // user found
			// check if user is admin or user
			$logged_in_user = mysqli_fetch_assoc($results);
			if ($logged_in_user['user_type'] == 'admin') {

				$_SESSION['user'] = $logged_in_user;
				$_SESSION['success']  = "You are now logged in";
				header('location: admin/home.php');		  
			}else{
				$_SESSION['user'] = $logged_in_user;
				$_SESSION['success']  = "You are now logged in";

				header('location: index.php');
			}
		}else {
			array_push($errors, "Wrong username/password combination");
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Php :: Between Two Dates day count and removed Sunday using php 
Php :: function wp_maintenance_mode() { 763 
Php :: the_fiel 
Php :: PHP wordwrap — Wraps a string to a given number of characters 
Php :: install PHP extension "amqp" not found, please install it 
Php :: child data retrive without timestamp laravel 
Php :: Laravel 8 Factory - One To Many (Polymorphic) Relationship 
Php :: Laravel efficient way to remove X records if there are duplicates 
Php :: Laravel Mix npm run production error 
Php :: php list all files in directory and subdirectories 
Php :: strtolower cyrillic 
Php :: Laravel 7 view @php 
Php :: php post data empty 
Php :: apache/2.4.52 (win64) openssl/1.1.1m php/8.1.2 server at localhost port 80 
Php :: woocommerce check if shop page 
Php :: simple php round example 
Php :: codes for php 
Php :: error logs wp 
Php :: php include file from file included before 
Php :: add method to laravel blade 
Java :: android manifest cleartext traffic permitted 
Java :: bukkit scoreboard 
Java :: lombok maven dependency 
Java :: java execution time 
Java :: java dictionary foreach 
Java :: java string to boolean 
Java :: Could not find any matches for com.transistorsoft:tsbackgroundfetch:+ as no versions of com.transistorsoft:tsbackgroundfetch are available. 
Java :: convert a string to int in java 
Java :: @SpringBootApplication 
Java :: java math.pi 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =