PDA

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


prashanth
08-05-2013, 11:55 AM
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

<?php
session_start();
$myusername=$_SESSION;

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

$myusername=trim($_POST["myusername"]);
$mypassword=trim($_POST["mypassword"]);

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

elseif($mypassword == "" )
{
$error= "Error : You did not enter a password.";
$code= "2" ;
}
else
{
$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
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// 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'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
$_SESSION['myusername'] = $myusername;
}
else
{
$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">
<tr>
<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>
</tr>

<tr>
<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>
</tr>
<tr>
<td>&nbsp;&nbsp;</td>
<td><input type= "submit" name= "Submit" value= "Submit" /></td>
</tr>
</table>
</form>
<div><table align="center">
<?php if (isset($error)) { ?>
<tr>
<td colspan="2" align="center" ><?php echo "<p class='message'>" .$error. "</p>" ; ?></td>
</tr>
<?php } ?>
</table></div>


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..

edbr
08-06-2013, 12: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

prashanth
08-06-2013, 05: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.

edbr
08-06-2013, 06:59 AM
yes Sessions is an array. if you want to see the sessions set
use
<?php
session_start();
Print_r ($_SESSION);
?>

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

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

<?php
include('config.php');
if(isset($_REQUEST['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.

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