Jump to content

Getting username


Markx

Recommended Posts

Hi All

 

I'm sure this is a simple question, but a section of my website is username and password protected. Is it possible to return the name of the user so I can use it as a column heading in a table  ???

 

Thanks in advance

Mark

Link to comment
Share on other sites

Well, you get the information through a query (assuming you're using MySQL), then loop through and display the users.

 

$query = mysql_query("SELECT * FROM users") or die(mysql_error());

echo <<<html
<table border="0">
<tr>
<td><b>Username:</b></td>
</tr>
html;

while($r = mysql_fetch_assoc($query)){
echo <<<html
<tr>
<td>{$r['username']}</td>
</tr>
html;
}

echo "</table>";

 

Very basic example, hopefully you can learn from that. :)

Link to comment
Share on other sites

Here's the code I have so far.

 

<!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>Ignition Driving School</title>
<style type="text/css">
<!--
body {
background-image:  url(/beta/header-bg.gif);
background-repeat: repeat-x;
}
A { color:#000000;}
.style22 {font-size: small}
.style27 {font-family: Tahoma; font-size: small; color: #FFFFFF; }
.style28 {color: #FFFFFF}
.style30 {font-family: Tahoma; font-size: small; color: #FFFFFF; font-weight: bold; }
-->
</style>
</head>

<?php include("../menu.php"); ?>

	<td width="100%" valign="bottom"><img src="/beta/space.gif" alt="" width="1" height="1" /></td>
      </tr>

    </table></td>
  </tr>
  <tr>
    <td height="37" valign="middle" background="/beta/menu-bg.gif" bgcolor="#84CA00"><img src="/images/phone.gif" alt="Call Us" width="200" height="16" hspace="10" vspace="0" align="right" /><font color="#FFFFFF" size="2" face="Tahoma">  
<?php include("submenu.php"); ?>
  </tr>
  <tr>
    <td height="2" bgcolor="#FFBA00"><img src="/beta/space.gif" width="1" height="2" /></td>
  </tr>
  <tr>
    <td bgcolor="#84CA00"><table width="100%" height="100%" border="0" cellpadding="25">
      <tr>
        <td valign="top"><p>          
        <p><strong><font color="#FFFFFF" size="6" face="Tahoma">Christmas Party Menu </font></strong></p>          
        <p align="left" class="style22"><font color="#FFFFFF" face="Tahoma">Please enter your name and the first date you are returning to work.</font></p>
        <p align="left" class="style22"> </p>
        <form method="post" name="christmas" action="christmasform.php">

	  <table width="595" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="291"> </td>
              <td width="135"> </td>
              <td width="169"><p>Partners Name:<br />
                  (If applicable)<br />
                  <input type="text" name="textfield" />
              </p>                </td>
            </tr>
            <tr>
              <td><span class="style30">Starters</span></td>
              <td> </td>
              <td> </td>
            </tr>
            <tr>
              <td><span class="style27">Roast vine tomato and basil soup </span></td>
              <td><div align="center">
                <input name="starter" type="radio" value="soup" />
              </div></td>
              <td><div align="center">
                <input name="starterp" type="radio" value="soupp" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style27">Smoked trout and horseradish tartare, chicory leaves, pea shoots</span></td>
              <td><div align="center">
                <input name="starter" type="radio" value="trout" />
              </div></td>
              <td><div align="center">
                <input name="starterp" type="radio" value="troutp" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style27">Honey roast figs, cashel blue cheese, balsamic reduction </span></td>
              <td><div align="center">
                <input name="starter" type="radio" value="roastfigs" />
              </div></td>
              <td><div align="center">
                <input name="starterp" type="radio" value="roastfigsp" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style27">Pressed terrine of local game, herb brioche, kumquat marmalade</span></td>
              <td><div align="center">
                <input name="starter" type="radio" value="terrine" />
              </div></td>
              <td><div align="center">
                <input name="starterp" type="radio" value="terrinep" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style28"></span></td>
              <td><div align="center"></div></td>
              <td><div align="center"></div></td>
            </tr>
            <tr>
              <td><span class="style30">Mains</span></td>
              <td><div align="center"></div></td>
              <td><div align="center"></div></td>
            </tr>
            <tr>
              <td><span class="style27">Free-range roast turkey breast and chestnut stuffing wrapped in pancetta, pearl onion jus, parsnip crisps </span></td>
              <td><div align="center">
                <input name="mains" type="radio" value="turkey" />
              </div></td>
              <td><div align="center">
                <input name="mainsp" type="radio" value="turkeyp" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style27">Crispy skin salmon, creamed pearl barley, sweet potato fondant </span></td>
              <td><div align="center">
                <input name="mains" type="radio" value="salmon" />
              </div></td>
              <td><div align="center">
                <input name="mainsp" type="radio" value="salmonp" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style27">Red onion, pear and goats cheese tart tatain, sweet and sour vegetables</span></td>
              <td><div align="center">
                <input name="mains" type="radio" value="tarttatain" />
              </div></td>
              <td><div align="center">
                <input name="mainsp" type="radio" value="tarttatainp" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style27">10oz Porterhouse Ribeye Steak, braised mushroom, roast shallot, red wine jus. </span></td>
              <td><div align="center">
                <input name="mains" type="radio" value="steak" />
              </div></td>
              <td><div align="center">
                <input name="mainsp" type="radio" value="steakp" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style28"></span></td>
              <td><div align="center"></div></td>
              <td><div align="center"></div></td>
            </tr>
            <tr>
              <td><span class="style30">Puddings</span></td>
              <td><div align="center"></div></td>
              <td><div align="center"></div></td>
            </tr>
            <tr>
              <td><span class="style27">Christmas pudding with brandy sauce </span></td>
              <td><div align="center">
                <input name="pudding" type="radio" value="xmas" />
              </div></td>
              <td><div align="center">
                <input name="puddingp" type="radio" value="xmasp" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style27">Selection of ice creams in a tuille basket </span></td>
              <td><div align="center">
                <input name="pudding" type="radio" value="icecreams" />
              </div></td>
              <td><div align="center">
                <input name="puddingp" type="radio" value="icecreamsp" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style27">Chocolate marquise with Drambuie</span></td>
              <td><div align="center">
                <input name="pudding" type="radio" value="chocmarquise" />
              </div></td>
              <td><div align="center">
                <input name="puddingp" type="radio" value="chocmarquisep" />
              </div></td>
            </tr>
            <tr>
              <td><span class="style27">English cheese board, crackers </span></td>
              <td><div align="center">
                <input name="pudding" type="radio" value="cheeseboard" />
              </div></td>
              <td><div align="center">
                <input name="puddingp" type="radio" value="cheeseboardp" />
              </div></td>
            </tr>
          </table>
	</form>
        <p align="left" class="style22"> </p>
        </tr>
      <tr>
        <td valign="top">      
      </tr>
    </table>      </td>
  </tr>
  <tr>
    <td height="2" align="center" bgcolor="#388900"><img src="/beta/space.gif" width="2" height="2" /></td>
  </tr>
  <tr>
    <td height="40" align="center"><font color="#FFFFFF" size="1" face="Tahoma">Copyright © 2004-<?php echo date("Y")?> Ignition Driving School. All Rights Reserved.</font></td>
  </tr>
</table>
</body>
</html>

 

The username is stored via the control panel using webprotect. Sorry, I haven't got any more information than that.

Link to comment
Share on other sites

Using ProjectFear's code you can also store it in cookie:

 

$query = mysql_query("SELECT * FROM users") or die(mysql_error());

echo <<<html
<table border="0">
<tr>
<td><b>Username:</b></td>
</tr>
html;

while($r = mysql_fetch_assoc($query)){
echo <<<html

setcookie('user', $r['username'], time() + (seconds # here), "/", websiteurl);

$username = $_COOKIE['user'];

<tr>
<td>{$username}</td>
</tr>
html;
}

echo "</table>";

 

By using a cookie you can use $_COOKIE['user'] on any page you like without having to set the username over and over again. I hope this helps.

Link to comment
Share on other sites

The code youve provided isn't relevent.

 

When you log your users in you need to store there username is the $_SESSION array. From there, you can easily retrieve it anywhere in your application.

 

Exactly what I was going to suggest.

 

This is how I do it. Occasionally I'll output a lot of user info (email, first name, location etc.) into the session variable to save me making multiple calls to the database and so they are available anywhere in the application.

Link to comment
Share on other sites

I would start a session, when a user has successfully been verified start a session and stick the user name in like this

 

{
	session_start();
	session_register('username'); 
	header('location:login_success.php'); 
} 

 

And then you can pass it from page to another by putting this above your head tag

 


<?
session_start();

if(!session_is_registered(username))
{
	header("location:notloggedin.html"); 
}

?> 

 

And to get the username put this in your body

 

$name = $_SESSION['username'];

 

Hope it helps

 

 

Link to comment
Share on other sites

Hi Mark

 

That's great, but for some reason it's not returning the $name. The code I have is:

 

<?
session_start();

if(!session_is_registered(username))
{
	echo "not logged in"; 
}

?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<? $name = $_SESSION['username'];
echo $name; ?>
</body>
</html>

It has the username in there because it hasn't come back with the error.

 

I'm sure it's just the final step I'm missing now.

 

Thanks

Mark

Link to comment
Share on other sites

Have you registered the session? Have your input form pass to a page similar to this

 

<?
$dbhost = "localhost";
$dbuser = "root";
$dbname = "your_db";

mysql_connect("$dbhost", "$dbuser")or die("Could not connect to database");

mysql_select_db("$dbname") or die("Could not select db");

//get variables from login form
$username = $_POST['username'];
$password = $_POST['password'];

//access table to check
$sql = "SELECT * FROM login WHERE login = '$username' and password = '$password'";


$result = mysql_query($sql);

$count = mysql_num_rows($result);

//make sure that theres only one matching username and passworrd, and start, register sessioin s

if ($count == 1) 
{
	session_start();
	session_register('username'); 
	session_register('password'); 
	header('location:login_success.php'); 
} 

else

//else kick out an error
{ 
	echo "Wrong Username or Password"; 
} 			

?>



<head>
<title>Login Failure</title>
</head>

<body>
Try again.
</body>
</html>

 

As you can see the header rediects to login_success.php (the coding that you posted before) assuming that they entered the correct login details. If not they get the message "Wrong Username or Password Try again" just change the db details and should work.

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.