1. Go to this page and download the library: Download shammaa/laravel-security library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// .env
SECURITY_EXCLUDED_ROUTES=api/ai/*
// Now your AI routes won't be blocked
Route::post('/api/ai/rewrite', [AIController::class, 'rewrite']);
Route::post('/api/ai/generate', [AIController::class, 'generate']);
// In Controller
public function upload(Request $request)
{
$file = $request->file('document');
// Simple way - Helper Function
if (!security_validate_file($file)) {
return back()->withErrors(['file' => 'File not allowed']);
}
// Secure storage
$path = security_store_file($file);
// Or use Facade
if (Security::validateFile($file)) {
$path = Security::storeFile($file);
}
return response()->json(['path' => $path]);
}
// In Controller or Middleware
public function apiEndpoint(Request $request)
{
$userId = auth()->id();
// Rate limit: 100 requests per minute
if (!Security::rateLimit("api:{$userId}", 100, 1)) {
return response()->json([
'error' => 'Too many requests'
], 429);
}
// Rest of the code...
}
// In LoginController
public function login(Request $request)
{
$email = $request->email;
// Simple way - Helper Functions
if (security_is_locked($email)) {
return back()->withErrors(['email' => 'Account locked. Try again later.']);
}
if (!Auth::attempt($request->only('email', 'password'))) {
// Record failed attempt
security_record_failed_login($email);
return back()->withErrors(['email' => 'Invalid credentials']);
}
// Success - clear failed attempts
security_clear_failed_logins($email);
return redirect('/dashboard');
// Or use Facade
// if (Security::isLocked($email)) { ... }
// Security::recordFailedLogin($email);
// Security::clearFailedLogins($email);
}
// Sanitize any input
$name = security_sanitize($request->input('name'));
$email = security_sanitize($request->input('email'));
// Or use Facade
$name = Security::sanitize($request->input('name'));
// In Blade
{!! Security::xssFilter($user->bio) !!}
// Or Helper
{!! security_xss_filter($user->bio) !!}
use Shammaa\LaravelSecurity\Events\SecurityThreatDetected;
Event::listen(SecurityThreatDetected::class, function ($event) {
// Send email, notification, etc...
Mail::to('[email protected]')->send(new SecurityAlert($event));
});