Trying to print name from login.

So I have some decent history with HTML/CSS but only recently started to look into PHP. I understand the logic behind it, but actually making something happen is still a bit beyond me. I borrowed a nice login script from the internet and it has been working wonderfully for me. The only thing is that I would like to print a person’s name on a page after they have logged in. (i.e. Welcome John Doe!). I have tried a dozen different things and still can’t manage to produce anything of value. I have a database with fields for username, password, and realname.

The script I’m using for login is:

[php]<?php

$host=“placeholder.com”; // Host name
$username=“admin”; // Mysql username
$password=“pw”; // Mysql password
$db_name=“login”; // Database name
$tbl_name=“members”; // Table name

// Connect to server and select databse.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

// username and password sent from form
$myusername=$_POST[‘myusername’];
$mypassword=$_POST[‘mypassword’];

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql=“SELECT * FROM $tbl_name WHERE username=’$myusername’ and password=’$mypassword’”;
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file
session_register(“myusername”);
session_register(“mypassword”);
header(“location:memberarea.php”);
}
else {
echo " ";
}
?>[/php]

And on each page to verify login it starts with:

[php]

<?php session_start(); if(!session_is_registered(myusername)){ header("location:login.html"); } ?>

[/php]

try this

[php]echo "Welcome, " . $_SESSION[‘myusername’];[/php]

Hmm. That is better than what I’ve been able to come up with so far, but it is not quite what I’m looking for. I have a table with usernames as well as real names. I’m trying to print out “Hello John Doe!” instead of “Hello testuser!”

So after a bunch of searching the web, I finally got it to work. Seems I was doing everything right except turning the array into information. The code I ended up with is as follows:

[php]mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);
$sql = “SELECT *
FROM members
WHERE username = '” . $_SESSION[‘myusername’] . “’” ;
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)){
$fullname = $row[‘name’];
}
?>

Welcome <?php echo $fullname ?>!

[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service