need help on user login
Posted: Fri Aug 08, 2008 12:45 am
hello everyone, good day.
I have created a script that has a username and password field, like normal login pages appear. But in my database there are 3 fields. The username, pwd, and realname. I have created values to that fields. When the user access the login area and typed admin in the username field and 1234 in the password field. It successfully loads my main.php page, but my problem is that, I want to show in the main.php page WELCOME realName. Could you please help me?
here are the codes for my files.
check.php:
<?php
ob_start();
$host = "localhost"; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name = "admin"; // Database name
$tbl_name = "users"; // Table name
mysql_connect("$host", "root")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
// To protect 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 pwd='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
ob_end_flush();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/login.css" rel="stylesheet" type="text/css"/>
<link rel="shortcut icon" href="images/favicon.ico">
<title>Lourdes C - Login</title>
</head>
<body>
<table width="974" border="0" cellpadding="0" cellspacing="0" align="center">
<!--DWLayoutTable-->
<tr>
<td width="974" height="518" valign="top"><p> </p>
<p> </p>
<p> </p>
<table width="227" border="0" align="center">
<tr>
<td width="221" height="79" colspan="8"><div align="center"><img src="images/lourdesC-logo.png" alt="lourdes C logo" width="221" height="77" /></div></td>
</tr>
<tr class="invalidLogin">
<td height="36" colspan="8"><?php
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "Success.php"
session_register("myusername");
session_register("mypassword");
//session_register("levelName");
header("location:Success.php");
}
else {
echo "<b>ERROR:</b> Invalid Username / Password";
}
?></td>
</tr>
<tr>
<td colspan="8"><form action="check.php" method="post" name="loginForm" target="_self" id="loginForm">
<p><span class="form-label"><img src="images/icons/personal.png" width="16" height="16" /> Username</span><br />
<input name="username" type="text" class="formFields" id="username" tabindex="1"/>
</p>
<p><span class="form-label"><img src="images/icons/password.png" width="16" height="16" /> Password</span><br />
<input name="password" type="password" class="formFields" id="password" tabindex="2"/>
</p>
<p align="right">
<input name="Submit" type="submit" class="form-button" id="button" tabindex="3" value=" Log In " />
</p>
</form> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
here is the Success.php:
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/login.css" rel="stylesheet" type="text/css"/>
<link rel="shortcut icon" href="images/favicon.ico">
<title>Welcome to Lourdes C</title>
</head>
<body>
<table width="974" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="254" height="41" valign="top">
<form method = "POST" action = "logout.php">
<?php
// check session variable is set
$greet = $_SESSION['$myusername'];
// if set, greet by name
echo "Welcome ".$greet;
?><input type = "submit" name="logout" value = "Logout">
</form>
</td>
<td width="720"> </td>
</tr>
<tr>
<td height="959"> </td>
<td> </td>
</tr>
</table>
</body>
</html>
and lastly,
here is the logout.php:
<?php
session_start();
$_SESSION = array();
session_destroy();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/login.css" rel="stylesheet" type="text/css"/>
<link rel="shortcut icon" href="images/favicon.ico">
<title>Lourdes C - Logged Out</title>
</head>
<body>
<table width="974" border="0" cellpadding="0" cellspacing="0" align="center">
<!--DWLayoutTable-->
<tr>
<td width="974" height="518" valign="top"><p> </p>
<p> </p>
<p> </p>
<table width="227" border="0" align="center">
<tr>
<td width="221" height="79" colspan="8"><div align="center"><img src="images/lourdesC-logo.png" alt="lourdes C logo" width="221" height="77" /></div></td>
</tr>
<tr class="loggedOut">
<td height="36" colspan="8"><?php
echo "You are now logged out.";
?></td>
</tr>
<tr>
<td colspan="8"><form action="login.php" method="post" name="loginForm" target="_self" id="loginForm">
<p><span class="form-label"><img src="images/icons/personal.png" width="16" height="16" /> Username</span><br />
<input name="username" type="text" class="formFields" id="username" tabindex="1"/>
</p>
<p><span class="form-label"><img src="images/icons/password.png" width="16" height="16" /> Password</span><br />
<input name="password" type="password" class="formFields" id="password" tabindex="2"/>
</p>
<p align="right">
<input name="Submit" type="submit" class="form-button" id="button" tabindex="3" value=" Log In " />
</p>
</form> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
</body>
</html>
Please help. Really need your help everyone. Is there anything wrong with my code?
I have created a script that has a username and password field, like normal login pages appear. But in my database there are 3 fields. The username, pwd, and realname. I have created values to that fields. When the user access the login area and typed admin in the username field and 1234 in the password field. It successfully loads my main.php page, but my problem is that, I want to show in the main.php page WELCOME realName. Could you please help me?
here are the codes for my files.
check.php:
<?php
ob_start();
$host = "localhost"; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name = "admin"; // Database name
$tbl_name = "users"; // Table name
mysql_connect("$host", "root")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
// To protect 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 pwd='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
ob_end_flush();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/login.css" rel="stylesheet" type="text/css"/>
<link rel="shortcut icon" href="images/favicon.ico">
<title>Lourdes C - Login</title>
</head>
<body>
<table width="974" border="0" cellpadding="0" cellspacing="0" align="center">
<!--DWLayoutTable-->
<tr>
<td width="974" height="518" valign="top"><p> </p>
<p> </p>
<p> </p>
<table width="227" border="0" align="center">
<tr>
<td width="221" height="79" colspan="8"><div align="center"><img src="images/lourdesC-logo.png" alt="lourdes C logo" width="221" height="77" /></div></td>
</tr>
<tr class="invalidLogin">
<td height="36" colspan="8"><?php
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "Success.php"
session_register("myusername");
session_register("mypassword");
//session_register("levelName");
header("location:Success.php");
}
else {
echo "<b>ERROR:</b> Invalid Username / Password";
}
?></td>
</tr>
<tr>
<td colspan="8"><form action="check.php" method="post" name="loginForm" target="_self" id="loginForm">
<p><span class="form-label"><img src="images/icons/personal.png" width="16" height="16" /> Username</span><br />
<input name="username" type="text" class="formFields" id="username" tabindex="1"/>
</p>
<p><span class="form-label"><img src="images/icons/password.png" width="16" height="16" /> Password</span><br />
<input name="password" type="password" class="formFields" id="password" tabindex="2"/>
</p>
<p align="right">
<input name="Submit" type="submit" class="form-button" id="button" tabindex="3" value=" Log In " />
</p>
</form> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
here is the Success.php:
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/login.css" rel="stylesheet" type="text/css"/>
<link rel="shortcut icon" href="images/favicon.ico">
<title>Welcome to Lourdes C</title>
</head>
<body>
<table width="974" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="254" height="41" valign="top">
<form method = "POST" action = "logout.php">
<?php
// check session variable is set
$greet = $_SESSION['$myusername'];
// if set, greet by name
echo "Welcome ".$greet;
?><input type = "submit" name="logout" value = "Logout">
</form>
</td>
<td width="720"> </td>
</tr>
<tr>
<td height="959"> </td>
<td> </td>
</tr>
</table>
</body>
</html>
and lastly,
here is the logout.php:
<?php
session_start();
$_SESSION = array();
session_destroy();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/login.css" rel="stylesheet" type="text/css"/>
<link rel="shortcut icon" href="images/favicon.ico">
<title>Lourdes C - Logged Out</title>
</head>
<body>
<table width="974" border="0" cellpadding="0" cellspacing="0" align="center">
<!--DWLayoutTable-->
<tr>
<td width="974" height="518" valign="top"><p> </p>
<p> </p>
<p> </p>
<table width="227" border="0" align="center">
<tr>
<td width="221" height="79" colspan="8"><div align="center"><img src="images/lourdesC-logo.png" alt="lourdes C logo" width="221" height="77" /></div></td>
</tr>
<tr class="loggedOut">
<td height="36" colspan="8"><?php
echo "You are now logged out.";
?></td>
</tr>
<tr>
<td colspan="8"><form action="login.php" method="post" name="loginForm" target="_self" id="loginForm">
<p><span class="form-label"><img src="images/icons/personal.png" width="16" height="16" /> Username</span><br />
<input name="username" type="text" class="formFields" id="username" tabindex="1"/>
</p>
<p><span class="form-label"><img src="images/icons/password.png" width="16" height="16" /> Password</span><br />
<input name="password" type="password" class="formFields" id="password" tabindex="2"/>
</p>
<p align="right">
<input name="Submit" type="submit" class="form-button" id="button" tabindex="3" value=" Log In " />
</p>
</form> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
</body>
</html>
Please help. Really need your help everyone. Is there anything wrong with my code?