KOhana ..简单的功能需要帮助
问题描述:
我新来kohana .. Guyz PLZ指出在下面的代码中的错误.. IM无法运行它..它是一个简单的连接到数据库..我可以比较查询结果,正如我已经这样做了POST,项目.. PLZ正确..KOhana ..简单的功能需要帮助
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Default Kohana 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!');
}
}
及其Kohana的v2.3.4(3.x的具有另一个控制器和方法名称约定)
答
线:
$index = new View('Index')
失踪 “;”最后。写:
$index = new View('Index');
线:
name = $post['name'];
password = $post['password'];
缺少$
变量名之前。 $post
应该是$_POST
或更好,请使用Kohana Arr::get()
(参见biakaveron的回答)。
建议:
在每个脚本文件的开头写defined('SYSPATH') or die('No direct script access.');
相反的,配置Apache来隐藏Apache的“范围”这些文件,或增加一些目录一的.htaccess,防止直接访问。
Kohana的是什么版本呢? 2.3.4? – biakaveron 2011-05-05 10:38:48
你遇到什么错误? – 2011-05-05 12:52:03