Display Username After Logged In

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icecoldx
Forum Newbie
Posts: 2
Joined: Sat May 23, 2009 12:50 pm

Display Username After Logged In

Post 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 =]
Last edited by Benjamin on Tue May 26, 2009 10:19 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Display Username After Logged In

Post 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.
icecoldx
Forum Newbie
Posts: 2
Joined: Sat May 23, 2009 12:50 pm

Re: Display Username After Logged In

Post by icecoldx »

could you tell me how to do so ? i just started php and sql ...
something to do with $_session ?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Display Username After Logged In

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