Php 防止用户未经授权访问网页
如何防止用户通过url浏览访问我的网页。我的意思是,在访问任何网页之前,我需要检查用户是否已登录。应用程序不应允许用户仅通过url访问页面 我必须检查每个控制器的身份验证,还是有其他方法 假设我有一个控制器Php 防止用户未经授权访问网页,php,laravel,laravel-5,Php,Laravel,Laravel 5,如何防止用户通过url浏览访问我的网页。我的意思是,在访问任何网页之前,我需要检查用户是否已登录。应用程序不应允许用户仅通过url访问页面 我必须检查每个控制器的身份验证,还是有其他方法 假设我有一个控制器DistributorController。现在控制器中的方法是 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Session; use App\Distributor; class D
DistributorController
。现在控制器中的方法是
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use App\Distributor;
class DistributorController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
function fetchData()
{
$distributors = Distributor::all()->toArray();
return compact('distributors');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('pages.distributors', $this->fetchData());
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
try{
// code block
}
catch (\Exception $e) {
// code block
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
在定义路由的web.php
文件中,可以使用Auth中间件对路由进行分组和包围。您可以阅读更多内容,例如,我想您可以查看用户会话
if (!$_SESSION['id']){ // if user session is not found
header("location:http://yoursite.index.php"); //redirect anywhere
}
else {
// Your code 'view page'
}
希望我的答案能解决这个问题:)可能的重复