Jump to content

Help with login form


Mlaaa

Recommended Posts

Hey all,

 

i made login script and all works fine, but i have problem with getting username to show like Welcome Username. I dont know if i wrong in function or when i started a session here is code

 

// function for display username
function user($username) {
	
	$username = sanitize($username);

	$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
	$result = mysql_result($query, 0, 'username');
	
	if ($result == true) {
		return true;
	} else {
		return false;
	}

}

 

this is function and when i call it it doesn't show nothing, and here is login.php

 

<?php

	include 'core/init.php';
	
	// check if user is logged
	if (!logged()) {
	
		if (isset($_POST['login'])) {
		
			$username = $_POST['username'];
			$password = $_POST['password'];
			
			if (isset($username) && isset($password)) {
			
				// check if username and password is entered
				if (empty($username) && empty($password)) {
					echo 'All fields are required';
					
				// if username exist in database
				} else if (user_exist($username) === false) {
					echo 'Username with that username dont exist.';
					
				// check if username and password match
				} else if (is_match($username, $password) === false) {
					echo 'Invalid username, password combination';
				} else {
				
					// start session
					$_SESSION['user_id'] = 1;
					
					// redirect user to user page
					header('Location: logged.php');
					exit();
				}
			
			}
		
		}
		
	} else {
		header('Location: index.php');
		exit();
	}

?>

 

so do i must make query in login.php and take username and put in in session or i can put that in function ? Please help im new in php so... be gentle :P

Link to comment
https://forums.phpfreaks.com/topic/276366-help-with-login-form/
Share on other sites

Session is started here in include init.php so i include that file everywhere i want to see if user is logged in and check with logged() func. I dont know if problem is here $_SESSION['user_id'] = 1; maybe i need to make query to take username from database and put it in session, but i want to make like this

 

function user() {
  grab data from database and compare it with logged user and started session
  tehn display only username when i call user() function
}

 

and use it like 

 

echo 'Welcome' . user();

 

not with session variable

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.