Mlaaa Posted April 1, 2013 Share Posted April 1, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/276366-help-with-login-form/ Share on other sites More sharing options...
Yohanne Posted April 1, 2013 Share Posted April 1, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/276366-help-with-login-form/#findComment-1422207 Share on other sites More sharing options...
Mlaaa Posted April 1, 2013 Author Share Posted April 1, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/276366-help-with-login-form/#findComment-1422276 Share on other sites More sharing options...
Yohanne Posted April 2, 2013 Share Posted April 2, 2013 so, what, exactly the problem you point out? Quote Link to comment https://forums.phpfreaks.com/topic/276366-help-with-login-form/#findComment-1422391 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.