Php – Alternatives to using cookies

PHPsession

Whate are alternatives to using cookies/client-side storage for a PHP/MySQL based site on Apache.

Scenario/Requirements:

  • I want to try using some anti-bot code to prevent specific scrapers etc. from accessing the site.
    • I would like to run this code before launching the rest of the site (before DB access etc.).
    • I don't want to constantly run the same code on every page-load after a visitor has passed the initial check.
  • I'd like to avoid the use of Cookies/Client side storage if at all possible.

The only solution I can currently think of is to write files to the server based on the visitors IP/UA, or to write a list of them to a single file.

Yet this has the limitation of multiple users through a proxy/same connection, etc …

So, any ideas/suggestions?

Or am I simply over working the issue?

Best Answer

In general case, in order to keep track of users and for example to know which one is logged on, sessions are used. But sessions mean session IDs stored in cookies¹; it's just that if you need to remember N entries for a user, there is only one cookie involved, not N.

In your particular case, on the other hand, you care about protecting your website against illegitimate use (and avoid repetitive checks). It means that sessions are not appropriate: why a scrapper would ever send session cookie to you?² Instead:

  1. Detect the IP address of the request before starting processing it,

  2. If you have no information about the IP address:

    • Detect if it is a scrapper (like you currently do),

    • Store the result in cache (or in database, or both),

  3. Load the result from cache.

  4. Respond according to the result. If it's a scrapper, send a "please stop scrapping" page; if it is a legitimate user, process the page and send the ordinary response.


¹ PHP is also able to pass session ID in URIs. It's ugly and must be used only when you can't do anything else.

² Remember, legitimate users may have their cookies disabled for some reason. The fact that a client doesn't send the cookies to you doesn't automatically mean it's an automated process.

Related Topic