【问题标题】:Use session object from PHP in Laravel 4.2 controller在 Laravel 4.2 控制器中使用来自 PHP 的会话对象
【发布时间】:2015-12-15 03:27:53
【问题描述】:

在 Laravel 4.2 中,我声明了一个会话:

session_start();
$code=rand(100000,999999);
$_SESSION["captcha"]=$code;

然后我想获取我存储的数据,并在 Laravel 控制器中使用它来检查用户是否输入了正确的代码,然后再将其输入数据库。

这是我的代码:

public function store2()
{
    // i was trying to get the value
    // session_start();
    // $getsCapt = $_SESSION["captcha"];

    $rules = array(
        'username'     => 'required|min:2|max:50|regex:/^[a-zA-Z0-9\-\s]+$/',
        'description'  => 'required|min:1|max:100|regex:/^[a-zA-Z0-9\-\s]+$/',
        'usertype'     => 'required|numeric'
    );
    $validator = Validator::make(Input::all(), $rules);

    // process the login
    if ($validator->fails()) 
    {
        return Redirect::to('createsu')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    } 
    else 
    {
        // store
        $su = new SystUser;
        $su->SystemUserTypeID       = Input::get('usertype');
        $su->SystemUserName         = Input::get('username');
        $su->SystemUserPassword     = 'changeme';
        $su->SystemUserDescription  = Input::get('description');
        $su->timestamps = false;
        $su->save();

        // redirect
        Session::flash('message', 'Successfully created system user!');
        return Redirect::to('createsu');
    }
}

【问题讨论】:

  • 我对 laravel 不熟悉,但我认为你应该使用 $getsCapt = Session::get('captcha'); 而不是 session_start();$getsCapt = $_SESSION["captcha"];
  • 谢谢你也试试这个!

标签: php session laravel laravel-4


【解决方案1】:

在 Laravel 中,您不必显式启动会话。

你可以这样做:

在会话中设置

$captcha='whatever';
Session::put('captcha', $captcha);

从会话中获取

if(Session::has('captcha'){
    $captcha = Session::get('captcha');
}

【讨论】:

  • 我可以在 php 文件中使用Session::put('captcha', $captcha)' 吗?因为我在 PHP 文件上生成 $captcha 的值
  • 是的,您可以在 php 文件中使用它,最好的位置是在控制器中。
猜你喜欢
  • 2016-07-21
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 2015-12-27
  • 2016-08-25
  • 2016-10-03
  • 2016-10-08
相关资源
最近更新 更多