How to do this login and fail message function?
Posted: Thu Oct 09, 2008 5:29 pm
Hi
Can someone give me a hand on my coding?
Tasks: I have 2 login pages. main.php is the input window, then it post to check.php which checks whether login is successful and it is invisible to users! If login is ok, then redirects user to further page otherwise I want to redirect user to the main.php. But then I want to print a message "Login fails on main.php". How can I do this?
Here are some code:
Can someone give me a hand on my coding?
Tasks: I have 2 login pages. main.php is the input window, then it post to check.php which checks whether login is successful and it is invisible to users! If login is ok, then redirects user to further page otherwise I want to redirect user to the main.php. But then I want to print a message "Login fails on main.php". How can I do this?
Here are some code:
Code: Select all
<?php
include '../conn.php';
ob_start();
// Connect to server and select databse.
$connect = mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect");
// Define $myusername and $mypassword
$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 customer WHERE username='$myusername' and password='$mypassword'";
$result=mysql_db_query($dbname, $sql, $connect);
// 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){
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id = $row[id];
}
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("id");
session_register("myusername");
session_register("mypassword");
header("location:../table.php");
}
else {
header("location:../main.php");
}
ob_end_flush();
?>