简体   繁体   中英

ignore favicon.ico request in MVC PHP

After few hours of struggle and re-reading my entire code I finally figured out what was that extra request that was logged in my User Activity table.

Basically my MVC structure logs each request that user makes. So if user goes to index page or does ajax call, everything is being logged in my table but the problem was that on each request it was logging 2 records and could not understand why.

Later I added some debugging var_dumps and found out that the second log is actually for file request: /favicon.ico

How would I ignore this in my router so that it does not make the log of this kind of file requests and just load the file. I do need favicon.ico and want to use it but dont want it to be part of request and dont want to log it on each request.

Every page hit records 2 rows right now, 1 row is correct and second row is for favicon.ico.

Please help if anyone had the same issue.

Edit:

Note that If I add some favicon.ico in my root folder then it makes only 1 request. So if i dont have favicon.icon then it makes 2 requests. Is it possible to check if favicon.ico does not exist dont make extra request.

As for requested here is my log code in my FrontController:

if ($user->isLoggedIn()) {
   $m_useractivity = ModelFactory::Create(array('userActivity', $user->getUID()));
   $log_id = $m_useractivity->logActivity($user->getUID(), $_SERVER['REQUEST_URI'], $a, $p, $request->getParams(), $request->browser(), $request->browserVersion(), $request->platform(), '', $request->ip(), $request->referer(), $request->getRequestMethod(), $request->type());
}

Just check the REQUEST_URI for favicon.ico:

if ($user->isLoggedIn() && strpos($_SERVER['REQUEST_URI'],'favicon.ico') === false) {
   //your code
}

Also you could use .htaccess to prevent this type of request from hitting your controller at all. Normally it should only hit your php code if the file requested doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM