laravel推荐系统:处理Cookie

时间:2018-06-22 20:20:39

标签: php laravel

我创建了一个简单的引荐系统,当您创建帐户时,它会创建引荐链接,您可以将其发送给个人,以便他们在使用该链接进行注册时使用。在用户表上,输入referred_id以匹配发送链接的用户ID。

http://localhost:8000/register/?ref=12

我遇到的唯一问题是,当我推荐用户时,cookie会保存好,但是如果我不使用www.localhost/8000/register来推荐用户而仅推荐用户的cookie,仍然会在其中输入用户表。但是用户表上的referred_id应该为空,因为我没有使用引荐链接?我该如何解决这个问题

App \ Middleware \ CheckReferral.php

<?php

namespace App\Http\Middleware;
use Illuminate\Http\Request;
use App\Http\Middleware\CheckReferral;
use Closure;
use Illuminate\Support\Facades\Cookie;

class CheckReferral
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // Check that there is not already a cookie set and that we have 'ref' in the url
        if (! $request->hasCookie('referral') && $request->query('ref') ) {
          // Add a cookie to the response that lasts 5 years (in minutes)
          $response->cookie( 'referral', encrypt( $request->query('ref') ), 500 );
        }
        // if ref exist already in the cookie then show error page
        else {
            if ( $request->query('ref') ) {
                return redirect('/error');
            }
            return $response;

        }

        return $response;
    }
}

Auth \ RegisterController.php

<?php

namespace App\Http\Controllers\Auth;
use Auth;
use Illuminate\Support\Facades\Cookie;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
     protected function create(array $data)
     {

             $referred_by = Cookie::get('referral');


         $user =  User::create([
             'name' => $data['name'],
             'email' => $data['email'],
             'password' => bcrypt($data['password']),
             'referred_by' => $referred_by,
         ]);
         return $user;
     }
}

1 个答案:

答案 0 :(得分:0)

如果您要执行此行为,则应仅使用查询参数,而不要使用Cookie;如果您在此处使用Cookie,即

  //nav-active
  function onScroll(event){
    var scrollPosition = $(document).scrollTop();
    $('.menu-list a').each(function () {
      var currentLink = $(this);
      var linkHref = currentLink.attr("href"); // extract the href attribute from link
      var anchor = linkHref.split("#").pop(); // split by # and take the last part
      var refElement = $("#" + anchor); // use the anchor extract to use it as id selector

      // add refElement.length check - to just continue if the element actually exist
      if (refElement.length && refElement.position().top <= scrollPosition && refElement.position().top + refElement.height() > scrollPosition) {
        $('.menu-list a').removeClass("active");
        currentLink.addClass("active");
      }
      else{
        currentLink.removeClass("active");
      }
    });
  }

如果存在,它将从cookie中获取价值。如果您只想在提交查询参数(如

)时保存$ referred_by
$referred_by = Cookie::get('referral');

您应该使用

http://localhost:8000/register/?ref=12