0

I have created a system in which i have two types of users: Authentic users Demo users

Authentic users are the users that are real life users,all data belonging to these users is needed. Demo users are the users which are fake users created for demonstration purpose only.Information of such users is not needed.

When an authentic user logs in he performs operations and saves his data and logs out .All information is saved in database.

When demo user logs in he performs same operations and logs out.I have created a script which deletes all the data of the demo user when log out.This works perfectly.

The problem arises when the demo user does not log out but just closes the browser.The delete script does not run which is present on the 'logout page' .

I want something that would run the script on the closing of the browser and the delete is successful.

Francis
  • 147
  • 3
  • 15
  • 4
    would it be a problem, to use a cron job to delete inactive users? – rsz May 23 '16 at 13:11
  • I think a cron job is scheduled .meaning on specific time.Not on action – Francis May 23 '16 at 13:14
  • [beforeunload](https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload) is the best you have, but thats if the page is navigated away from, not just when the browser is closed. IMO I wouldn't depend on the users browser to solve this issue. The cron that rsz mentioned is a better bet. – castis May 23 '16 at 13:18
  • @Francis You likely don't *need* to clear out the demo data immediately. Use a cron to cleanup any demo user that's older than 24 hours or something. – ceejayoz May 23 '16 at 13:21

2 Answers2

1

You can use a back process(cron) [look for crontab for unix based servers] that runs in background executes every 5 min for example and deletes all the data you don't need.

If you don't want to use a cron you can also use a function that deletes all the data that you don't need on every login or logout, but keep in mind that this will affect the user experience about the times of loading in case that you have big data.

I suggest the first way but maybe the second it's easier for you.

;)

Caius
  • 193
  • 12
0

In Javascript, you can get the onunload event, that is called before the page quits. You can perform an AJAX request to your .php script before the user quits the page, in order to destroy all the data of the demo on your server when the user quits, even if he does not logout manually.

Community
  • 1
  • 1
Olivier
  • 118
  • 11
  • 3
    this would call it always? how can you distinguish a "leaving user" from a "user which is loading a differnet page"? – rsz May 23 '16 at 13:22
  • I think you have to do it manually. You could set a callback when a link on your page is clicked, which could set a boolean to `true` when it's an internal link, and `false` when it's an external one for example. And before launching the script to erase the data, you check if the boolean is set to `true` or `false`. That would work for links or forms for example, but not when the user reloads the page. – Olivier May 23 '16 at 13:44