trangshopeeaffiliate/app/Http/Controllers/Web/ShortLinkController.php
2026-07-11 22:56:22 +07:00

46 lines
1.3 KiB
PHP

<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Models\ConversionHistory;
use Illuminate\Http\RedirectResponse;
class ShortLinkController extends Controller
{
/**
* Redirect short URL to the actual Shopee affiliate redirect URL.
*/
public function redirect(string $code)
{
$history = ConversionHistory::whereRaw('BINARY code = ?', [$code])
->where('status', 'success')
->first();
if (!$history || !$history->affiliate_url) {
abort(404, 'Short link not found or invalid.');
}
// Detect if request is from a crawler bot (Zalo, Facebook, etc.)
$userAgent = strtolower(request()->header('User-Agent', ''));
$bots = ['zalo', 'facebookexternalhit', 'telegrambot', 'twitterbot', 'slackbot', 'googlebot', 'crawler'];
$isBot = false;
foreach ($bots as $bot) {
if (str_contains($userAgent, $bot)) {
$isBot = true;
break;
}
}
if ($isBot) {
return view('short_link_preview', compact('history'));
}
// Increment click count for real users
$history->increment('clicks');
return redirect()->away($history->affiliate_url);
}
}