Display username once user has logged in - php

Hello i am try to display the username after they log in.
here is my code
This is the page i would like to show it.
index.php
<?php
require_once 'classes/Membership.php';
$membership = New Membership();
$membership->confirm_Member();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home Page</title>
<link href="css/me.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top">
</div>
<div id="top-register">
<?
$_SESSION['username']
?>
<a href="login.php?status=loggedout">
Log Out </a>
</div>
<div id="top-login">
</div>
<div id="line">
<div id="banner-text">
Testing
</div>
<div id="banner">
</div>
</div>
<center>
<div id="plan">
<div id="plan-innder">
<img src="images/plan/starter.png" alt="Starter" width="250" height="300" />
<img src="images/plan/regular.png" alt="Regular" width="250" height="300" />
<img src="images/plan/advanced.png" alt="Advanced" width="250" height="300" />
</div>
</div>
enter code here
</center>
</body>
</html>
The workings
membership.php
<?php
require 'Mysql.php';
class Membership {
function validate_user($un, $pwd) {
$mysql = New Mysql();
$ensure_credentials = $mysql->verify_Username_and_Pass($un, md5($pwd));
if($ensure_credentials) {
$_SESSION['status'] = 'authorized';
header("location: index.php");
} else return "Please enter a correct username and password";
}
function log_User_Out() {
if(isset($_SESSION['status'])) {
unset($_SESSION['status']);
if(isset($_COOKIE[session_name()]))
setcookie(session_name(), '', time() - 1000);
session_destroy();
}
}
function confirm_Member() {
session_start();
if($_SESSION['status'] !='authorized') header("location: login.php");
}
}
Mysql.php
This is what is connecting to the data base.
require_once 'includes/constants.php';
class Mysql {
private $conn;
function __construct() {
$this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or
die('There was a problem connecting to the database.');
}
function verify_Username_and_Pass($un, $pwd) {
$query = "SELECT *
FROM users
WHERE username = ? AND password = ?
LIMIT 1";
if($stmt = $this->conn->prepare($query)) {
$stmt->bind_param('ss', $un, $pwd);
$stmt->execute();
if($stmt->fetch()) {
$stmt->close();
return true;
}
}
}
}

In your index.php write the following code:
<div id="top-register">
<?
echo "Hello" .$_SESSION['username'];
?>

Create a session variable which will save the username you got from the query then echo that session variable in your page.

I think, you missed the echo before. Change
<?
$_SESSION['username']
?>
to
<?
echo $_SESSION['username']
?>

In class Membership change this...
if($ensure_credentials) {
$_SESSION['status'] = 'authorized';
header("location: index.php");
} else return "Please enter a correct username and password";
To this...
if($ensure_credentials) {
$_SESSION['status'] = 'authorized';
$_SESSION['username'] = $un;
header("location: index.php");
} else return "Please enter a correct username and password";

I used the same nettuts tutorial for my user login page. This is what works for me to show logged in user on index.php page.
INSERT IN BODY OF INDEX.PHP PAGE:
<?php
echo "Welcome ", $_SESSION['username'];
?>

i think you missed to start session on your index.php page
so enter this line at the starting of your page
session_start();

Related

PHP Session doesn't exist on index.php

I get this error while checking for user session on index.php. Once a user has logged on, I want to display a "Log out" button in the menu. This is the code:
session.php
<?php
include('config.php');
session_start();
$user_check = $_SESSION['login_user'];
$ses_sql = mysqli_query($db,"select username from users where username = '$user_check' ");
$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$login_session = $row['username'];
?>
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include('session.php');
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/css/main.css" />
<title>LVRP</title>
</head>
<body>
<div id="top-line"></div>
<div id="header">
<div class="wrapper">
<div class="logo"> </div>
</div>
</div>
<div id="menu">
<div class="wrapper">
<ul id="navigation">
<li><a class="active" href="index.php">Home</a></li>
<li><a class="inactive" href="/forum" target="_blank">Forums</a></li>
<li><a class="inactive" href="ucp.php">User Control Panel</a></li>
<?php if(isset($_SESSION['login_user'])){echo('<li><a class="inactive" href="logout.php">Log out</a></li>');} ?>
</ul>
</div>
</div>
<div id="content">
</div>
</body>
</html>
It returns Notice: Undefined index: login_user in /var/www/html/session.php on line 5 on index.php
"It dies with a blank page."
Enable errors using
ini_set('display_errors', 'On');
At the top of your page, in your PHP tags and post the error.
try the following corrections:
<?php
include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);
$sql = "SELECT ID FROM users WHERE username = '$myusername' and password = '$mypassword'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
//$active = $row['active'];
$active = $row['ID'];
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1) {
// This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0. (From: http://php.net/manual/en/function.session-register.php)
//session_register("myusername");
$_SESSION['login_user'] = $myusername;
header("location: ucp.php");
}else {
$error = "Wrong username/password.";
echo "<pre>";var_dump($error);exit();
}
}
?>

php mysql error index.php

My error is always said "invalid username and password",
please any one can help?
i want to fix some errors"'please help
i want to do this like in this link please see" this is a program i like to do
http://alumnisys.hostei.com/
please any one can solve this problem""
the error is is in variable sysntax""
code below is i like to fix
heres my other code""
connect.php
login.php
index.php
admin.php
staff.php
student.php
|
Username:
Password:
S u b m i t
This is a sample program of 3 accounts use this as your guide to your case study.
The pdf files is in the admin page login first before download:)
note: you need to debug the codes :)
note: the database is in the student page.
Admin Account
username: admin
password: admin
Staff Account
username staff
password staff
student account
username pedro
password pedropedro
Connect.php
<?php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('psu1', $db);
?>
Index.php
<?php
session_start();
$pg='hm';
if($_SESSION['usertype']=='admin'){
header('location: admin.php');
}elseif($_SESSION['usertype']=='staff'){
header('location: staff.php');
}elseif($_SESSION['usertype']=='alumni'){
header('location: alumnu.php');
}else{
}
$msg='';
if(isset($_POST['do'])){
$uname = $_POST['username'];
$upass = $_POST['password'];
if(($uname=='') && ($upass=='')){
$msg = 'frmError';
$m = 'Dont leave blanks...';
}else{
include('connect.php');
$sql="SELECT * FROM alumni_login WHERE userrname='".$uname."' AND password='".md5($upass)."'";
$result=mysql_query($sql);
$rc = mysql_num_rows($result);
if($rc==0){
$msg = 'frmError';
$m = 'Invalid Username or Password';
}else{
$row = mysql_fetch_assoc($result);
$ip=$_SERVER['REMOTE_ADDR'];
$sql2="INSERT INTO login_infos VALUES(".$row['userid'].",
'".$row['username']."', '".date('Y-m-d H:i:s')."', '$ip')";
$result2=mysql_query($sql2);
if($result2){
$_SESSION['username'] = $row['username'];
$_SESSION['useraydi'] = $row['userid'];
$_SESSION['usertype'] = $row['usertype'];
if($row['usertype']=='admin'){
header('location: admin.php');
}elseif($row['usertype']=='staff'){
header('location: staff.php');
}elseif($row['usertype']=='alumni'){
header('location: student.php');
}
}
}
}
}
?>
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1251" />
<link rel="stylesheet" href="css/main.css" type="text/css" />
<style type="text/css">
<!--
.style1 {font-size: 10px}
.style2 {
font-size: 11px;
font-weight: bold;
}
</style>
</head>
<body>
<?php include('login.php'); ?>
</div>
</body>
</html>
Admin.php
<?php
session_start();
$pg='hm';
if($_SESSION['usertype']!='admin'){
header('location: index.php');
}
?>
<head>
<title>Admin-Main Page</title>
</head>
<body>
<li style="background: none;">Welcome ADMIN</li>
<h3>Welcome System Administration.</h3>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<li><a href="logout.php"><img src="menu/logout.png"
/></a></li>
</body>
</html>
Staff.php
<?php
session_start();
$pg='st';
if($_SESSION['usertype']!='staff'){
header('location: index.php');
}
include('connect.php');
$sql = "SELECT * FROM alumni_login WHERE userid='".$_SESSION['useraydi']."'";
$result = mysql_query($sql);
$rc=#mysql_num_rows($result);
if($rc>0){
$row=mysql_fetch_assoc($result);
$neym=$row['username'];
}
?>
<html><title>staff page</title>
</head>
<body>
<li style="background: none;">Welcome STAFF</li>
<h3>Staff Main Page</h3>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<li><a href="logout.php"><img src="menu/logout.png"
/></a></li>
</div>
</body>
</html>
Student.php
<?php
session_start();
if($_SESSION['usertype']!='student'){
header('location: index.php');
}
$pg='hm';
include('../student_for_demo correct codes/connect.php');
$sql = "SELECT * FROM alumni_info WHERE userid='".$_SESSION['useraydi']."'";
$result = mysql_query($sql);
$rc=#mysql_num_rows($result);
if($rc>0){
$row=mysql_fetch_assoc($result);
$neym=$row['firstname'];
if($row['myphoto']!='')
if(file_exists($uploads.$row['myphoto']))
$imgphoto = $uploads.$row['myphoto'];
else
$imgphoto = $uploads.'nopic.gif';
else
$imgphoto = $uploads.'nopic.gif';
}
?>
<html>
<head>
<title>student page</title>
<td id="content" valign="top"><h2>Welcome, <?php echo $neym;?></h2>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<li><a href="../student_for_demo correct codes/logout.php"><img src="../student_for_demo
correct codes/menu/logout.png" /></a></li>
</div>
</body>
</html>
Inc.login.php
<form method="post" id="loginform" action="index.php">
<div align="center"><strong><font color="#003300" size="2"><span class="<?php echo $msg;
?>"><font color="#EDF5FE">|</font><?php echo $m; ?></span></font></strong></div>
<table width="222"><tr height="30">
<td align="right"><strong>Username:</strong></td>
<td><input name="username" type="text"></td>
</tr><tr>
<td align="right"><strong>Password:</strong></td>
<td><input name="password" type="password"></td>
</tr></table>
<br />
<input type="image" align="center" src="images/login.png" name="do" value="S u b m i t" />
</form>
Logout.php
<?php
session_start();
session_destroy();
header("location: index.php");
?>
I have check, In your query there is spelling mistake
New Query
$sql="SELECT * FROM alumni_login WHERE username='".$uname."' AND password='".md5($upass)."'";
You have used userrname it should be username

My login page is not working

I am designing a simple login page that can allow users to login.
But I am not able to login. I have checked my data In database,
in fact I also tried to edit database table's data,
but it didn't help me either.
It tells me that my email or password is incorrect. I don't know why this is happening.
Here's my code, Please help me to solve this problem
<div class="header">
<a href="<?php echo $url_home; ?>"><img src="<?php echo
$design; ?>/images/logo1.png" alt="Forum" style="float:right;" /></a>
</div>
<div class="message">You have successfully been logged out.<br />
Home</div>
<?php
}
else
{
$ousername = '';
if(isset($_POST['username'], $_POST['password']))
{
if(get_magic_quotes_gpc())
{
$ousername = stripslashes($_POST['username']);
$username = mysql_real_escape_string(stripslashes
($_POST['username']));
$password = stripslashes($_POST['password']);
}
else
{
$username = mysql_real_escape_string($_POST
['username']);
$password = $_POST['password'];
}
$req = mysql_query('select password,id from users where
username="'.$username.'"');
$dn = mysql_fetch_array($req);
if($dn['password']==sha1($password) and mysql_num_row
($req)>0)
{
$form = false;
$_SESSION['username'] = $_POST['username'];
$_SESSION['userid'] = $dn['id'];
if(isset($_POST['memorize']) and $_POST['memorize']
=='yes')
{
$one_year = time()+(60*60*24*365);
setcookie('username', $_POST['username'],
$one_year);
setcookie('password', sha1($password),
$one_year);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-
8" />
<link href="<?php echo $design; ?>/style.css" rel="stylesheet"
title="Style" />
<title>Login</title>
</head>
<body>
<div class="header">
<a href="<?php echo $url_home; ?>"><img src="<?php echo
$design; ?>/images/logo1.png" alt="Forum" /></a>
</div>
<div class="message">You have successfully been logged.<br />
<a href="<?php echo $url_home; ?>"><b>Click here to go to main
forum</b></a></div>
<?php
}
else
{
$form = true;
$message = 'The username or password you entered is
invalid.';
}
}
else
{
$form = true;
}
if($form)
{
?>
I recommend you take a look at PHP's PDO instead. Much easier and secure to work with.

How come the user name is not shown?

I wrote some simple login script for a school assignment. I need to ask the user to log in redirect them to the main page, and display their username on top of the main page. I've been following the instructions I found online, but the username is not shown in the main page after the user logged in. Can someone take a look at my PHP code and give me some hints on how to resolve this? Thanks!
Here is my main php:
<?php
session_start();
echo "You are logged in as " .$_SESSION['username'];
echo "<p>Click here to logout</p>";
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysqli("abc", "edf","xyz", "123");
if($mysqli->connect_errno){
echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Test</title>
<meta charset="UTF-8">
<style type="text/css">
body {font-family:sans-serif;}
h1 {color: #0000FF;text-align: center;}
.fieldset-auto-width {display: inline-block;}
</style>
</head>
<body>
<div id="header" style="background-color:#FFA500;">
<h1>Restaurant Review</h1>
</div>
//DO SOMETHING HERE
<div id="content">
<form method="post" action="addreview.php">
</div>
</form>
</body>
</html>
Here is my login php
<?php
ob_start();
$username = $_POST['username'];
$password = $_POST['password'];
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysqli("abc", "edf","xyz", "123");
if($mysqli->connect_errno){
echo "Connection error " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
$username = mysqli_real_escape_string($mysqli, $username);
$query = "SELECT password, salt FROM member WHERE username = '$username';";
$result = mysqli_query($mysqli, $query);
// User not found. So, redirect to login_form again.
if (mysqli_num_rows($result) == 0)
{
header('Location: login.html');
}
$userData = mysqli_fetch_array($result, MYSQL_ASSOC);
$hash = hash('cs494', $userData['salt'] . hash('cs494', $password));
//Incorrect password. Redirect to login form again
if ($hash != $userData['password'])
{
header('Location: login.html');
}else {
//redirect to main page after successful login
session_start();
$_SESSION['username'] = $username;
header('Location: main.php');
}
?>
You are echo-ing outside of the HTML document, and it is probably on the page where you cannot see it. If you click View > Source you might see it printed at the top of the document before the <!DOCTYPE> declaration.
Instead of:
echo "You are logged in as " .$_SESSION['username'];
echo "<p>Click here to logout</p>";
<!DOCTYPE html>
<html>...</html>
You should move the echo inside the document like:
<!DOCTYPE html>
<html>
<head>...</head>
<body>
<?php
echo "You are logged in as " .$_SESSION['username'];
echo "<p>Click here to logout</p>";
?>
...
</body>
</html>

php redirect when wrong password

is this right the code will redirect a person to the login page when they try to access it using without going into the login page
<?php
$pass = 'password';
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
if ( $_POST["pass"] == $pass){
?>
Congrats you have log in!
<?php
}else{
header("Location: http://signin.com/");
}
?>
</body>
</html>
i ended up having a "Server error
The website encountered an error while retrieving http://www.test.com It may be down for maintenance or configured incorrectly."
You can't call header after you've already outputted some HTML. Do your password checks & redirect. above the HTML
Eg:
<?php
$pass = 'password';
if ( $_POST["pass"] != $pass){
header("Location: http://signin.com/");
exit;
}
?>
<html>
<head>
<title></title>
</head>
....
So the HTML will only show if they're successful.
You can't send a header() after any output to the user:
<?php
$pass = 'password';
if ( $_POST["pass"] == $pass)
{
?>
<html>
<head>
<title></title>
</head>
<body>
Congrats you have log in!
</body>
</html>
<?php
}
else
{
header("Location: http://signin.com/");
}
?>
Something like this would work better:
<?php
$pass = 'password';
if ($_POST["pass"] != $pass){
header("Location: http://signin.com/");
exit;
}
?>
<html>
<head>
<title></title>
</head>
<body>
Congrats you have log in!
</body>
</html>
You need to check if the user is logged in. If not, redirect and exit. If so, display the message.
Put ob_start(); at the top and ob_end_flush(); and that might fix it.
You can't output html before make a redirect with header. Code all logic before:
<?php
$pass = 'password';
if ($_POST["pass"] == $pass)
{
$message = "Congrats you have log in!";
}
else
{
header("Location: http://signin.com/");
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php echo $message; ?>
</body>

Categories