5

对于我的项目,我使用学生表进行身份验证和存储信息。对于学生注册,所有数据都插入到学生故事中,这与登录身份验证不同。学生表的属性是[id(primary key and auto incremented), username, password, stdname, stdage, created_at, updated_at]

登记

public function post_register()
{
    $input = Input::all();
    $rules = array(
        'email' => 'required|email|Between:3, 64|unique:student,username',
        'studentPass' => 'required',
        'studentName' => 'required',
        'studentAge'  => 'integer|Min:18|Max:45');
    $messages = array(
        'email.required' => 'You forgot to enter your email id!',
        'email.between'  => 'Email must be between 3 to 64 character!',
        'studentPass.required' => 'You forgot to enter your password!',
        'studentName.required' => 'You forgot to enter student name!',
        'studentAge.integer'   => 'Student age must be a integer value!',
        'studentAge.min'   => 'Student age must be a greater than 18 years!',
        'studentAge.max'   => 'Student age must be a less than value 45 years!'

    ); /* Add your custom messages here */
    $validator = Validator::make($input, $rules, $messages);
    if($validator->fails())
    {
        return Redirect::to('register')->withErrors($validator);
    }
    else
    {
        $student = new student();
        $student->username = $input['email'];
        $student->password = $input['studentPass'];
        $student->stdname  = $input['studentName'];
        $student->stdage   = $input['studentAge'];
        $student->save();
        return Redirect::to('login');
    }
}

登录

public function post_index()
{
    $input = Input::all();
    $rules = array(
        'email' => 'required|email|Between:3, 64',
        'studentPass' => 'required');

    $messages = array(
        'email.required' => 'You forgot to enter your email id!',
        'email.between'  => 'Email must be between 3 to 64 character!',
        'studentPass.required' => 'You forgot to enter your password!'
    ); /* Add your custom messages here */

    $validator = Validator::make($input, $rules, $messages);

    if($validator->fails())
    {
        return Redirect::to('login')->withErrors($validator);
    }
    else
    {
        $credential = array('username' => $input['email'], 'password' => $input['studentPass']);
        $auth = Auth::attempt($credential);
        if(Auth::attempt($credential))
            return Redirect::to('index');
        else
            return Redirect::to('/');
    }
}
4

2 回答 2

5

在您的注册页面中,使用 HASH 输入您的密码。将代码编写为

public function post_register()
{
    $input = Input::all();
    $rules = array(
        'email' => 'required|email|Between:3, 64|unique:student,username',
        'studentPass' => 'required',
        'studentName' => 'required',
        'studentAge'  => 'integer|Min:18|Max:45');
    $messages = array(
        'email.required' => 'You forgot to enter your email id!',
        'email.between'  => 'Email must be between 3 to 64 character!',
        'studentPass.required' => 'You forgot to enter your password!',
        'studentName.required' => 'You forgot to enter student name!',
        'studentAge.integer'   => 'Student age must be a integer value!',
        'studentAge.min'   => 'Student age must be a greater than 18 years!',
        'studentAge.max'   => 'Student age must be a less than value 45 years!'

    );//Add your custom messages here
    $validator = Validator::make($input, $rules, $messages);
    if($validator->fails())
    {
        return Redirect::to('register')->withErrors($validator);
    }
    else
    {
        $password = Hash::make($input['studentPass']);
        $student = new student();
        $student->username = $input['email'];
        $student->password = $password;
        $student->stdname  = $input['studentName'];
        $student->stdage   = $input['studentAge'];
        $student->save();
        return Redirect::to('login');
    }
}

在 Laravel 中,密码必须是 HASH。我认为在替换函数 post_register() 之后,您的登录身份验证将正常工作。

于 2013-06-25T12:05:10.757 回答
0

For the user registration, you need to Hash::make($input['studentPass'])

For the user login, you need to (A) Verify your posted password field name, and (B) auth only once... you have:

 $auth = Auth::attempt($credential);
 if(Auth::attempt($credential))

Instead, just use if($auth).

Lastly, you should check your app/config/auth.php and ensure your 'driver' is set to 'eloquent'. Then make your class User extends Eloquent model.

于 2013-06-25T20:12:19.107 回答