Current location: Hot Scripts Forums » Programming Languages » PHP » How to show username?


How to show username?

Reply
  #1 (permalink)  
Old 08-04-07, 04:40 PM
kickassmedia kickassmedia is offline
Newbie Coder
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
How to show username?

Hello people, I am new to php and i need to know how to show the current logged in username anywhere on my restricted page. My login.php page processes it self and the page that checks the login is called functions.php. The restricted page includes the following code on the top.
PHP Code:

<?php
    
include "functions.php";
    
phpAuth_chk_login();
?>
The functions.php page has the following code.
PHP Code:

<?
function phpAuth_chk_login()
{

                include 
"config.php";

    
session_start();
    if (!isset(
$_SESSION["phpAuth_username"])) {
        echo 
"You are currently not logged in. Please go to the <a href=".$register_redirect.">login page</a>.";
        exit;
    }
}
?>
and the login has the following code.
PHP Code:

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

<body>
<?
if (isset($_POST["phpAuth_Submit"])) {

        include 
"config.php";
        
$filename $database_name;
        
    if (!
file_exists($filename)) {
            
            echo 
"No database exists. Please check your config file.";
            
    } else {
        
        
$handler fopen($filename,'r');
        
$members file_get_contents($filename);
        
// get username and password
        
$users_array explode ("\n",$members);
        
        foreach (
$users_array as $users) {
            if (
$users != "" || $users != NULL) {
        
                
// break username and password string into seperate variables.
                
list($username,$password) = explode("\t",$users);
                    
                if (
$_POST["phpAuth_usr"] == trim($username) && md5($_POST["phpAuth_pwd"]) == trim($password)) {    

                    
$logged_in 1;
                    break;
                    
                } else {
                        
                    
$logged_in 0;
                        
                }
                    
            } else {
            
                break;
            
            }
            
        } 
// end foreach
        
        
if ($logged_in != 1) { // IF USER IS NOT LOGGED IN
            
            
echo "Error! Incorrect Details. <a href=\"".$_SERVER['HTTP_REFERER']."\">Go Back</a>";
        
        } else { 
// ELSE LOGGED IN            
                    
            
session_start();
            
$_SESSION["phpAuth_username"] = $_POST["phpAuth_usr"];
            
$_SESSION["phpAuth_password"]  = $_POST["phpAuth_pwd"];
            
                if (
$_POST["phpAuth_rm"] == 1) {
                    
setcookie("phpAuth_username",$_POST["phpAuth_usr"],time()+3600);
                                                                                      
                }
                                        
                                                                 
header('Location: '.$login_redirect.'');        
                               
        }    
    
    } 
// END IF FILE EXISTS
    
} else {    
        
?>
<div id="bv_" style="position:absolute;left:0px;top:0px;width:480px;height:272px;background-image:url(http://kickassmedia.newsit.es/Images/login.png);z-index:1" align="left">
<form name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
  <table width="45%"  border="0" cellpadding="2" cellspacing="0" style="position:absolute;left:137px;top:102px">   
 <tr>
      <td><center><font color="white"><strong>Username</strong></font></center></td>
    </tr>
    <tr>
      <td><center><input name="phpAuth_usr" type="text" id="phpAuth_usr" value="<? if (isset($_COOKIE["phpAuth_username"])) { echo $_COOKIE["phpAuth_username"]; } ?>"></center></td>
    </tr>
    <tr>
      <td><center><font color="white"><strong>Password</strong></font></center></td>
    </tr>
    <tr>
      <td><center><input name="phpAuth_pwd" type="password" id="phpAuth_pwd"></center></td>
    </tr>
    <tr>
      <td><center><font color="white">Not Registered? <a href="register.php">Register Here</a></font></center> </td>
    </tr>
    <tr>
      <td><center><input name="phpAuth_Submit" type="submit" id="phpAuth_Submit" value="Login"></center></td>
    </tr>
  </table>
</div>
</form>
<?
}
?>
</body>
</html>
<?
ob_end_flush
();
?>
Now if you guys need to check all the code to figure out how to do it then feel free to do so.
Now the restricted page is the following...
PHP Code:

<?php
    
include "functions.php";
    
phpAuth_chk_login();
?>
<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>KickAssMedia</title>

<meta name="GENERATOR" content="Created by BlueVoda">

<style type="text/css">

img { behavior: url("pngfix.htc"); }

</style>

</head>

<body bgcolor="#FFFFFF" text="#000000">

<img src="/Images/kickass_Media.gif" id="Image1" alt="" align="top" border="0" width="480" height="272" style="position:absolute;left:0px;top:0px;width:480px;height:272px;z-index:0">

<a href="logout.php"><img src="/Images/logout_btn.png" id="Image2" alt="" align="top" border="0" width="25" height="25" style="position:absolute;left:304px;top:92px;width:25px;height:25px"></a>
<a href="login.php"><img src="/Images/login_btn.png" id="Image3" alt="" align="top" border="0" width="25" height="25" style="position:absolute;left:149px;top:92px;width:25px;height:25px"></a>
<div id="bv_" style="position:absolute;left:300px;top:6px;width:169px;height:16px;z-index:1" align="left">





<font style="font-size:13px" color="white" face="Arial">Welcome, </font></div>





<iframe width="80" scrolling="no" height="35" frameborder="0" style="position: absolute; left: 200px; top: 86px;" src="/Main%20Page/enter.html" name="frame02" id="iplaylist"></iframe>

<img src="/Images/MUSIC.png" id="Image2" alt="" align="top" border="0" width="100" height="35" style="position:absolute;left:16px;top:98px;width:100px;height:35px;z-index:1">

<img src="/Images/VIDEO.png" id="Image3" alt="" align="top" border="0" width="100" height="35" style="position:absolute;left:362px;top:98px;width:100px;height:35px;z-index:2">

<div id="bv_" style="position:absolute;left:47px;top:144px;width:55px;height:45px;z-index:3" align="left">

<script language="JavaScript" type="text/javascript">

<!--

RollOver1 = new Image();

RollOver1.src = "mp3.gif";

//-->

</script>

<a href="/MP3/mp3.html" target="frame01">

<img src="/Images/mp3.png" id="RollOver1" width="55" height="45" alt="" border="0" onMouseOver="this.src='/Images/mp3.gif'" onMouseOut="this.src='/Images/mp3.png'"></a>

</div>

<div id="bv_" style="position:absolute;left:48px;top:200px;width:55px;height:45px;z-index:4" align="left">

<script language="JavaScript" type="text/javascript">

<!--

RollOver2 = new Image();

RollOver2.src = "listen.gif";

//-->

</script>

<a href="/Listen/list.html" target="frame01">

<img src="/Images/listen.png" id="RollOver2" width="55" height="45" alt="" border="0" onMouseOver="this.src='/Images/listen.gif'" onMouseOut="this.src='/Images/listen.png'"></a>

</div>

<div id="bv_" style="position:absolute;left:375px;top:144px;width:55px;height:45px;z-index:5" align="left">

<script language="JavaScript" type="text/javascript">

<!--

RollOver3 = new Image();

RollOver3.src = "shows.gif";

//-->

</script>

<img src="/Images/shows.png" id="RollOver3" width="55" height="45" alt="" border="0" onMouseOver="this.src='/Images/shows.gif'" onMouseOut="this.src='/Images/shows.png'">

</div>

<div id="bv_" style="position:absolute;left:376px;top:200px;width:55px;height:45px;z-index:6" align="left">

<script language="JavaScript" type="text/javascript">

<!--

RollOver4 = new Image();

RollOver4.src = "movie.gif";

//-->

</script>

<img src="/Images/movies.png" id="RollOver4" width="55" height="45" alt="" border="0" onMouseOver="this.src='/Images/movie.gif'" onMouseOut="this.src='/Images/movies.png'">

</div>

<iframe width="240" scrolling="yes" height="120" frameborder="0" style="position: absolute; left: 120px; top: 143px;" src="/Main%20Page/frame01.html" name="frame01" id="iplaylist"></iframe>



<img width="244" height="5" border="0" align="top" style="position: absolute; left: 118px; top: 261px; width: 245px; height: 5px; z-index: 7;" alt="" id="Image2" src="/Images/border-down.png" /> 

<img width="5" height="121" border="0" align="top" style="position: absolute; left: 117px; top: 140px; width: 5px; height: 121px; z-index: 8;" alt="" id="Image5" src="/Images/border-left.png" /> 

<img width="5" height="121" border="0" align="top" style="position: absolute; left: 359px; top: 141px; width: 5px; height: 121px; z-index: 9;" alt="" id="Image7" src="/Images/border-right.png" /> 

<img width="246" height="5" border="0" align="top" style="position: absolute; left: 118px; top: 138px; width: 246px; height: 5px; z-index: 10;" alt="" id="Image8" src="/Images/border-up.png" />

</body>

</html>
Now i want the username to appear right next to the welcome message on line "38".

Thanks in advance!

Last edited by UnrealEd; 08-05-07 at 03:32 AM. Reason: please use the [php] wrapper when posting php code
Reply With Quote
  #2 (permalink)  
Old 08-04-07, 07:53 PM
Gelfdon's Avatar
Gelfdon Gelfdon is offline
New Member
 
Join Date: Mar 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Well i'm relitively new to php aswell but i would recon seein as u already assigned the persons username to the $_SESSION["phpAuth_username"] variable and kept the session open on the welcome page then it should just b a simple matter of,

PHP Code:

Welcome, <?php echo $_SESSION["phpAuth_username"]; ?>

Reply With Quote
  #3 (permalink)  
Old 08-05-07, 07:18 AM
kickassmedia kickassmedia is offline
Newbie Coder
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply. Now I understand what i was doing wrong. I was putting quotes after the echo part. Thanks now it works great. Thanks for the help.
Reply With Quote
  #4 (permalink)  
Old 08-05-07, 07:34 AM
kickassmedia kickassmedia is offline
Newbie Coder
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Now if you could also answer another question. Can you tell how to redirect my login.php to my own error pages instead of just showing a simple message in a blank page, when they enter a wrong username or password?

Thanks in advance!
Reply With Quote
  #5 (permalink)  
Old 08-06-07, 08:20 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
PHP Code:

if($login=="false" || $_SESSION['phpAuth_username']=="" ){ 
/*login == false is a failed login attempt */
header("location: login.php");
} else {
header("location: mainpage.php";

__________________
i am still a learner and i like $this-> smilie!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Username exist in database zoliky PHP 2 10-23-06 03:32 AM
Would like some help understanding this php part. clantron PHP 12 03-12-06 08:24 PM
Extracting username with ldap and htaccess file silentsnake Script Requests 2 08-04-05 10:08 PM
looking for a shoping cart has a sign up form with username and password they choose. kicon Script Requests 1 11-03-04 11:15 AM
Quick Question for you php guru's Tokahashi PHP 3 04-09-04 12:00 PM


All times are GMT -5. The time now is 06:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.