954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

undefined index username and password

hi...
this is my index page...

include("config.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from form

$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);


$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);

$sql="SELECT id FROM admin WHERE username='$myusername' and passcode='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];

$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
session_register("myusername");
session_register("mypassword");


$_SESSION['login_user']=$myusername;

header("location: welcome.php");
}
else
{
$error="Your Login Name or Password is invalid";
}
}
?>


im getting the error as
undefined index username and password at the lines 10 and 11

please help me to solve this error

danielbala
Light Poster
40 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

first off, you cannot put session_start() at the top of a login page, because there is no session yet. I would just take out $_SERVER["REQUEST_METHOD"] == "POST") unless you know that the variables are being posted. Try this because i have a feeling you got this from another website.. Also, you dont need to make a session variable on the login screen because you are already going to register the username. If you want to register more variables, then add them to the to session_register.

session_register("myusername");
session_register("mypassword"); 
// Dont Need
$_SESSION['login_user']=$myusername;
<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // 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 "login_success.php"
session_register("myusername");
session_register("mypassword"); 
session_register("active");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
fobos
Posting Whiz in Training
297 posts since Feb 2009
Reputation Points: 29
Solved Threads: 52
 

You CAN and perhaps should put session_start() at the head of the page.

Do not use session_register:

"This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged."

from the php manual.

session_start();
include("config.php");
if(isset($_POST['username']) && isset($_POST['password']) && !empty($_POST['username']) && !empty($_POST['password'])){
  $myusername=mysql_real_escape_string($_POST['username']); 
  $mypassword=mysql_real_escape_string($_POST['password']); 

  $sql="SELECT id FROM admin WHERE username='$myusername' and passcode='$mypassword'";
  $result=mysql_query($sql);
  if(mysql_num_rows($result)){
    $_SESSION['login_user']=$myusername;
    //what's the purpose of active?? does it do anything?
    header("location: welcome.php");
  }else{
    $error="Your Login Name or Password is invalid";
  }
}
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

first off, you cannot put session_start() at the top of a login page, because there is no session yet. I would just take out $_SERVER["REQUEST_METHOD"] == "POST") unless you know that the variables are being posted. Try this because i have a feeling you got this from another website.. Also, you dont need to make a session variable on the login screen because you are already going to register the username. If you want to register more variables, then add them to the to session_register.

session_register("myusername");
session_register("mypassword"); 
// Dont Need
$_SESSION['login_user']=$myusername;
<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // 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 "login_success.php"
session_register("myusername");
session_register("mypassword"); 
session_register("active");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

sorry guys i tried both the codings still im not getting the answer

can u give suggest me some other login page with codings

danielbala
Light Poster
40 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

What is the problem you are having now? Include code and error, if any.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Are you able to echo username and password after the if($_SERVER["REQUEST_METHOD"] == "POST")?

fobos
Posting Whiz in Training
297 posts since Feb 2009
Reputation Points: 29
Solved Threads: 52
 

ARe you sure that the form field names are username and password. The names are username and passcode in th DB. Just wondering.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 
ARe you sure that the form field names are username and password. The names are username and passcode in th DB. Just wondering.


hi guys.............

i got the output.............

variable names were wrong...

thx......for helping me

danielbala
Light Poster
40 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

mark solved and rep points

fobos
Posting Whiz in Training
297 posts since Feb 2009
Reputation Points: 29
Solved Threads: 52
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You