/    Sign up×
Community /Pin to ProfileBookmark

PHP logged in user display

hi, i have run into a slight problem?. i have made a login system using php and i wish for the user (once logged in) to have their user name diplayed in the header of my site on all pages the visit whilst they are logged in. or if they are a guest for it to display ‘guest’. i just cant remember where abouts i have to put this piece of code and what needs to be on each page. could anyone give me a clue as to where it goes and what it needs to be
thanks in advance ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@chrisranjanaMar 29.2012 — Are you setting the username in the PHP session ? If you can show me the code you are using for logging in the user I can give you some hints.
Copy linkTweet thisAlerts:
@fightapilotdeanauthorMar 29.2012 — this is the code that i have on my login page (login.php)

<?php

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("my_db") or die(mysql_error());

if(isset($_COOKIE['ID_my_site']))

{

$username = $_COOKIE['ID_my_site'];

$pass = $_
COOKIE['Key_my_site'];

$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());

while($info = mysql_fetch_array( $check ))

{

if ($pass != $info['password'])

{

}

else

{

header("Location: members.php");

}
}

}

if (isset($_POST['submit'])) {

if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());


$check2 = mysql_num_rows($check);

if ($check2 == 0) {

die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');

}

while($info = mysql_fetch_array( $check ))

{

$_POST['pass'] = stripslashes($_POST['pass']);

$info['password'] = stripslashes($info['password']);

$_POST['pass'] = md5($_POST['pass']);

if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}

else

{

$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;

setcookie(ID_my_site, $_POST['username'], $hour);

setcookie(Key_my_site, $_
POST['pass'], $hour);

//then redirect them to the members area

header("Location: members.php");

}

}

}

else

{

?>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">

<table border="0">

<tr><td colspan=2><h1>Login</h1></td></tr>

<tr><td>Username:</td><td>

<input type="text" name="username" maxlength="40">

</td></tr>

<tr><td>Password:</td><td>

<input type="password" name="pass" maxlength="50">

</td></tr>

<tr><td colspan="2" align="right">

<input type="submit" name="submit" value="Login">

</td></tr>

</table>

</form>

<?php

}

?>

i hope this can be of some use

thanks for any help in advance ?
Copy linkTweet thisAlerts:
@chrisranjanaMar 29.2012 — [CODE]
<?php
if(isset($_COOKIE['ID_my_site']))

{
echo $_COOKIE['ID_my_site'];

}
else
{
echo "Guest";
}

?>

[/CODE]


Try the above snippet
Copy linkTweet thisAlerts:
@fightapilotdeanauthorMar 29.2012 — and this goes at the top of each of my pages or on just this page?

thanks for the help ?
×

Success!

Help @fightapilotdean 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 5.4,
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,
)...