Page 1 of 1

Display Username After Logged In

Posted: Sat May 23, 2009 12:59 pm
by icecoldx
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>Student Login </strong></td>
</tr>
<tr>
<td width="78">Student ID</td>
<td width="6">:</td>
<td width="294"><input name="myid" type="text" id="myid"></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>
 
checklogin.php

Code: Select all

<?php
$host="localhost"; 
$username="root"; 
$password=""; 
$db_name="assignment_db1"; 
$tbl_name="student"; 
 
mysql_connect("$host", "$username", "$password")or die("Can't Connect");
mysql_select_db("$db_name")or die("Can't Select DataBase");
 
$myusername=$_POST['myid'];
$mypassword=$_POST['mypassword'];
 
 
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
 
$sql="SELECT * FROM $tbl_name WHERE Id='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);
 
$count=mysql_num_rows($result);
 
if($count==1){
session_register("myid");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Student ID or Password";
}
?>
 
login_success.php

Code: Select all

<?session_start();
if(!session_is_registered("myid")){
header("location:main_login.php");
}
?>
 
<html>
<body>
Login Successful<br>
<? echo "Welcome" ?????????? ?>   <==how do i display the user id here??
<p>
<a href="logout.php">Log Out!</a></p>
 
</body>
</html>
Thanks in advance =]

Re: Display Username After Logged In

Posted: Sat May 23, 2009 1:22 pm
by jayshields
When a user logs in, set a session variable which contains the users' ID and then query the database to fetch the username for that ID. An easier solution would be to save the username in the session variable instead of the ID, then just print that.

Re: Display Username After Logged In

Posted: Sun May 24, 2009 1:40 am
by icecoldx
could you tell me how to do so ? i just started php and sql ...
something to do with $_session ?

Re: Display Username After Logged In

Posted: Sun May 24, 2009 5:17 am
by jayshields
icecoldx wrote:could you tell me how to do so ? i just started php and sql ...
something to do with $_session ?
Yep. Use the $_SESSION array. Read the manual.