Displaying Login Error in the login page
Posted: Sun Jun 28, 2009 10:20 am
Hi, i am still new to php..
I had a index.php which has a login form inside. THis form will then send to check_login.php to check if the username exist or not? The problem is, i could not display the login error message in the same page as index.php.
Here is my check_login php code
<?php
session_start();
?>
<?php
$host= "localhost"; // Host name
$username= "root"; // Mysql username
$db_name="mysecureemail"; // Database name
$tbl_name="member"; // Table name
// Connect to server and select databse.
$conn= mysql_connect("$host", "$username")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$email=$_POST['email'];
$Upass=$_POST['Upass'];
// To protect MySQL injection
$email = stripslashes($email);
$Upass = stripslashes($Upass);
$email = mysql_real_escape_string($email);
$Upass = mysql_real_escape_string($Upass);
$sql= sprintf("SELECT * FROM $tbl_name WHERE email='$email' and Upass='$Upass'");
$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("email");
session_register("Upass");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
how to display the error message in the index.php instead off appearing in check_login.php
I had a index.php which has a login form inside. THis form will then send to check_login.php to check if the username exist or not? The problem is, i could not display the login error message in the same page as index.php.
Here is my check_login php code
<?php
session_start();
?>
<?php
$host= "localhost"; // Host name
$username= "root"; // Mysql username
$db_name="mysecureemail"; // Database name
$tbl_name="member"; // Table name
// Connect to server and select databse.
$conn= mysql_connect("$host", "$username")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$email=$_POST['email'];
$Upass=$_POST['Upass'];
// To protect MySQL injection
$email = stripslashes($email);
$Upass = stripslashes($Upass);
$email = mysql_real_escape_string($email);
$Upass = mysql_real_escape_string($Upass);
$sql= sprintf("SELECT * FROM $tbl_name WHERE email='$email' and Upass='$Upass'");
$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("email");
session_register("Upass");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
how to display the error message in the index.php instead off appearing in check_login.php