View Full Version : Display username after login from login page in other pages

08-05-2013, 12:55 PM
i want to display the username in other pages when i logged in to the login page & its redirect into other page. The redirected page should display the username.

In the login page it compare the username & password from the database, if it mach then only it redirect into other pages. i did all the necessary validation part to login page.
I am new to PHP. Please help me .

Please find the code below
index.php(Containing Login details)

Logic Part




if($myusername == "" )
$error= "Error : You did not enter a username.";
$code= "1" ;

elseif($mypassword == "" )
$error= "Error : You did not enter a password.";
$code= "2" ;
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="transport"; // Database name
$tbl_name="members"; // Table name

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

// username and password sent from form

// To protect MySQL injection (more detail about 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'";

// Mysql_num_row is counting table row
// If result matched $myusername and $mypassword, table row must be 1 row
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$error= "Wrong Username or Password.";

Body Part

<div class="row3" align="center">
<form name= "info" id= "info" method= "post" action= "" >
<table width= "327" border= "0" align="center" cellpadding= "5" cellspacing= "1" class="table">
<td align="right">UserName: </td>
<td><input name= "myusername" type= "text" id= "myusername" placeholder="Username" value="<?php if(isset($myusername)){echo $myusername;} ?>"<?php if(isset($code) && $code == 4){echo "class=error" ;}?> ></td>

<td align="right">Password: </td>
<td><input name= "mypassword" type= "password" id= "mypassword" placeholder=" Password" value="<?php if(isset($mypassword)){echo $mypassword;} ?>"<?php if(isset($code) && $code == 5){echo "class=error" ;}?> ></td>
<td><input type= "submit" name= "Submit" value= "Submit" /></td>
<div><table align="center">
<?php if (isset($error)) { ?>
<td colspan="2" align="center" ><?php echo "<p class='message'>" .$error. "</p>" ; ?></td>
<?php } ?>

I want to display the username in the other page. Please give me code to disply.
I know that it will wil be done through using session.
Please help me..

08-06-2013, 01:54 AM
$myusername=$_SESSION; theres you have the clue. if the new pages has session_start(); at the top of the page you can access the session name

08-06-2013, 06:19 AM
Thank you for spending some time for me.
if i login with different username name, but it display username only Array.
dont no where i am going wrong.

08-06-2013, 07:59 AM
yes Sessions is an array. if you want to see the sessions set
Print_r ($_SESSION);

08-12-2013, 11:58 AM
<form method="post" action="">

<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="search" />

$username= $_REQUEST['username'];
$query = "select * from user where user='"$username" ";
$res1 = mysql_query($query);
while($rows = mysql_fetch_array($res1))

<?PHP echo $username; ?>

if is it incorrect please reply me correct one.
Thank you happy to help you.

08-12-2013, 04:31 PM
jc920060, one more time posting a link signature underneath your posts and you're out. Read the forum rules!