HTML PHP Display Username After Success Login

HTML PHP Display username after success login

Either you need to rename your onlinestore.html and login.html to be .php files so the PHP will work in them, or use the addtype option in your htaccess file.

In your onlinestore.html do this:

<?php
session_start(); // Right at the top of your script
?>

<li class='active' style='float:right;'>
<?php
if($_SESSION['logged']==true)
{
echo $_SESSION["username"];
echo '<a href="logout.php"><span>Logout</span></a></li>';
}
elseif($_SESSION['logged']==false)
{
echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
}
?>

In your checklogin.php do this:

<?php
session_start(); // Right at the top of your script
?>

if($count==1)
{
$_SESSION['logged']=true;
$_SESSION['username']=$myusername;
header("Location: onlinestore.html");
exit();
}
else
{
$_SESSION['logged']=false;
header("Location: login.html");
exit();
}

If the above doesn't work then please tell me what happens.

Do you have html files set to parse PHP code?

Or a htaccess file with:

AddType application/x-httpd-php .htm .html
?

EDIT

Try this for debugging:

In your checklogin.php do this:

<?php
session_start(); // Right at the top of your script
?>

if($count==1)
{
$_SESSION['logged']=true;
$_SESSION['username']=$myusername;
echo "Login worked";
exit();
}
else
{
$_SESSION['logged']=false;
echo "Login failed";
exit();
}

This will show you if login is working. If it's not and you get "login failed" then that is why you get the "Login/Register" link on your page.

If it shows "Login worked" then set the code back to how it was and then on your onlinestore.html page, do this at the top of the file:

echo "This is working";
exit();

What happens? Do you get the message "This is working" on the page and nothing else?

How can I display a user's username after successful php login?

on your config.php page:

    $_SESSION["username"] = "xxx";

on your home.php start a session as

       session_start();

and wherever you want to put the username:

     <?php echo $_SESSION["username"]; ?>

in your case most probably will be :

    <li><a href="#">Hello, <?php echo $_SESSION["username"]; ?></a></li>

display username AFTER user has logged in PHP MySQL

All that means is $_SESSION['firstname'] is not defined, in which case you forgot to assign a value to $_SESSION['firstname'] upon user login.

in your header.php :

<?php

session_start();

...

// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);

if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}

if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {

//fetch result into an associative array.
$user = mysqli_fetch_assoc($results);

//$_SESSION['firstname'] should go here
$_SESSION['firstname'] = $user['firstname'];

$_SESSION['username'] = $user['username'];
//$_SESSION['success'] = "You are now logged in" ;
header('location: profile.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}

HTML-PHP After login and before login

You want to use $_SESSION.

When you determine a successful login has occurred, set a $_SESSION to = something, such as "loggedintrue" or whatever.

eg:

if($count==1)
{
$_SESSION['loggedin']['username'] = $myusername;
}

Then wherever you want to serve user specific data, do something like:

if (!empty($_SESSION['loggedin']['username']))
{
echo "Hi ".$_SESSION['loggedin']['username'];
// OR show some other content, ie allow then to see more menu items.
}

You will need to have session_start(); at the very top of any page you want to use the $_SESSION on.

Also, be aware that just not showing them a page or something they need to login first to see doesn't stop them accessing it. You'd need further checks on the page, such as:

if (empty($_SESSION['loggedin']['username']))
{
echo "You are not allowed to access this page. Please login first";
// Or redirect them etc
}

EDIT: Code you wanted.

You need to check if they are logged in, if yes serve their username and a link to logout.

If not logged in then show them the login/register link and text.

A pretty simple if else!

Where you have your div or whatever which holds the text "login/register", put this:

// They are logged in
if (!empty($_SESSION['loggedin']['username']))
{
// Display their username
echo $_SESSION['loggedin']['username'];
// Display the logout link
echo "<a href='logout.php'> (Logout)</a>";
}
// Not logged in
else
{
// Display the login/register link
echo "<a href='login-register.php'>Login/Register</a>";
}

The above code will work, however please note it's quite basic.

You should really have a separate class or function which you call on every single page to check if a user is logged in or not and return certain data if yes or no.

For example:

function logged_in()
{

if (!empty($_SESSION['loggedin']['username']))
{
return $_SESSION['loggedin']['username']."<a href='logout.php'> (Logout)</a>";
}
else
{
return "<a href='login-register.php'>Login/Register</a>";
}

}

Then include the file which has that above function in on every page you need it, then in the page where your username or login/register link is, do this:

echo logged_in();  

This will display either they username, or a link to login/register.

Doing it this way means if you change anything you just change the function script file and it changes it everywhere you included the file and used the function.

Also you can simply use the logged_in() n your site wherever you want without checking sessions and setting data all the time.

It's a basic approach to separating business logic from presentation, but there's so much more to all this, such as where you keep your function files, how you include them within your application etc.

But way to much to talk about here. Read some tutorials on login and register with PHP and using include files in pages.


Side Notes:

Please make sure you validate and sanitise the user input data.

$myusername=$_POST["myusername"];
$mypassword=$_POST["mypassword"];

Maybe you do, but if not, as your code is they could wreak havoc on your database and/or website.

Also, consider using mysqli_ or PDO as the mysql_ functions are depreciated as from PHP version 5.5.0.

Also, md5() is no longer a secure method. See here for why and alternatives:

http://sg2.php.net/manual/en/faq.passwords.php#faq.passwords.fasthash

How to redirect to the page that I want after logged in and display username after successful login

You can try window.location.href for redirecting. And to pass in a value to another page, you can use sessions or make a GET request.

Using Sessions:

login_page.php

<?phpsession_start();$_SESSION['username'] = $username;?><script>window.location.href="logged_in.php";</script>

HTML Change login label to username after login

You need a simple condition

if($_SESSION['logged_in']){
echo $_SESSION['username'];
}else{
echo 'Please login';
}

When your login condition gets true set some session data or cookie data then use a condition to display content based on session or cookie

How to display user log in information at the corner of the page using php

Error in your code, your code failed to set the session, when the
condition found true, then it redirect user to another .php page as
per the condition which you have used.

Try this code.

<?php session_start(); ?>

if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);

if (empty($username)) {
array_push($errors, "Username is required");

}
if (empty($password)) {
array_push($errors, "Password is required");

}

if (count($errors) == 0) {

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

if (mysqli_num_rows($results) == 1) {
$row = mysqli_fetch_assoc($results);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";

//if you want to redirect user as per its type, you can use this condition

if('student' == $row['user_type']) {
header('Location: ogrenciarayuzu.php');
exit();
} elseif ('department' == $row['user_type']) {
header('Location: bolumsekreterligiarayuzu.php');
exit();

} elseif ('institute' == $row['user_type']) {
header('Location: enstituarayuzu.php');
exit();

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

}


Related Topics



Leave a reply



Submit