php - laravel 推荐系统:处理 Cookie
问题描述
我制作了一个简单的推荐系统,当您创建帐户时,它会创建推荐链接,您可以将其发送给个人,以便他们使用该链接注册时。在用户表中,输入了 refer_id 以匹配发送链接的用户 id。
http://localhost:8000/register/?ref=12
我遇到的唯一问题是,当我推荐用户时,cookie 被保存,这很好,但如果我在没有推荐用户的情况下注册,仅使用www.localhost/8000/register
被推荐用户的 cookie 仍会输入到用户表中。但是用户表上的referred_id 应该为空,因为我没有使用推荐链接?我该如何解决这个问题
应用\中间件\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;
}
}
解决方案
如果你想要这种行为,你应该只使用查询参数而不是cookie,如果你在这里使用cookie,即
$referred_by = Cookie::get('referral');
如果存在,它将从 cookie 中获取值。如果您只想在提交查询参数时保存 $referred_by
http://localhost:8000/register/?ref=12
你应该使用
$referred_by = $request->query('ref');
推荐阅读
- javascript - document.body.innerText 不会从页面中选择所有文本
- c - 为什么 pragma unroll 1 意味着根本不展开?
- sas - 仅在没有新信息时保留重复 ID
- python - 似乎无法使用 Pycharmpy 设置 tesseract 模块
- javascript - 调整页面大小时元素的位置移动
- unity3d - Unity3D:需要帮助旋转角色而不涉及位置
- angular - Angular 5 Reactive Forms 自定义和必需验证器:与自定义验证器一起使用时,必需验证器始终返回 True
- java - 使用迭代器打印和更改项目
- javascript - 如何为更改图像添加动画
- solr - Solr Parent Blockjoin 与子级过滤查询