Jump to content

Trying to display username through session


designedfree4u

Recommended Posts

I Created a login area and members page, which works. What i want is for the username to be displayed. From what i gather session would be the awnser but its not working for me heres what i got.

 

 

login page

<?php
session_start();
$_SESSION['username'] = $_POST['username'];

$user_area_location = 'account.php'; // Location of the user area
// Connect to MySQL database:
$username="";
$password="";
$database="";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$error = array();
if($_GET['action']) {
switch($_GET['action']) {
case 'logoff':
unset($_SESSION['loggedIn']);
array_push($error, 'You were logged off.');
break;
}
}
if(!$error) {
if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); }
if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); }
}
if(!$error){
$result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\'');
if($row = @mysql_fetch_row($result)) {
$_SESSION['loggedIn'] = true;
header('Location: '.$user_area_location);
die('<a href="'.$user_area_location.'">Go to your user account</a>');
}else{
array_push($error, 'The credentials you provided were not correct');
}
}
?>
<!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=iso-8859-1" />
<title>Login</title>
</head>
<body>
<table width="284" height="162" border="0" cellpadding="0" cellspacing="2">
  <form method="post" action="login.php">
    <?php if(isset($error) && $error) { ?>
    <tr> 
      <td colspan="2"> <ul>
          <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?>
        </ul></td>
    </tr>
    <?php } ?>
    <tr> 
      <td>Username:</td>
      <td><input type="text" name="username" /></td>
    </tr>
    <tr> 
      <td>Password:</td>
      <td><input type="password" name="password" /></td>
    </tr>
    <tr> 
      <td> </td>
      <td><input type="submit" name="submit" value="Login!" /></td>
    </tr>
    <tr> 
      <td colspan="2">Not a User? <a href="register.php">Register Here</a></td>
    </tr>
  </form>
</table>
</body>
</html>

 

Members area

 

<?php session_start();
session_start();
$_SESSION['username'] = $_POST['username'];
if(!isset($_SESSION['loggedIn'])) { header('Location: login.php'); die('<a href="login.php">Login first!</a>'); }





?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<p> </p>
<table width="791" height="515" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr> 
    <td width="205" bgcolor="#FFFFFF"> <? echo $session_username ?> 
</td>
    <td width="198" bgcolor="#FFFFFF"><img src="japimages/addfiles.jpg" width="150" height="106"></td>
    <td width="236" bgcolor="#FFFFFF"><img src="japimages/deletefiles.jpg" width="150" height="106"></td>
    <td width="152" height="135" colspan="3" bgcolor="#FFFFFF"> </td>
  </tr>
  <tr> 
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"><img src="japimages/adduser.jpg" width="150" height="99"></td>
    <td bgcolor="#FFFFFF"><img src="japimages/deleteuser.jpg" width="150" height="99"></td>
    <td height="99" colspan="3" bgcolor="#FFFFFF"> </td>
  </tr>
  <tr> 
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"> </td>
    <td height="150" colspan="3" bgcolor="#FFFFFF"> </td>
  </tr>
  <tr> 
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"> <p><a href="login.php?action=logoff">Log off</a> </p></td>
    <td colspan="3" bgcolor="#FFFFFF"> <div align="center"> 
        <p> </p>
      </div></td>
  </tr>
</table>
<p>  </p>
</body>
</html>

Link to comment
Share on other sites

first off don't suppress mysql errors they are a good thing.

secondly take the data in $row in your login function area and attach it to sessions

i.e

$row = mysql_fetch_assoc($result);
$_SESSION['Username'] = $row['Username'];

and then you can recall it for the life of the session

Link to comment
Share on other sites

i placed the code like this but still no succsess

 

login.php

<?php
session_start();
$row = mysql_fetch_assoc($result);
$_SESSION['username'] = $row['username'];

$user_area_location = 'account.php'; // Location of the user area
// Connect to MySQL database:
$username="";
$password="";
$database="";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$error = array();
if($_GET['action']) {
switch($_GET['action']) {
case 'logoff':
unset($_SESSION['loggedIn']);
array_push($error, 'You were logged off.');
break;
}
}
if(!$error) {
if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); }
if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); }
}
if(!$error){
$result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\'');
if($row = @mysql_fetch_row($result)) {
$_SESSION['loggedIn'] = true;
header('Location: '.$user_area_location);
die('<a href="'.$user_area_location.'">Go to your user account</a>');
}else{
array_push($error, 'The credentials you provided were not correct');
}
}
?>
<!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=iso-8859-1" />
<title>Login</title>
</head>
<body>
<table width="284" height="162" border="0" cellpadding="0" cellspacing="2">
  <form method="post" action="login.php">
    <?php if(isset($error) && $error) { ?>
    <tr> 
      <td colspan="2"> <ul>
          <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?>
        </ul></td>
    </tr>
    <?php } ?>
    <tr> 
      <td>Username:</td>
      <td><input type="text" name="username" /></td>
    </tr>
    <tr> 
      <td>Password:</td>
      <td><input type="password" name="password" /></td>
    </tr>
    <tr> 
      <td> </td>
      <td><input type="submit" name="submit" value="Login!" /></td>
    </tr>
    <tr> 
      <td colspan="2">Not a User? <a href="register.php">Register Here</a></td>
    </tr>
  </form>
</table>
</body>
</html>

 

And then account.php (is this the right way to display the username in the members area?) =

<? echo $session_username ?>

 

<?php
session_start();
$row = mysql_fetch_assoc($result);
$_SESSION['username'] = $row['username'];

$user_area_location = 'account.php'; // Location of the user area
// Connect to MySQL database:
$username="";
$password="";
$database="";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$error = array();
if($_GET['action']) {
switch($_GET['action']) {
case 'logoff':
unset($_SESSION['loggedIn']);
array_push($error, 'You were logged off.');
break;
}
}
if(!$error) {
if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); }
if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); }
}
if(!$error){
$result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\'');
if($row = @mysql_fetch_row($result)) {
$_SESSION['loggedIn'] = true;
header('Location: '.$user_area_location);
die('<a href="'.$user_area_location.'">Go to your user account</a>');
}else{
array_push($error, 'The credentials you provided were not correct');
}
}
?>
<!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=iso-8859-1" />
<title>Login</title>
</head>
<body>
<table width="284" height="162" border="0" cellpadding="0" cellspacing="2">
  <form method="post" action="login.php">
    <?php if(isset($error) && $error) { ?>
    <tr> 
      <td colspan="2"> <ul>
          <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?>
        </ul></td>
    </tr>
    <?php } ?>
    <tr> 
      <td>Username:</td>
      <td><input type="text" name="username" /></td>
    </tr>
    <tr> 
      <td>Password:</td>
      <td><input type="password" name="password" /></td>
    </tr>
    <tr> 
      <td> </td>
      <td><input type="submit" name="submit" value="Login!" /></td>
    </tr>
    <tr> 
      <td colspan="2">Not a User? <a href="register.php">Register Here</a></td>
    </tr>
  </form>
</table>
</body>
</html>

Link to comment
Share on other sites

I didn't see the change I suggested either!

 

Are your sessions working at all? Make sure your web server has write access to the session folder. I think the default is just the root, but I'm not sure. I recommend setting a session folder.

Link to comment
Share on other sites

sorry new to all this. I created a folder named session and gave it 777

 

Here is the changes you suggest right?

 

login.php

<?php
session_start();
$row = mysql_fetch_assoc($result);
$_SESSION['username'] = $row['username'];

$user_area_location = 'account.php'; // Location of the user area
// Connect to MySQL database:
$username="jaybirdf_Admin";
$password="607101593";
$database="jaybirdf_RealEstate";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$error = array();
if($_GET['action']) {
switch($_GET['action']) {
case 'logoff':
unset($_SESSION['loggedIn']);
array_push($error, 'You were logged off.');
break;
}
}
if(!$error) {
if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); }
if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); }
}
if(!$error){
$result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\'');
if($row = @mysql_fetch_row($result)) {
$_SESSION['loggedIn'] = true;
header('Location: '.$user_area_location);
die('<a href="'.$user_area_location.'">Go to your user account</a>');
}else{
array_push($error, 'The credentials you provided were not correct');
}
}
?>
<!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=iso-8859-1" />
<title>Login</title>
</head>
<body>
<table width="284" height="162" border="0" cellpadding="0" cellspacing="2">
  <form method="post" action="login.php">
    <?php if(isset($error) && $error) { ?>
    <tr> 
      <td colspan="2"> <ul>
          <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?>
        </ul></td>
    </tr>
    <?php } ?>
    <tr> 
      <td>Username:</td>
      <td><input type="text" name="username" /></td>
    </tr>
    <tr> 
      <td>Password:</td>
      <td><input type="password" name="password" /></td>
    </tr>
    <tr> 
      <td> </td>
      <td><input type="submit" name="submit" value="Login!" /></td>
    </tr>
    <tr> 
      <td colspan="2">Not a User? <a href="register.php">Register Here</a></td>
    </tr>
  </form>
</table>
</body>
</html>

 

Account.php

<?php 
session_start();
$_SESSION['username'] = $_POST['username']; 

if(!isset($_SESSION['loggedIn'])) { header('Location: login.php'); die('<a href="login.php">Login first!</a>'); }





?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<p> </p>
<table width="791" height="515" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr> 
    <td width="205" bgcolor="#FFFFFF"> <? echo $session_username ?> 
</td>
    <td width="198" bgcolor="#FFFFFF"><img src="japimages/addfiles.jpg" width="150" height="106"></td>
    <td width="236" bgcolor="#FFFFFF"><img src="japimages/deletefiles.jpg" width="150" height="106"></td>
    <td width="152" height="135" colspan="3" bgcolor="#FFFFFF"> </td>
  </tr>
  <tr> 
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"><img src="japimages/adduser.jpg" width="150" height="99"></td>
    <td bgcolor="#FFFFFF"><img src="japimages/deleteuser.jpg" width="150" height="99"></td>
    <td height="99" colspan="3" bgcolor="#FFFFFF"> </td>
  </tr>
  <tr> 
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"> </td>
    <td height="150" colspan="3" bgcolor="#FFFFFF"> </td>
  </tr>
  <tr> 
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"> </td>
    <td bgcolor="#FFFFFF"> <p><a href="login.php?action=logoff">Log off</a> </p></td>
    <td colspan="3" bgcolor="#FFFFFF"> <div align="center"> 
        <p> </p>
      </div></td>
  </tr>
</table>
<p>  </p>
</body>
</html>

Still no worky

 

Link to comment
Share on other sites

ok how about through cookie

right now this works but i want it to display the user that is logged on

 

Login.php

session_start();

$username = 'jonny4';

setcookie('username', $username);

 

Account.php

echo $_COOKIE['username'];

 

Full code for login.php

<?php 
session_start();

$username = 'jonny4';

setcookie('username', $username);




$user_area_location = 'account.php'; // Location of the user area
// Connect to MySQL database:
$username="jaybirdf_Admin";
$password="607101593";
$database="jaybirdf_RealEstate";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$error = array();
if($_GET['action']) {
switch($_GET['action']) {
case 'logoff':
unset($_SESSION['loggedIn']);
array_push($error, 'You were logged off.');
break;
}
}
if(!$error) {
if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); }
if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); }
}
if(!$error){
$result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\'');
if($row = @mysql_fetch_row($result)) {
$_SESSION['loggedIn'] = true;
header('Location: '.$user_area_location);
die('<a href="'.$user_area_location.'">Go to your user account</a>');
}else{
array_push($error, 'The credentials you provided were not correct');
}
}
?>
<!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=iso-8859-1" />
<title>Login</title>
</head>
<body>
<table width="284" height="162" border="0" cellpadding="0" cellspacing="2">
  <form method="post" action="login.php">
    <?php if(isset($error) && $error) { ?>
    <tr> 
      <td colspan="2"> <ul>
          <?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?>
        </ul></td>
    </tr>
    <?php } ?>
    <tr> 
      <td>Username:</td>
      <td><input type="text" name="username" /></td>
    </tr>
    <tr> 
      <td>Password:</td>
      <td><input type="password" name="password" /></td>
    </tr>
    <tr> 
      <td> </td>
      <td><input type="submit" name="submit" value="Login!" /></td>
    </tr>
    <tr> 
      <td colspan="2">Not a User? <a href="register.php">Register Here</a></td>
    </tr>
  </form>
</table>
</body>
</html>

 

Instead of jonny4 should it be $result ?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.