简体   繁体   中英

How to limit number of searches per user in PHP

I am trying to develop an application where a guest user can see search results only 10 times after which he should be directed to payment page. I can use sessions on the search results page, but how can i put a counter on that. Can any please help me on that.

Every time a search request is created you just do

$_SESSION['counter']++

Altough he can just get rid of the limit by deleting cookies. An other approach would be, to store the number of search requests in a database table including the IP address, but this can also be bypassed, while it takes more work to do so.

If you should put search limit on current running session than you can use $_SESSION['count']++ . And if you should put search limit per day than you can use 'UPDATE users SET search_count = search_count+1'

It depends whether you would allow him to search again when he comes to your website or just those 10 times even after he visits after a year.

For temporary bases, you can use cookies (see setcookie function) but if you want to restrict him once and for all, you will have to ssave that information in database.

You would code something like:

<?php
  session_start();

  $_SESSION['counter'] += 1;
  // more logic/code

Now you will have to save the value of $_SESSION['counter'] .

If your users can search only while logged in, then I see no problem - you definitely have db table with users, so just add another column to it, say 'search_count' and increase it by one each time user attemps a search.

For example:

UPDATE `users` SET search_count = search_count+1

您还可以在数据库的表用户中使用计数器,并在每次用户查找结果时调用函数,将值递增1,这样就可以进行简单的更新。

I think maintaining database will be much better then maintaining SESSION because may be due to some reason session removed or erased.

add a field within users table name for example visit with default value 0 and update this field on every visit of search result page..

 "update usertablename set visitfield = visitfield + 1 where user_id = ".$current_user_id

thanks

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