Reputation: 53
in this i wannna get the username who is logged in and display it in the home page when the username is correct and registered in database .should i use session and where to use it.how it is been done
<?php
error_log("chk.php executing");
// Get values from form
include 'config.php';
foreach ($_POST as $key => $value) {
error_log($key);
}
//error_log($_POST['username']);
$username=$_POST['username'];
$password=$_POST['password'];
// Insert data into mysql
$qry = mysql_query("SELECT * FROM useraccount WHERE username='$username'");
if(!$qry) {
die("Query Failed: ". mysql_error());
} else {
$row=mysql_fetch_array($qry);
if ($username==$row['username']) {
if($username=='' || $password=='') {
error_log("some fields are empty");
//header("Location:login.php?id=Some fields are empty");
// header("Content-Type: text/html");
// {echo "<b>Some fields are empty</b>";}
} else if($username==$row['username'] && $password==$row['password']) {
error_log("logged in");
header('Location: home.html');
// header("Content-Type: text/html");
// {echo "<b>User name password verified</b>";}
//header("Location: home.html?id=$username");
} else {
error_log("password is incorrect");
// header("Content-Type: text/html");
// {echo "<b>username already taken or your password is incorrect. Please try again</b>";}
//header("Location:.php?id=username already taken or your password is incorrect. Please try again");
}}
else
error_log("username incorrect");
}
mysql_close();
?>
html,body
{
margin:0px;
height:100%;
}
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
width: 60%;
margin: auto;
}
.content
{
width:100%;
height:400px;
}
.signup
{
height:500px;
}
.footer
{
position:relative;
background-color:black;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>ASK</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="boot.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-md-6">
<h1>school name</h1>
</div>
<div class="col-md-6">
<img src="../project/photo/l.png" height="150px"/>
</div>
</div>
</div>
</div>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#mynavbar">schoolName</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="about.html" target=_self>About Us</a></li>
<li><a href="infra.html" target=_self>Infrastructure</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="">Administration<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="stlogin.html">staff login</a></li>
<li><a href="stdetails.html">staff details</a></li>
<li><a href="class.html">class handling</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<form class="form-horizontal" action="chk.php" method="POST">
<div class="form-group">
<div class="col-xs-3">
<label for="username">Username:</label>
<input name="username" type="username" class="form-control" id="username" placeholder="Enter username">
</div></div>
<div class="form-group">
<div class="col-xs-3">
<label for="pwd">Password:</label>
<input name="password" type="password" class="form-control" id="password" placeholder="Enter password">
</div></div>
<!-- <div class="checkbox">
<label><input type="checkbox"> Remember me</label><br>
</div> -->
<button type="submit" class="btn btn-default">Submit</button><br>
</form>
</div>
<div class="footer navbar-fixed-bottom">
<p> Copyrights@ ©WWW.schools.com</p>
</div>
</body>
</html>
Upvotes: 0
Views: 811
Reputation: 138
You can use session_start();
and store the username into it.
//Initialize the session:
session_start();
<!doctype html>
<head>
.
.
.
</head>
<body>
.
.
<?php
$q = "SELECT * FROM useraccount WHERE username='$username'";
$r = $mysqli->query($q);
if ($r->num_rows == 1) {
$_SESSION = $r-> fetch_array(MYSQLI_ASSOC);
}
?>
.
</body>
</html>
You can take a look at my site for that same query at hiteachers.com
Upvotes: 0
Reputation: 198
You can use SESSION
. You have to use session_start()
in every files that you want to display the username
else if($username==$row['username'] && $password==$row['password']) {
$_SESSION['username'] = $username; // store username name in session
error_log("logged in");
header('Location: home.html');
// header("Content-Type: text/html");
// {echo "<b>User name password verified</b>";}
header("Location: home.php?id=$username"); // redirect to home.php page
You can simply display the username by echo $_SESSION['username'];
Don't forget to add session_start();
in your home.php. For more info please refer to this link http://php.net/manual/en/function.session-start.php
Upvotes: 1
Reputation: 111
You should use session_start();
After successfully login,you can post username or email like this-
<?php
include_once'dbconnect.php';
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
//print logged in user's email as saved in database
echo $userRow['email'];
////print logged in user's username as saved in database
echo $userRow['username'];
?>
This is as per my database where-
'dbconnect.php'is connection file.
'users' is tablename.
Upvotes: 0
Reputation: 107
first of all when username store in session on login time
<?php
session_start();
- List item
error_log("chk.php executing"); // Get values from form include 'config.php';
foreach ($_POST as $key => $value) { error_log($key); } //error_log($_POST['username']); $username=$_POST['username']; $password=$_POST['password']; // Insert data into mysql $qry = mysql_query("SELECT * FROM useraccount WHERE username='$username'"); if(!$qry) { die("Query Failed: ". mysql_error()); } else {
$row=mysql_fetch_array($qry);
if ($username==$row['username']) {
if($username=='' || $password=='') {
error_log("some fields are empty");
//header("Location:login.php?id=Some fields are empty");
// header("Content-Type: text/html"); // {echo "<b>Some fields are empty</b>";}
} else if($username==$row['username'] && $password==$row['password']) {
error_log("logged in");
$_SESSION['username']=$row['username'];
header('Location: home.html');
// header("Content-Type: text/html"); // {echo "<b>User name password verified</b>";}
//header("Location: home.html?id=$username");
} else {
error_log("password is incorrect");
// header("Content-Type: text/html"); // {echo "<b>username already taken or your password is incorrect. Please try again</b>";}
//header("Location:.php?id=username already taken or your password is incorrect. Please try again");
}}
else
error_log("username incorrect"); } mysql_close(); ?>
Another page we get this username and we change file name home.html to home.php
home.php
echo $_SESSION['username'];
?> output username print this page
Upvotes: 1