5

I want to build a website that only one person at a time can visit. How would I accomplish this task?

I looked at Web counters built in php, but I don't think this is the right solution. Is there an http request for this?

Thank you in advance.

3
  • 5
    I'm very curious why you want his?
    – Martijn
    Commented Apr 11, 2017 at 10:40
  • 3
    It might be worth clarifying what you mean by 'on my website'. Traditionally a web browser sends a request for a page, and then as soon as it receives it closes the connection to the server, but the user can keep looking at it for as long as they want. So do you consider them to be 'on the website' for all the time they're looking at the page? If so you'd need to add some method to try to track when they stop looking.
    – bdsl
    Commented Apr 11, 2017 at 13:43
  • 1
    I think you understood 'on my website' correctly: As long as the first visitor has the page open it is unavailable for any new visitors. (Maybe the user even gets kicked out, after he changes tabs or the focus is not on the window). Their is no specific reason behind. Just out of artistic curiosity :) I found this page, when I looked if something similar already existed: mostexclusivewebsite.com Commented Apr 12, 2017 at 12:11

2 Answers 2

3

If you're looking to only serve content to one person at a time, this would be a server level mod. As an example for Nginx, you'd limit the number of active connections to 1:

server { limit_conn perserver 1; }

This serves a 503 error to anyone else that connects. It is possible to serve a custom page with the 503 error, alerting the user as to why they've received it.

If this doesn't suit your needs, you need to serve content to everyone but gate the 'real' content so that only one user at a time can view it. You'd create a page that everyone downloads with an embedded AJAX script. The script makes an API call to your server, asking if the 'page' is occupied. When it's not, the server assigns that page to the user session and returns the content. The script would then display this. Those not currently on the 'page' would be served a waiting page and continue to poll. This would also allow you to form an orderly queue on your server's side too.

Going into that territory, you're better off posting on Stackoverflow to ask how to make such a script.

1
  • This server level mod is exactly what I was looking for, but as you already figured doing it with AJAX or NodeJS would give me a lot more options to play around with. I will probably try doing it with NodeJS for the reasons Martijn already stated. Thank you very much! Commented Apr 12, 2017 at 12:26
2

You can use something like NodeJS. This is serverside javascript, with a template for the visitor. This template connects to the serverside JS (it's easier than it sounds).

The advantage of using NodeJS (or another serverside javascript language) is that the moment the logged in user leaves, another can join. You can broadcast a "we have room for one" signal, and maybe via a cue the next one joins. And you can give all the other users a nice message instead of a (customized) error page and possibly a queue.

It works similar to AJAX, but reversed. With AJAX you ask the server "Can I go in yet?" periodically, whilst the socket by NodeJS has a small connection open, and the server triggers the "OK, I'm available now". This allows for a more userfriendly alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.