Page 1 of 1

Undefined index?

Posted: Sun Jun 26, 2011 12:42 pm
by Theramore
Can someone please tell me why i am getting this error:

Notice: Undefined index: username in E:\xampp\htdocs\test\test2.php on line 26

Notice: Undefined index: password in E:\xampp\htdocs\test\test2.php on line 28

Notice: Undefined index: token in E:\xampp\htdocs\test\test2.php on line 31

Code: Select all

<?php

session_start();
error_reporting(E_ALL);

class Login
{

private $_id;
private $_username;
private $_password;
private $_passmd5;

private $_login;
private $_access;
private $_errors;
private $_token;


public function __construct()
{
$this->_login = isset($_POST['login'])? 1 : 0;
$this->_id = 0;
$this->_username = ($this->_login)? $this->filter($_POST['username']) : $_SESSION['username'];
$this->_password = ($this->_login)? $this->filter($_POST['password']) : '';
$this->_passmd5 = ($this->_login)? md5($this->_password) : $_SESSION['password'];
$this->_access = 0;
$this->_errors = array();
$this->_token = $_POST['token'];


}


public function isloggedin()
{

($this->_login)? $this->verifypost() : $this->verifydatabase();

}

public function verifypost()
{

try{

if(!$this->verifydata())
throw new Exception("Invalid data submission!");

if(!$this->verifydatabse())
throw new Exception("Invalid username/password!");

if(!$this->istokenvalid())
throw new Exception("Invalid form submission!");

$this->_access = 1;
$this->_registervalues();
}

catch(Exception $e)
{

$this->_errors[] = $e->getMessage();

}

}

public function istokenvalid()
{

return(!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 1 : 0;

}

public function verifydata()
{

return (preg_match('/[^a-zA-Z0-9]{5,12}$/',$this->_username) && preg_match('/[^a-zA-Z0-9]{5,12}$/',$this->_password))? 1 : 0;

}


public function filter($var)
{

return preg_replace('/[^a-zA-Z0-9]/','',$var);

}


public function verifydatabase()
{

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$data = mysql_query("SELECT * FROM users WHERE username = '{$this->_username}' AND password = '{$this->_passmd5}' ");


if(mysql_num_rows($data))
{

list($this->_id) = @array_values(mysql_fetch_assoc($data));
return true;

}
else
return false;

}

public function registervalues()
{

$_SESSION['username'] = $this->_username;
$_SESSION['ID'] = $this->_id;
$_SESSION['password'] = $this->_passmd5;

}


public function showerrors()
{
echo "<h3>Errors</h3>";
foreach($this->_errors as $key=>$value)
{

echo $value."<br>";

}

}



}

$login = new Login();

if($login->isloggedin())
{

echo $_SESSION['username'];

}
else{
$login->showerrors();
}

$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>


<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='POST'>
<table>
<tr><td>
Username:
</td><td>
<input type='text' name='username'>
</td></tr>
<tr><td>
Password:
</td><td>
<input type='password' name='password'>
<input type='hidden' name='token' value='<?php echo $token; ?>'>
</td></tr>
</table>
<input type='submit' name='login' value='Log In'>
</form>

Re: Undefined index?

Posted: Sun Jun 26, 2011 1:41 pm
by Idri
Undefined Index means that the specified index for, in this case, your array isn't present.

You create a new instance of your Login class, which calls the __construct function.

In the construct function you're trying to get the values of $_POST['username'], $_POST['password'] and $_POST['token]. Notice how you don't get the error when you click your Log In button?
Try debugging the $_POST values using print_r (PHP.net - print_r).
Simply add the following to the top of your __construct function in your Login class.

Code: Select all

print_r($_POST);
It'll output the contents of the $_POST array.

As you may notice, you're trying to access the $_POST data while it doesn't actually exist.
You should use the isset function (PHP.net - isset) to check whether or not the values actually exists.

Code: Select all

if(isset($_POST['value'])){
	echo 'Value is set : '. $_POST['value'];
}else{
	echo 'Value is not set.';
}

Re: Undefined index?

Posted: Sun Jun 26, 2011 2:35 pm
by Theramore
Thank you Idri!

Look how i modified the code for username,pass and token inside the construct() :

Code: Select all

if(isset($_POST['login'])){ $this->_username = $_POST['username']; }
if(isset($_POST['login'])){ $this->_password = $_POST['password']; }
if(isset($_POST['login'])){ $this->_passmd5 = md5($this->_password); }
but something is still wrong in my code even if i insert the correct username/pass i still got errors :banghead:

Re: Undefined index?

Posted: Sun Jun 26, 2011 2:47 pm
by Idri
My crystal ball broke down the other week, still have to get around fixing it. Could you please post the errors you're getting, should you get stuck trying to solve them that is. :)

Also, though the code you posted will work, it would be more efficient do structure it like so;

Code: Select all

if(isset($_POST['login'])){
	$this->_username = $_POST['username'];
	$this->_password = $_POST['password'];
	$this->_passmd5 = md5($this->_password);
}

Re: Undefined index?

Posted: Sun Jun 26, 2011 3:04 pm
by Theramore
oh yeah bassicly you declare an if statement and include all the variables for the login :))much easier:D

The error i get is not a php error is the "Invalid username/password" its generated by the function verifydatabase() (line 95)


Here's the code again :

Code: Select all

<?php



session_start();
error_reporting(E_ALL);

class Login
{

private $_id;
private $_username;
private $_password;
private $_passmd5;

private $_login;
private $_access;
private $_errors;
private $_token;




public function __construct()
{

$this->_login = isset($_POST['login'])? 1 : 0;
$this->_id = 0;
if(isset($_POST['login'])){ $this->_username = $_POST['username']; }
if(isset($_POST['login'])){ $this->_password = $_POST['password']; }
if(isset($_POST['login'])){ $this->_passmd5 = md5($this->_password); }
$this->_access = 0;
$this->_errors = array();
if(isset($_POST['login'])){ $this->_token = $_POST['token']; }


}


public function isloggedin()
{

($this->_login)? $this->verifypost() : $this->verifydatabase();

}

public function verifypost()
{

try{

if(!$this->verifydata())
throw new Exception("Invalid data submission!");

if(!$this->verifydatabase())
throw new Exception("Invalid username/password!");

if(!$this->istokenvalid())
throw new Exception("Invalid form submission!");

$this->_access = 1;
$this->_registervalues();
}

catch(Exception $e)
{

$this->_errors[] = $e->getMessage();

}

}

public function istokenvalid()
{
return(!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
}

public function verifydata()
{

return (preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_username) && preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_password))? 1 : 0;

}


public function filter($var)
{

return preg_replace('/[^a-zA-Z0-9]/','',$var);

}


public function verifydatabase()
{

mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
mysql_select_db("mmwowgc") or die(mysql_error());

$data = mysql_query("SELECT * FROM users WHERE username = '{$this->_username}' ");


if(mysql_num_rows($data))
{

list($this->_id) = @array_values(mysql_fetch_assoc($data));
return true;

}
else
return false;

}

public function registervalues()
{

$_SESSION['username'] = $this->_username;
$_SESSION['ID'] = $this->_id;
$_SESSION['password'] = $this->_passmd5;

}


public function showerrors()
{
echo "<h3>Errors</h3>";
foreach($this->_errors as $key=>$value)
{

echo $value."<br>";

}

}



}

$login = new Login();

if($login->isloggedin())
{

echo $_SESSION['username'];

}
else{
$login->showerrors();
}

$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>


<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='POST'>
<table>
<tr><td>
Username:
</td><td>
<input type='text' name='username'>
</td></tr>
<tr><td>
Password:
</td><td>
<input type='password' name='password'>
<input type='hidden' name='token' value='<?php echo $token; ?>'>
</td></tr>
</table>
<input type='submit' name='login' value='Log In'>
</form>