Jump to content

[SOLVED] login error message


squiblo

Recommended Posts

hi, im quite a noob to php and i need some help with login, what i want is, an error message to appear on the same page as the login page saying something like "Wrong username or password" when someone has made a mistake logging in.

 

 

"session_register("myusername");

session_register("mypassword");

header("location:profile.php");

}

else {

echo "Wrong Username or Password";

}"

 

this script i have directs me to another page while i want the error message to appear on the same page, does anyone have a solution?

Link to comment
Share on other sites

when an incorrect username or password is entered it directs me to a DIFFERENT page showing "Incorrect username or password", but what i want is "Incorrect username or password" to show on the same page next to the "login" button

Link to comment
Share on other sites

<?php

$host="localhost"; // Host name

$username=""; // Mysql username

$password=""; // Mysql password

$db_name=""; // Database name

$tbl_name="members"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// username and password sent from form

$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 $tbl_name WHERE username='$myusername' and password='$mypassword'";

$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 "profile.php"

session_register("myusername");

session_register("mypassword");

header("location:profile.php");

}

else {

echo "Wrong Username or Password";

}

?>

Link to comment
Share on other sites

<form name="form1" method="post" action="checklogin.php">

<input type="text" id="myusername" style="position:absolute;left:651px;top:59px;width:174px;font-family:Arial;font-size:13px;z-index:4" size="29" name="myusername" value="" title="username">

<input type="password" id="mypassword" style="position:absolute;left:651px;top:86px;width:174px;font-family:Arial;font-size:13px;z-index:5" size="29" name="mypassword" value="" tabindex="password" title="password">

<input type="checkbox" id="Checkbox1" name="Checkbox1" value="on" style="position:absolute;left:648px;top:113px;font-family:Arial;font-size:13px;z-index:6">

<input type="submit" id="Button1" name="Login" value="Login" style="position:absolute;left:778px;top:117px;width:50px;height:43px;font-family:Arial;font-size:13px;z-index:8" title="Login">

</form>

Link to comment
Share on other sites

Try this:

 

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['Login'])){
// username and password sent from form 
$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 $tbl_name WHERE username='$myusername' and password='$mypassword'";
$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 "profile.php"
session_register("myusername");
session_register("mypassword"); 
header("location:profile.php");
}
else {
$error= "Wrong Username or Password";
}
}
?>

 

And:

<form name="form1" method="post" action="checklogin.php">
   <input type="text" id="myusername" style="position:absolute;left:651px;top:59px;width:174px;font-family:Arial;font-size:13px;z-index:4" size="29" name="myusername" value="" title="username">
   <input type="password" id="mypassword" style="position:absolute;left:651px;top:86px;width:174px;font-family:Arial;font-size:13px;z-index:5" size="29" name="mypassword" value="" tabindex="password" title="password">
   <input type="checkbox" id="Checkbox1" name="Checkbox1" value="on" style="position:absolute;left:648px;top:113px;font-family:Arial;font-size:13px;z-index:6">
   <input type="submit" id="Button1" name="Login" value="Login" style="position:absolute;left:778px;top:117px;width:50px;height:43px;font-family:Arial;font-size:13px;z-index:8" title="Login">
</form>
<?php echo $error ?>

Link to comment
Share on other sites

it goes to this

// Check if session is not registered , redirect back to main page.

// Put this code in first line of web page.

<?

session_start();

if(!session_is_registered(myusername)){

header("location:index.php");

}

?>

 

<html>

<body>

Login Successful

</body>

</html>

Link to comment
Share on other sites

So it mean, it is working properly for correct username and pass.

As i see from ur code. You are using checklogin.php to check username and pass. After successful login i will go to profile page. Am i correct?

So u need to redirect the user again to index page upon wrong input.

Just put header in your else statement for index page and see, what it doing...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.