php - Why does laravel redirect twice?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-06-20 10:07:24
0
1
1134

Currently we are building a webpage for WeChat login, using the interface provided by WeChat. When the user accesses the index/index, it jumps to the url specified by WeChat:

function index(){
    $wxService = \App::make('App\Http\Service\WXActivityService');
    $target_url = route('Index/Activity');
    $redirect_url = $wxService->getWxRedirectUrl($target_url);
    return redirect()->intended($redirect_url);
}

This code is very simple, it just splices the url according to the url format specified by WeChat, and then redirects to this url ($target_url). WeChat will redirect back to the $target_url page I set based on the redirect_uri.
But the problem now is that after WeChat redirects back to my page with the code, my server will receive the same request twice, which is very strange. May I ask what is happening? Is it laravel? Is there a problem with cross-site redirection? The header function cannot be used to redirect, which is really frustrating...
I recorded the log and found that the index method was only executed once, but $target_url was accessed twice. Second-rate...

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(1)
滿天的星座

Check the Request Method of the two requests:
Because it is a cross-domain request, the client sends an OPTIONS request first
Preflighted Requests is a transparent server verification mechanism in CORS. The preflight request first needs to send an HTTP OPTIONS request header to the resource of another domain name. The purpose is to determine whether the actual request sent is safe.

It is very likely that this is the problem.
I now use axios as my network request library, and I have also encountered this problem. When accessing cross-domain, an OPTIONS request will be sent first to determine whether the next request is safe and acceptable. Allow

My current approach is to check whether it is an OPTIONS request in the index.php entry file, and if so, return a status
Specific code

if ($_SERVER['REQUEST_METHOD']=='OPTIONS') {
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
    header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
    return;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!