0

I don't know if this is possible or even if it exist, but I'm very curious to find out. I don't want to give the wrong ideas, and describing what I'm trying to achieve before giving out examples definitely will - so I'll just dive right into it.

As far as I know, codes on websites are only ever executed when those websites are accessed by someone. If no one accesses those website, the codes just sit there. The codes have no reason to run if no one's using them, right?

Now what I'm going to propose may sound ridiculous, but please hear me out. I don't know if there is a way to do this, so I'm just going to ask. Is there a way to run those codes without someone accessing the website itself?

Now I know some of you are like, "Huh? What is he talking about? Why would you even want to run the codes if no one is on the website? That literally makes no sense," so I'm going to try and justify why I want something like that to be possible.

For example, if you want to create a script for automatically logging out an user if they've stayed inactive for a certain amount of time, you would need to check whether they've been active in the last (amount of time to wait before logging them out). You can use AJAX to check if they've been active in the last (amount of time to wait before logging them out). If they navigates, or refreshes the page, then it'll reset the counter, and let you know that they've been active in the last (amount of time to wait before logging them out). However, if they do nothing in (amount of time to wait before logging them out), they will be automatically logged out.

If they closed their browser, or exit the tabs that monitors their progress using AJAX, it will no longer monitor their progress, and thus their counter will not be updated, and thus you will have no idea whether they've been active or not. You can't just log them out if they close a tab or a browser, because what if they have multiple tabs or browsers of your website open? Then you would only want to log them out when they closed all of them.

I have other examples, but this is the gist of it. Is there a way to execute codes on a website without the website being accessed by a user? Thank you.

iscattered
  • 103
  • 1
  • 10
  • 1
    You can run php from the command line, if you are using unix/linux host than you can use cron jobs to execute php code – Patrick Evans Jun 07 '15 at 18:00
  • 1
    There are `cron` servers out there that will do exactly that for you. You will probably have to pay for their service but it comes fairly cheap, like 0.001 Cent per call. You can set up `cronjob`s on these servers that will call up any URL you tell them and thereby run the php code you want. – Carsten Massmann Jun 07 '15 at 18:02
  • @Roberto It was just an example. – iscattered Jun 07 '15 at 18:07
  • See: [How do I expire a PHP session after 30 minutes?](http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes) – Yogi Jun 07 '15 at 18:14

2 Answers2

4

You are looking for cron jobs. They are basically scheduled jobs that run at set times. A cron job can run all kinds of scripts including PHP scripts.

Whether such a script can easily clear expired sessions, I don't know. It will probably depend on the way you store the sessions.

It may be just as easy to implement it in the website. If you store the last activity timestamp of a user, you can just check on a new request whether that timestamp is too old, and if so, delete the session and redirect to the login page. That way, the user officially remains logged in until their next request.

Optionally you may delete old sessions that are remembered by PHP. See related question: Cleanup PHP session files.

Community
  • 1
  • 1
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
1

One approach is that you could run your PHP scripts on a timer using CRON jobs. These jobs typically repeat every x hours, minutes, or days.

I'm not sure about the example you provided, though.

Gyt Dau
  • 100
  • 8