...

View Full Version : Displaying username after login



rpjd
03-16-2011, 01:03 AM
I want to display a users name on a page after a successful login. I've looked at the source for the site after logging in and it displays

Welcome, <a href="member.php?u=49542">rpjd</a>
Can anyone explain the href to me?

oesxyl
03-16-2011, 01:22 AM
I want to display a users name on a page after a successful login. I've looked at the source for the site after logging in and it displays

Welcome, <a href="member.php?u=49542">rpjd</a>
Can anyone explain the href to me?
is a url to User CP of a given user. What do you want to explain? what is in the page? how it work? or what?

best regards

rpjd
03-16-2011, 01:42 AM
How it works would be interesting to know. I thought it would display the username using a session variable, which I can't get working:

<?php $SESSION['Username'] ?>

tangoforce
03-16-2011, 01:46 AM
You need to use echo or print like this:



//echo and print both do the same thing
<? echo $_SESSION['username'];?>
<? print $_SESSION['username'];?>

oesxyl
03-16-2011, 02:11 AM
How it works would be interesting to know. I thought it would display the username using a session variable, which I can't get working:

<?php $SESSION['Username'] ?>
there are two things here:
1. the page where you see that a anchor, is generated using the user id that u and his user name which apear between a tags. the url for href point to the user cp and doesn't do nothing until you click on it
2. the user CP page, the url from href, is displayed only after you click on that link and redirect you to that page

session are used only to check if you are loged in or not and probably for few other things like this.

best regards

rpjd
03-16-2011, 02:43 AM
This is my login script that works, not getting any errors and redirection works.

<?php
global $Con;
$Username = $_POST['Username'];
$Password = $_POST['Password'];
if(empty($Username) || empty($Password))
{
echo "Username and/or Password empty. Fill in both to login";
header('Location: http://localhost/DB/Login.php');
}
else{
include('E:/MySQLCon.php');
$Con = mysql_connect($host, $user, $pass);
if(!$Con)
{
die('There was an error connecting to the server, please try again later' . mysql_error());
}
if(!mysql_select_db("DB", $Con))
{
echo "not connected";
}
$Username = str_replace("'","''",$Username);
$Password = md5($Password);

// Verify that user is in database
$query = "SELECT Pass FROM password WHERE Username = '$Username'";
$user = mysql_query($query, $Con);
if(!$user || (mysql_numrows($user) < 1))
{
return 1; //Indicates username failure
}
// Retrieve password from result
$Pass = mysql_fetch_array($user);

// Validate that password is correct
if($Password == $Pass['Pass'])
{
session_start();
session_register('Username');
header('Location: http://localhost/DB/Home.php');
}
else
{
header('Location: Buyers Login.php');
}
}
?>

This is the script that I want to display the username on successful login, and guest if not.

<?php
if(isset($_REQUEST[session_name()]))
{
echo "<div id='User'>Welcome: " . $_SESSION['Username'] . "</div>";
}
else
{
echo "<div id='Guest'>Welcome: Guest </div>";
}
?>
However its displaying Welcome: Guest
can anyone tell me whats wrong?

oesxyl
03-16-2011, 02:47 AM
not sure if will work:

<?php
if(isset($_SESSION['Username']))
{
echo "<div id='User'>Welcome: " . $_SESSION['Username'] . "</div>";
}
else
{
echo "<div id='Guest'>Welcome: Guest </div>";
}
?>

best regards

rpjd
03-16-2011, 11:20 AM
No luck with this either

<?php
session_start();
if(isset($_SESSION['Username']))
{
echo "<div id='User'>Welcome: " . $_SESSION['Username'] . "</div>";
}
else
{
echo "<div id='Guest'>Welcome: Guest </div>";
}
?>

bullant
03-16-2011, 12:04 PM
if($Password == $Pass['Pass'])
{
session_start();
$_SESSION['Username'] = $Username;
header('Location: http://localhost/DB/Home.php');
} (Your session_start() should be at the top of your script so you can check if the user at least came from the login page. The way your code is now, it is very hackable)

then on each page


<?php
session_start();
if(isset($_SESSION['Username']))
{
echo "<div id='User'>Welcome: " . $_SESSION['Username'] . "</div>";
}
else
{
echo "<div id='Guest'>Welcome: Guest </div>";
}
?>

rpjd
03-16-2011, 12:20 PM
Thanks bullant, problem is neither Username or Guest is displaying. Surely one should?

bullant
03-16-2011, 12:25 PM
yep, one or the other definitely should.

If nether are, then either your IF block is not being reached in your code before the script terminates or redirects elsewhere, or it is being bypassed during the running of your script.

Most probable cause is a logic error in your code somewhere. Can you post your actual php code from the top of the scrip to at least the line after you IF block so we can see what is happening.

rpjd
03-16-2011, 01:35 PM
I've tested the login script and I've got both the username and the 32 character password to print. The redirect works as well. I tested the session

if(isset($_SESSION['Username']))
{
echo "ok";
}
else
{
echo "no";
}
?>
and prints 'ok'. I have a style id 'User' to position the users name on the page. So I tested to see if the positioning works

if(isset($_SESSION['Username']))
{
echo "<div id='User'>Welcome </div>";
}
else
{
echo "no";
}
and it didn't, yet other contents style accordingly. Can't understand why the Welcome won't print let alone position on page. Help!

rpjd
03-16-2011, 02:43 PM
Got it working at last. I didn't realise that I had display none in my css.

<?php
session_start();
if(isset($_SESSION['Username']))
{
echo "<div id='User'>Welcome : " . $_SESSION['Username'] . " </div>";
}
else
{
echo "no";
}
?>
Thanks again.

kbluhm
03-16-2011, 04:00 PM
Got it working at last. I didn't realise that I had display none in my css.

:confused: :eek: :mad:

Always use your browser's `view source` functionality.

Glad to see you finally got it to work, though it's most likely been working all along.

rpjd
04-17-2011, 07:57 PM
A whiI le ago I had my login script and display username working. I was registering the username with session_register() and it was working, but I read it has been deprecated, which has me wondering why? I'm trying to get this working with $_session which I understand to be a global variable. I changed


session_start();
$register_session('Username') = $Username;
to


session_start();
$_SESSION['Username'] = $Username;
in my login script. On the webpages I have


session_start();
if(isset($_SESSION['Username']))
{
echo "<div id='User'>Welcome : " . $_SESSION['Username'] . " </div>";
}
else
{
echo "<div id='Guest'>Welcome : Guest</div>";
}

Now I keep getting Welcome: Guest.
Why if $_session is a global array is it not recognising the session Username?
Or am I doing something wrong here?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum