Page 1 of 1

Need to Username after successfull login

Posted: Sat Dec 11, 2010 10:10 pm
by jagannathg
Hello All,

I have a login page which validates the username and password from the Database (MySQL) and allows the user to access next page. The code works fine and it validtaes the user's username and password. Now I need to display that user's username after he successfuly logins. Tried to google a lot but not getting the concept.

My Login Page PHP Code is below (main_login.php):

Code: Select all

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
My checklogin.php code is as below:

Code: Select all

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="rajeshwari"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
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");
}
else {
echo "Wrong Username or Password. Check your username or password and try again";

}
?>
<br />
<a href='main_login.php'>Please login again</a>
And here in this page the user's username needs to be displayed (login_success.php) below is the code:

Code: Select all

<? 
session_start();
if(!session_is_registered(myusername)){
header("location:login_success.php");
}
?>
<html>
<body>
Your Login is Successful!!<br /> <br />
<a href='logout.php'>Logout Account</a>
</body>
</html>
any one who can help me with this.

Regards,
Jagannath

Re: Need to Username after successfull login

Posted: Sun Dec 12, 2010 1:24 am
by jagannathg
Requesting all to help me with this!!

Re: Need to Username after successfull login

Posted: Sun Dec 12, 2010 3:07 am
by social_experiment
When the user has been authorised set a $_SESSION variable that contains the username.

Code: Select all

<?php
 // login successful
 $_SESSION['username'] = $username;
 session_regenerate_id();
 // 
?>
$_SESSION['username'] now contains a value that you can use to achieve your goal.

Re: Need to Username after successfull login

Posted: Sun Dec 12, 2010 9:48 pm
by jagannathg
social_experiment wrote:When the user has been authorised set a $_SESSION variable that contains the username.

Code: Select all

<?php
 // login successful
 $_SESSION['username'] = $username;
 session_regenerate_id();
 // 
?>
$_SESSION['username'] now contains a value that you can use to achieve your goal.
Thank you social_experiment...

Yes you are right we have to use the Session Variable to achieve that. I had been seeing few codes googling for the same and found a very nice video tutorials on PHP.

http://www.youtube.com/watch?v=4oSCuEtxRK8

This help me a lot!!! Nice tutorial for beginners like me.

Thanks again for your help!!!

Regards,
Jagannath