Jump to content


Photo

Help with login form


  • Please log in to reply
3 replies to this topic

#1 Mlaaa

Mlaaa

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 31 March 2013 - 07:49 PM

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



#2 JaysonDotPH

JaysonDotPH

    Advanced Member

  • Members
  • PipPipPip
  • 177 posts
  • LocationPhilippines

Posted 31 March 2013 - 10:07 PM

in your welcome page or home.php you must set session

 

session_start();
		
	if(!isset($_SESSION['username']))
		{
			header("Location:login.php");
			exit();
		}

<p> Welcome : <?php echo $_SESSION['username']; ?></p>


#3 Mlaaa

Mlaaa

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 01 April 2013 - 11:29 AM

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



#4 JaysonDotPH

JaysonDotPH

    Advanced Member

  • Members
  • PipPipPip
  • 177 posts
  • LocationPhilippines

Posted 01 April 2013 - 10:53 PM

so, what, exactly the problem you point out? 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com