Displaying Login Error in the login page

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
xiaomahe
Forum Newbie
Posts: 11
Joined: Sun Jun 28, 2009 10:14 am

Displaying Login Error in the login page

Post by xiaomahe »

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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Displaying Login Error in the login page

Post by McInfo »

When posting code in the forum, place it between BBCode tags.

Code: Select all

[php][/php] or [code=php]
[/code]
PHP Manual wrote:session_register() -- This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.
This

Code: Select all

$email = $_POST['email'];
$Upass = $_POST['Upass'];
$email = stripslashes($email);
$Upass = stripslashes($Upass);
$email = mysql_real_escape_string($email);
$Upass = mysql_real_escape_string($Upass);
can be condensed into

Code: Select all

$email = mysql_real_escape_string(stripslashes($_POST['email']));
$Upass = mysql_real_escape_string(stripslashes($_POST['Upass']));
which is better because $email and $Upass are never contaminated.

This

Code: Select all

$sql = sprintf("SELECT * FROM $tbl_name WHERE email='$email' and Upass='$Upass'");
should be

Code: Select all

$sql = sprintf("SELECT * FROM %s WHERE email='%s' and Upass='%s'", $tbl_name, $email, $Upass);
or

Code: Select all

$sql = "SELECT * FROM $tbl_name WHERE email='$email' and Upass='$Upass'";
The only reasons to do this

Code: Select all

$y = "$x";
are
  • you suspect that $x might be a number and you want $y to be a string.
  • you are going to add more to the string, as in these examples:

Code: Select all

$y = "$x\n";
$y = "<p>$x</p>";
Otherwise,

Code: Select all

$y = $x;
some_function($x);
xiaomahe wrote:how to display the error message in the index.php instead off appearing in check_login.php
Redesign the relationship of your pages so that check_login.php is included in index.php. Store the error message in a variable and echo it on index.php.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:59 pm, edited 1 time in total.
xiaomahe
Forum Newbie
Posts: 11
Joined: Sun Jun 28, 2009 10:14 am

Re: Displaying Login Error in the login page

Post by xiaomahe »

hai, thnks for replying.. i forget to check this post, because i am so busy doing this project..
i am still learning PHP and thanks for the answer as well as the tips..
gimpact
Forum Commoner
Posts: 65
Joined: Tue Jun 16, 2009 11:08 pm

Re: Displaying Login Error in the login page

Post by gimpact »

Hi,
You can also do this without session and other things, follow this

login.html

Code: Select all

 
<?php
$urlValue = $_GET['code'];
if($urlValue = "password"){
  $display = "Password wrong";
}else if ($urlValue = "username"){
$display = "User name invalid";
}else{
$display = "";
}
?>
 
The place in html where you want to display the error message

Code: Select all

<?php $display ?>
in your login_check.php

Code: Select all

 
//When the userid is wrong
header("Location:http://www.x.com/index.html?code=username");
 
// When the password is wrong
header("Location:http://www.x.com/index.html?code=password");
 
Hope this helps

Warning: I am new to php, cross check my code!
Post Reply