/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] how to display username after logged in? Help

Ok i know some of yous will know what i mean, I want to know how to display the username of the logged in user EXAMPLE (Welcome, _______)

Index.php

[QUOTE]

<title>Pulse-Box</title>
<link rel=”stylesheet” href=”assets/css/style.css” type=”text/css” media=”all” />
</head>
<body>

<div id=”wrapper”>

<?php include (‘header.php’); ?>

<div id=”nav”></div>

<?php include (‘leftbar.php’); ?>

<div id=”right”>
<h1><font size=”2″>News & Updates</font></h1>
<p>
Well hello and welcome, Today i have fianled installed and fixed all errors on my login script, if you spot any errors
please report them to me <b>Via</b> <a href=”contact.php”>Contact</a> page.
</p>
<span class=”postinfo”> Posted by Admin on 22:20 02/07/2009</span>

</div>

</div>

<?php include (‘footer.php’); ?>

</body>

[/QUOTE]

Register.php

[QUOTE]

<? include(“includes/connect.php”);?>

<title>Pulse-Box Tester</title>
<link rel=”stylesheet” href=”assets/css/style.css” type=”text/css” media=”all” />
</head>
<body>

<div id=”wrapper”>

<?php include (‘header.php’); ?>

<div id=”nav”></div>

<?php include (‘leftbar.php’); ?>

<div id=”right”>

<div id=”right”>

<?PHP session_start();?>
<? include(“includes/connect.php”);?>

<? if($_SESSION[‘logged_in’] == ‘Yes’) { ?>

<script type=”text/JavaScript”>window.location=”index.php”;</script>

<? } else { ?>
<?//#########################################################//?>

<? if($_POST[‘submit’]) { ?>

<?
$fullname=$_POST[‘fullname’];
$email=$_
POST[’email’];
$username=$_POST[‘username’];
$password=$_
POST[‘password’];
?>

<? if(!$fullname) { ?>
Please enter your fullname.
<? } else if(!$email) { ?>
Please enter an email address.
<? } else if(!$username) { ?>
Please enter a username.
<? } else if(!$password) { ?>
Please enter a password.
<? } else { ?>

<?
$new = “INSERT INTO `members` (`ip`, `fullname`, `email`, `username`, `password`)
VALUES (‘127.0.0.1’, ‘$fullname’, ‘$email’, ‘$username’, ‘$password’)”;
mysql_query($new) or die(mysql_error());
?>

<div id=”right”>
<h1><font size=”2″>Registration Success</font></h1>
<p>
Thank you for registering with us, your accouunt has sucessfully been created, </ br>
Please click <a href=”index.php”>here</a> to be returned to home page.

</div>

</div>

<? } ?>

<? } else { ?>

<h1><font size=”2″>Register your free user account!</font></h1>

<table cellpadding=”0″ cellspacing=”0″ border=”0″>
<p>
<tr>
<td><p><font size=”2pt”>Username</font></p></td>
<td> <input type=”text” name=”username” id=”username” value=””> </td>
</tr>

<tr>
<td><p><font size=”2pt”>Password</font></p></td>
<td> <input type=”password” name=”password” id=”password” value=””> </td>
</tr>

<tr>
<td><p><font size=”2pt”>Full Name</font></p></td>
<td> <input type=”text” name=”fullname” id=”fullname” value=””> </td>
</tr>

<tr>
<td><p><font size=”2pt”>Email</font></p></td>
<td> <input type=”text” name=”email” id=”email” value=””> </td>
</tr>

<tr><td colspan=””> <br><input type=”submit” name=”submit” id=”submit” value=”Register!”>
</table>
</form>
<br>
<b><i>*All fields above have to be filled in otherwise you will not be able to sign up!*</b></i>

<? } ?>

<?//#########################################################//?>
<? } ?>

</div>

</div>

<?php include (‘footer.php’); ?>

</body>

[/QUOTE]

Please help me correct my scipt if its rong, and help me getting username displayed

Regards Robert ?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@HHCOwnerJul 03.2009 — You'll need to have your login pages. (I didn't notice any in your post)

Make sure you have a [B]login.html[/B] and a [B]login.php[/B].

Your login.html will be the login form and login.php will be the script that checks the username and password. This is what I use for login.php ...

[code=php]
<?

$dbhost = "localhost";
$dbname = "db";
$dbuser = "user";
$dbpass = "pass";

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$user = $_POST['user'];
$pass = $_POST['pass'];

$sql="SELECT * FROM users WHERE user='$user' and pass='$pass'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
header("Location: http://mysite.com/membersarea.php?user=$user"); }
else
{ header("Location: http://mysite.com/loginerror.php");
}
?>
[/code]


You put this at the top of your members only page ...

[code=php]
<?
$user = $_GET['user'];
session_start();
$_SESSION['user'] = $user;

if(isset($_SESSION['user'])){
} else {
echo "
<script language='javascript'>
alert('Sorry, but you must login to view the members area!')
</script>
<script>
window.location='http://mysite.com/login.html'
</script>
"; }
?>
[/code]


And finally, to actually display the username put this code where you want it to be displayed ...

[code=php]
<? echo "Welcome, $user"; ?>
[/code]


Now for an explanation ...

This part connects to your database:
[code=php]
$dbhost = "localhost";
$dbname = "db";
$dbuser = "user";
$dbpass = "pass";

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
[/code]


This part checks to see if the password matches with the username:
[code=php]
$sql="SELECT * FROM users WHERE user='$user' and pass='$pass'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
[/code]


This part basically says that if the username and password match, then place the username as a url variable otherwise redirect to loginerror.php:
[code=php]
if($count==1){
session_start();
$_SESSION['users'] = $user;
header("Location: http://mysite.com/membersarea.php?user=$user"); }
else
{ header("Location: http://mysite.com/loginerror.php");
}
[/code]


This part takes the information out of your url variable user and puts it as the session called user:
[code=php]
$user = $_GET['user'];
session_start();
$_SESSION['user'] = $user;
[/code]


This part says that if $SESSION['user'] is set, then continue with the rest of the website but if its not set, then send an alert saying you must be logged in and then redirects you to the login page:
[code=php]
if(isset($_SESSION['user'])){
} else {
echo "
<script language='javascript'>
alert('Sorry, but you must login to view the members area!')
</script>
<script>
window.location='http://mysite.com/login.html'
</script>
"; }
[/code]


The reason we use the session is to determine whether the person is logged in or not and if they aren't then redirect them to the login page.

Hope this helps. ?
Copy linkTweet thisAlerts:
@Robert_ElsdonauthorJul 03.2009 — here is the index.php, register.php, login.php and connect.php

[B]connect.php[/B]
<?

$host = "localhost";

$user = "user";

$pass = "pass";

$name = "dbname";

$ms = mysql_pconnect($host, $user, $pass);

if ( !$ms )

{

echo "Error connecting to database.n";

}

mysql_select_db($db);

?>[/QUOTE]


[B]login.php

[/B]

<?PHP session_start();?>

<? include("includes/connect.php");?>

<? if($_SESSION['logged_in'] == 'Yes') { ?>

<script type="text/JavaScript">window.location="index.php";</script>

<? } else { ?>

<?//#########################################################//?>

<? if($_POST['submit']) { ?>

<?

// Get Form Data

$username=$_POST['username'];

$password=$_
POST['password'];

?>

<?

// Check to see if the data is correct

$getuserinfo = "SELECT * FROM members where username='$username' and password='$password'";

$getuserinfo1 = mysql_query($getuserinfo) or die("No Info");

$user = mysql_fetch_array($getuserinfo1);

?>

<? if(!$username) { ?>

Please enter your username.

<? } else if(!$password) { ?>

Please enter your password.

<? } else if(!$user) { ?>

Username/Password is incorrect.

<? } else { ?>

<?PHP

session_start();

$_SESSION['username']=$username;

$_
SESSION['logged_in']='Yes';

?>

<script type="text/JavaScript">window.location="index.php";</script>

<? } ?>

<? } else { ?>

<form method="POST" action="login.php">

<table cellpadding="0" cellspacing="1" border="0">

<tr><td> Member Login </td></tr>

<tr>

<td> Username </td>

<td> <input type="text" name="username" id="username" value=""> </td>

</tr>

<tr>

<td> Password </td>

<td> <input type="password" name="password" id="password" value=""> </td>

</tr>

<tr><td colspan="2"> <input type="submit" name="submit" id="submit" value="Login!">

</table>

</form>

<? } ?>

<?//#########################################################//?>

<? } ?>[/QUOTE]


[B]register.php[/B]

<? include("includes/connect.php");?>

<title>Pulse-Box Tester</title>

<link rel="stylesheet" href="assets/css/style.css" type="text/css" media="all" />

</head>

<body>

<div id="wrapper">

<?php include ('header.php'); ?>

<div id="nav"></div>

<?php include ('leftbar.php'); ?>


<div id="right">










<div id="right">


<?PHP session_start();?>

<? include("includes/connect.php");?>

<? if($_SESSION['logged_in'] == 'Yes') { ?>

<script type="text/JavaScript">window.location="index.php";</script>

<? } else { ?>

<?//#########################################################//?>

<? if($_POST['submit']) { ?>

<?

$fullname=$_POST['fullname'];

$email=$_
POST['email'];

$username=$_POST['username'];

$password=$_
POST['password'];

?>

<? if(!$fullname) { ?>

Please enter your fullname.

<? } else if(!$email) { ?>

Please enter an email address.

<? } else if(!$username) { ?>

Please enter a username.

<? } else if(!$password) { ?>

Please enter a password.

<? } else { ?>




<?

$new = "INSERT INTO members (ip, fullname, email, username, password)

VALUES ('127.0.0.1', '$fullname', '$email', '$username', '$password')";

mysql_query($new) or die(mysql_error());

?>


<div id="right">

<h1><font size="2">Registration Success</font></h1>

<p>

Thank you for registering with us, your accouunt has sucessfully been created, </ br>

Please click <a href="index.php">here</a> to be returned to home page.

</div>
</div>

<? } ?>

<? } else { ?>


<h1><font size="2">Register your free user account!</font></h1>

<table cellpadding="0" cellspacing="0" border="0">

<p>

<tr>

<td><p><font size="2pt">Username</font></p></td>

<td> <input type="text" name="username" id="username" value=""> </td>

</tr>

<tr>

<td><p><font size="2pt">Password</font></p></td>

<td> <input type="password" name="password" id="password" value=""> </td>

</tr>

<tr>

<td><p><font size="2pt">Full Name</font></p></td>

<td> <input type="text" name="fullname" id="fullname" value=""> </td>

</tr>

<tr>

<td><p><font size="2pt">Email</font></p></td>

<td> <input type="text" name="email" id="email" value=""> </td>

</tr>

<tr><td colspan=""> <br><input type="submit" name="submit" id="submit" value="Register!">

</table>

</form>

<br>

<b><i>*All fields above have to be filled in otherwise you will not be able to sign up!*</b></i>

<? } ?>

<?//#########################################################//?>

<? } ?>


</div>














</div>


<?php include ('footer.php'); ?>

</body>
[/QUOTE]


[B]index.php[/B]


<title>Pulse-Box</title>

<link rel="stylesheet" href="assets/css/style.css" type="text/css" media="all" />

</head>

<body>

<div id="wrapper">

<?php include ('header.php'); ?>

<div id="nav"></div>

<?php include ('leftbar.php'); ?>


<div id="right">

<h1><font size="2">News & Updates</font></h1>

<p>

Well hello and welcome, Today i have fianled installed and fixed all errors on my login script, if you spot any errors

please report them to me <b>Via</b> <a href="contact.php">Contact</a> page.

</p>

<span class="postinfo"> Posted by Admin on 22:20 02/07/2009</span>

</div>
</div>


<?php include ('footer.php'); ?>

</body>


[/QUOTE]


Please note im very new to php and just dont get some of this ?, please help me thanks alot!
Copy linkTweet thisAlerts:
@HHCOwnerJul 03.2009 — Well It looks like your login.php already set the session username, so all you have to do is place ...

[code=php]
<? echo "Welcome, {$_SESSION['logged_in']}"; ?>
[/code]


Where ever you want it to welcome them.
Copy linkTweet thisAlerts:
@aj_nscJul 03.2009 — The above poster meant to say

[code=php]
<? echo "Welcome, {$_SESSION['username']}"; ?>
[/code]
Copy linkTweet thisAlerts:
@HHCOwnerJul 03.2009 — Sorry, I meant to put "username" not "logged_in" :p
Copy linkTweet thisAlerts:
@Robert_ElsdonauthorJul 03.2009 — thanks aj_nsc ? You solved it! ? Thanks a million ?
Copy linkTweet thisAlerts:
@rishi25ilsFeb 29.2012 — K. But, if I want to create somewhere on the web and display the username somewhere on the page without disturbing the content that is already on the page?

And how can a user delete his account and make his username accesible to others. How to make any content on a different page not visible to the user who are not logged in and with a link to login.

And you told us how to show the username and what will be displayed there if the user has not logged in? How to make a link with Login or Register appear if the user hasn't logged in?

I am a novice pragrammer and I started learning PHP basics just today and fell into it. Stuck right here coz my dream is to make something like Google. And I'm the Founder amd CEO of Pupoz, M.S.Rishikesh.

Regards,

Me.
×

Success!

Help @Robert_Elsdon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...