Jump to content

Display login name


andy_b_1502

Recommended Posts

Could somebody help to pint me in the right direction where to look for the correct syntax for my problem.

 

The user logs in and is sent to the members area, their username is echoed out to display that they are logged in, my problem is that it is displaying all the users in my database (currently 2 usernames). I would just like to have the current users username displayed.

 

<?php $con = mysql_connect("server","username",
															 "password");
															 if (!$con)
   																 { die('Could not connect: ' . mysql_error()); }
																 mysql_select_db("database", $con);
																 $result = mysql_query("SELECT username FROM users"
															 );
																 while($row = mysql_fetch_array($result))
   																 {
   																 echo "<tr>";
															 echo "<td>" . $row['username'] . "</td>";;
   																 echo "</tr>";}
																 echo "</table>"; ?>

 

What sholud i research to perform this task properly? thank you.

Link to comment
Share on other sites

Thanks, something on the lines of this:

 

$result = mysql_query("SELECT username FROM users WHERE username='$username'");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";;
echo "</tr>";}
echo "</table>"; ?>

 

i would like it to take their username value from when they click log-in and it display in the memebr's area?

Link to comment
Share on other sites

To display the username of the user.

 

the log in script stores the username in a variable i want it to be displayed on the memebers area of the site.

 

should i use session like this:

 

<?php $con = mysql_connect("removalspacecom.ipagemysql.com","removalspace",
															 "123");
															 if (!$con)
   																 { die('Could not connect: ' . mysql_error()); }
																 mysql_select_db("removalspacelogin", $con);
																 $_SESSION['username'] = $username;				
															 echo "Hello ".$_SESSION['username']; ?>

 

 

this is the log-in script that takes the $username:

 

<?php		
/*	 "Warning: Cannot modify header information - headers already sent by "	  	
To avoid the header error , give value zero to		

$mosConfig_locale_debug = 0;		
$mosConfig_locale_use_gettext = 0;	*/	
$mosConfig_locale_debug = 0;	
$mosConfig_locale_use_gettext = 0;	
ob_start();
if (isset($_POST['submitted']) && !empty($_POST['myusername']) && !empty($_POST['mypassword'])) 	
{
//check for submit button click 		
$username = $_POST['myusername'];   		
$password = $_POST['mypassword']; 				
// NOTE  - quick check		
$_SESSION['username'] = $username;				
echo "Hello ".$_SESSION['username'];		 						
// proceed with rest of code		

$self = $_SERVER['PHP_SELF'];		
$referer = $_SERVER['HTTP_REFERER'];							
#if either form field is empty return to the log-in page		
if( (!$username ) or (!$password ) ){ header(  "Location:$referer" ); exit(); }				
#connect to mysql		
$conn = @mysql_connect( "server", "username", "password" )		
or die( "Could not connect" );		
#select database 		
$rs = @mysql_select_db( "database", $conn )			
or die ( "Could not select db" );		
#create query		
$sql="select * from users where username = '". $username."' and upassword = '".$password."'";		
#execute query		
$rs = mysql_query( $sql, $conn )		
or die( mysql_error() );		
if($rs){   $_SESSION['username'] = $username;}		
#get number of rows that match username and password		
$num = mysql_numrows( $rs );		
#if there is a match the log-in is done		
if( $num != 0 ){ header( "Location:members_area.php" ); }		
else 		{ header( "Location:$referer" ); exit(); }}				
else	{
?>

 

 

Link to comment
Share on other sites

Thanks, i called session start () at line 1, so it puts it before anything to the browser,

 

<?php

session_start();

?>

 

then i take it it should display the username where ever i want it to using:

 

<?php $con = mysql_connect("removalspacecom.ipagemysql.com","removalspace","123");
															 if (!$con)
   																 { die('Could not connect: ' . mysql_error()); }
																 mysql_select_db("removalspacelogin", $con);
																 $_SESSION['username'] = $username;																				 
															 echo "Hello ".$_SESSION['username']; 
															 mysql_close($con); ?>

 

but im getting header error? what do you think i could to do to stop this error from occuring?

 

specific error code is:

 

Warning: session_start() [function.session-start]: open(/home/users/web/b109/ipg.removalspacecom/cgi-bin/tmp/sess_1719827ba3cdce3c8642d3ff0bd404fc, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php on line 2

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php on line 2

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php on line 2

 

Link to comment
Share on other sites

I use this on my site. YOu'd need to adapt it for your own of course.

 

<?php
  session_start();

    // check session variable

  if (isset($_SESSION['first_name']))
  {

         echo 'Welcome';
 echo ", {$_SESSION['first_name']}!";
        


 

just change the $_SESSION[] from first_name to whatever you use on your db.

 

btw, I use the isset is to ensure that someone is logged on.

 

The useful code as far as your query goes is {$_SESSION['first_name]}.

 

hope that helps

Link to comment
Share on other sites

Im having a little trouble getting this.

 

Ive put the session_start (); at the top of the html page at line 1 and i get headers already sent error.

 

I also placed this code where i would like the username to be displayed:

 

<?php 
session_start ();
$_SESSION['username'] = $username;				
echo "Hello ".$_SESSION['username']; ?>

 

But i get the same error message, i wonder whats happening here?

Link to comment
Share on other sites

The field name in my table "users" is called "username"

 

That is the name of the $_session created when they log-in. $_session['username']

 

If i call session like:

 

echo '<div class="medium">Welcome';

echo ", {$_SESSION['first_name']}!";

 

Is doesn't give me any header errors, however it doesn't display the users username either. It reads:

 

Welcome, !

 

Where it should read:

 

Welcome, (username)!

Link to comment
Share on other sites

well that's progress of sorts!

 

Perhaps you could try using isset function to see whether the $_SESSION[] is set?

 

http://php.net/manual/en/function.isset.php

 

I use it like this;

 

if (isset($_SESSION['first_name']))
    {

     echo '<div class="medium">Welcome';
     echo ", {$_SESSION['first_name']}!";
     echo '</div>';
  } 
else
  {
    include ('includes/header.html');	
    echo '<h1><p>You are not logged in.</p></h1><br>';
    echo '<h2><p>If you want to access the members\' area, please register or log in.</p></h2>';
   }
?>

 

Maybe there's something wrong with your log-in script if $_SESSION[] is not being set?

 

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.