Forum Moderators: coopster

Message Too Old, No Replies

Getting an echo to appear in a certain place

         

Jamier101

4:19 pm on Dec 1, 2010 (gmt 0)

10+ Year Member



I seem to have an issue with getting my 'Incorrect username or password' message to display in the correct place. I would like it to display where the 'You are already logged in' message appears but don't seem to be able to copy the code into the correct places, can someone please help by pointing me in the right direction?


<?php

$tbl_name="users"; // Table name

// Connect to server and select database.
require_once("Connections/connection.php"); // Connection to the server

session_start();

$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];

if ($userid && $password){
/////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM users WHERE username='$userid' and password='$password'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
/////////////////////////////////////////////////////////////////////////
}

if ($rowAccount){

$_SESSION['id'] = $rowAccount['username'];

header("location:index.php");
exit;

}elseif($submitted){

echo "Incorrect username or password";
}

?>

<!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 rel="stylesheet" type="text/css" href="css/stylesheet.css">
<title>Owner Login</title>
</head>

<body>
<div id="wrapper">
<div id="container">
<div id="header">
<div id="user_box">
<?php
//if($id!=""){
//echo 'Logged in as: '. $rowAccount['username'];
//echo '<input type="button" name="Button" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
//}
if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
echo "<font color='white'>" . 'You are already logged in' . "</font>";
echo '<input type="button" name="logout" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
}
//else{
//}
?>
</div>

JuicyScript

1:21 pm on Dec 3, 2010 (gmt 0)

10+ Year Member



Try this

//Input Validations
if($userid == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}

//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}

if($rowAccount) {
if(mysql_num_rows($result) == 1) {
$member = mysql_fetch_assoc($rowAccount);
$_SESSION['ID'] = $member['id'];
$_SESSION['USERNAME'] = $member['username'];
header("location:index.php");
exit();
}else {
//Login failed

$errmsg_arr[] = 'Username or Password incorrect ";
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}
}else {
die("Query failed");
}


And Change

if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
echo "<font color='white'>" . 'You are already logged in' . "</font>";
echo '<input type="button" name="logout" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
}

to
if(!isset($_SESSION['ID']) || (trim($_SESSION['ID']) == '')) {
header("location: access-denied.php");
exit();
} elseif(isset($_SESSION['id']) && !empty($_SESSION['id'])){
echo "<font color='white'>" . 'You are already logged in as '{$_SESSION['USERNAME']} . "</font>";
echo '<input type="button" name="logout" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
}

Hope it works

JuicyScript

1:35 pm on Dec 3, 2010 (gmt 0)

10+ Year Member



Sorry i forgot to declare the array
//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

Jamier101

10:52 pm on Dec 3, 2010 (gmt 0)

10+ Year Member



I seem to have a problem getting the page to load when I use the script but am struggling to find the error, I don't really want to post too much code here but here goes:
Top of page

<?php

$tbl_name="users"; // Table name

// Connect to server and select database.
require_once("Connections/connection.php"); // Connection to the server

session_start();

$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];

if ($userid && $password){
/////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM users WHERE username='$userid' and password='$password'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
/////////////////////////////////////////////////////////////////////////
}

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Input Validations
if($userid == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}

//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}

if($rowAccount) {
if(mysql_num_rows($result) == 1) {
$member = mysql_fetch_assoc($rowAccount);
$_SESSION['ID'] = $member['id'];
$_SESSION['USERNAME'] = $member['username'];
header("location:index.php");
exit();
}else {
//Login failed

$errmsg_arr[] = 'Username or Password incorrect ';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}
}else {
die("Query failed");
}
?>


Code behind the user_box:

<?php
if(!isset($_SESSION['ID']) || (trim($_SESSION['ID']) == '')) {
header("location: access-denied.php");
exit();
} elseif(isset($_SESSION['id']) && !empty($_SESSION['id'])){
echo "<font color='white'>" . 'You are already logged in as '{$_SESSION['USERNAME']} . "</font>";
echo '<input type="button" name="logout" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
}
?>

JuicyScript

9:16 am on Dec 4, 2010 (gmt 0)

10+ Year Member



Ok i went ahead and reorganized the code
Session start shd alway be at the top of your php code, to avoid any white spaces.It triggers a lot of errors if not set as such.
Try this and lemme know if it works.
<?php
session_start();

// Connect to server and select database.
require_once("Connections/connection.php");



$userid = $_POST['userid'];
$password = $_POST['password'];
//$submitted = $_POST['submitted'];


$query = "SELECT * FROM users ";
$query .= "WHERE username = '{$userid}' ";
$query .= "AND password = '{$password}' ";
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);



//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Input Validations
if($userid == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}

//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}

if($rowAccount) {
if(mysql_num_rows($result) == 1) {
$member = mysql_fetch_assoc($rowAccount);
$_SESSION['ID'] = $member['id'];
$_SESSION['USERNAME'] = $member['username'];
header("location:index.php");
exit();
}else {
//Login failed

$errmsg_arr[] = 'Username or Password incorrect ';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}
}else {
die("Query failed");
}
?>


<!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 rel="stylesheet" type="text/css" href="css/stylesheet.css">
<title>Owner Login</title>
</head>

<body>
<div id="wrapper">
<div id="container">
<div id="header">
<div id="user_box">
<?php
if(!isset($_SESSION['ID']) || (trim($_SESSION['ID']) == '')) {
header("location: access-denied.php");
exit();
} else{
if(isset($_SESSION['id']) && (!empty($_SESSION['id']))){
echo "<font color='white'>" . 'You are already logged in as '{$_SESSION['USERNAME']} . "</font>";
echo '<input type="button" name="logout" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
}
?>

Jamier101

12:57 pm on Dec 4, 2010 (gmt 0)

10+ Year Member



I've at reading through until my eyes hurt but I just don't seem to be able to get it to run, I simply get a blank page :-S
Top of page

<?php

session_start();

error_reporting(E_ALL|E_STRICT);

// Connect to server and select database.
require_once("Connections/connection.php"); // Connection to the server

$tbl_name="users"; // Table name

$userid = $_POST['userid'];
$password = $_POST['password'];
//$submitted = $_POST['submitted'];

if ($userid && $password){
/////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM users WHERE username='$userid' and password='$password'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
/////////////////////////////////////////////////////////////////////////
}

//=================== Suggested Code ==============================

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Input Validations
if($userid == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}

//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
echo header("location: login.php");
exit();
}

if($rowAccount) {
if(mysql_num_rows($result) == 1) {
$member = mysql_fetch_assoc($rowAccount);
$_SESSION['ID'] = $member['id'];
$_SESSION['USERNAME'] = $member['username'];
header("location:index.php");
exit();
}else {
//Login failed
$errmsg_arr[] = 'Username or Password incorrect ';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: login.php");
exit();
}
}else {
die("Query failed");
}

//=================== Really Old Code ==========================
//if ($rowAccount){

//$_SESSION['id'] = $rowAccount['username'];

//header("location:index.php");
//exit;

//}elseif($submitted){

//echo "Incorrect username or password";
//}
//======================== End ==================================
?>

User Box

<?php
if(!isset($_SESSION['ID']) || (trim($_SESSION['id']) == '')) {
header("location: access-denied.php");
exit();
}else{
if(isset($_SESSION['id']) && (!empty($_SESSION['id']))){
echo "<font color='white'>" . 'You are already logged in as '{$_SESSION['USERNAME']} . "</font>";
echo '<input type="button" name="logout" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
}

//========================================== Old Code =======================================================
//if($id!=""){
//echo 'Logged in as: '. $rowAccount['username'];
//echo '<input type="button" name="Button" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
//}
//if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
//echo "<font color='white'>" . 'You are already logged in' . "</font>";
//echo '<input type="button" name="logout" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
//}
//============================================= End =========================================================
?>

Jamier101

9:06 pm on Dec 5, 2010 (gmt 0)

10+ Year Member



I initially thought the issue was with this piece of code:

//If there are input validations, redirect back to the login form 
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
echo header("location: login.php");
exit();
}


this was only because the web browser complained about a looping issue.

Jamier101

9:16 pm on Dec 9, 2010 (gmt 0)

10+ Year Member



Bump :)

Jamier101

7:19 pm on Dec 27, 2010 (gmt 0)

10+ Year Member



I'm sorry to bring this thread back to life but I've still been unable to solve the problem.