开发者

KOhana.. simple function need help

I m new to kohana.. Guyz plz point out the mistakes in the following code.. i m unable to run it.. Its a simple connection to database.. can i compare the query result and the psot item as i have done so.. plz correct..

<?php defined('SYSPATH') or die('No direct script access.');
/**
* Default Ko开发者_JS百科hana controller.
*/
class index_Controller extends Controller {
public function index()
{
$db = new Database();
$index = new View('Index')
$db->connect();
name = $post['name'];
password = $post['password'];
$result = $db->query('name');
foreach($result as $row)
{
  if($row->Password === password)
  {
    echo "login Successful" ;
   }

        }
}

}
?> 


public function index()
{
   $db = new Database();
   $index = new View('Index'); // unused var?
   //$db->connect();
   $name = Arr::get($_POST, 'name');
   $password = Arr::get($_POST, 'password');
   if ( ! $name OR !$password)
   {
      die('name and password required!');
   }
   $user = $db->select('*')   // use Query Builder!
           ->from('users')
           ->where('username', $name)
           ->get();
   if ( empty($user))
   {
      die('user '.$user.' not found!');
   }
   $user = current($user);
   if ($user['password'] == $password)
   {
       // correct password
   }
   else
   {
       die('wrong username/password combination!');
   }

}

Its for Kohana v2.3.4 (3.x has another controllers and methods name convention)


Line:

$index = new View('Index')

missing ";" at the end. Write:

$index = new View('Index');

Lines:

name = $post['name'];
password = $post['password'];

Missing $ before variable names. $post should be $_POST or better, use Kohana Arr::get() (see biakaveron's answer).

Advice:

Instead of writing defined('SYSPATH') or die('No direct script access.'); at the top of each script file, configure Apache to hide those files from Apache's "scope", or add a .htaccess to prevent direct access to some directories.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜