PHP turns Watch

Swinely
Badges: 2
Rep:
?
#1
Report Thread starter 11 years ago
#1
Basically i'm slowly making a php + mysql text based game just for the practice but i'm having real problems developing a way of adding turns to users after every X amount of time.
I would have included it on the main page but that would require that users have to login to get their turns, does anyone have any suggestions for implementing turns?
0
reply
samba
Badges: 3
Rep:
?
#2
Report 11 years ago
#2
cron executing the ticker every x seconds?
0
reply
Swinely
Badges: 2
Rep:
?
#3
Report Thread starter 11 years ago
#3
Ahh so thats what crons are... i'll look into that any minute, just got dinner! Rep for you already though
0
reply
samba
Badges: 3
Rep:
?
#4
Report 11 years ago
#4
here we go... sorry, was too lazy to search for anything :p:

CRON(8) FreeBSD System Manager's Manual CRON(8)

NAME
cron -- daemon to execute scheduled commands (Vixie Cron)

SYNOPSIS
cron [-j jitter] [-J rootjitter] [-s] [-o] [-x debugflag[,...]]

DESCRIPTION
The cron utility should be started from /etc/rc or /etc/rc.local. It
will return immediately, so you don't need to start it with '&'.

The cron utility searches /var/cron/tabs for crontab files which are
named after accounts in /etc/passwd; crontabs found are loaded into mem-
ory. The cron utility also searches for /etc/crontab which is in a dif-
ferent format (see crontab(5)). The cron utility then wakes up every
minute, examining all stored crontabs, checking each command to see if it
should be run in the current minute. When executing commands, any output
is mailed to the owner of the crontab (or to the user named in the MAILTO
environment variable in the crontab, if such exists).

Additionally, cron checks each minute to see if its spool directory's
modification time (or the modification time on /etc/crontab) has changed,
and if it has, cron will then examine the modification time on all
crontabs and reload those which have changed. Thus cron need not be
restarted whenever a crontab file is modified. Note that the crontab(1)
command updates the modification time of the spool directory whenever it
changes a crontab.

CRONTAB(1) FreeBSD General Commands Manual CRONTAB(1)

NAME
crontab -- maintain crontab files for individual users (V3)

SYNOPSIS
crontab [-u user] file
crontab [-u user] { -l | -r | -e }

DESCRIPTION
The crontab utility is the program used to install, deinstall or list the
tables used to drive the cron(8) daemon in Vixie Cron. Each user can
have their own crontab, and though these are files in /var, they are not
intended to be edited directly.

If the allow file exists, then you must be listed therein in order to be
allowed to use this command. If the allow file does not exist but the
deny file does exist, then you must not be listed in the deny file in
order to use this command. If neither of these files exists, then
depending on site-dependent configuration parameters, only the super user
will be allowed to use this command, or all users will be able to use
this command. The format of these files is one username per line, with
no leading or trailing whitespace. Lines of other formats will be
ignored, and so can be used for comments.

The first form of this command is used to install a new crontab from some
named file or standard input if the pseudo-filename `-' is given.

The following options are available:

-u Specify the name of the user whose crontab is to be tweaked. If
this option is not given, crontab examines ``your'' crontab,
i.e., the crontab of the person executing the command. Note that
su(1) can confuse crontab and that if you are running inside of
su(1) you should always use the -u option for safety's sake.

-l Display the current crontab on standard output.

-r Remove the current crontab.

-e Edit the current crontab using the editor specified by the VISUAL
or EDITOR environment variables. The specified editor must edit
the file in place; any editor that unlinks the file and recreates
it cannot be used. After you exit from the editor, the modified
crontab will be installed automatically.

SEE ALSO
crontab(5), cron(8)

FILES
/var/cron/allow
/var/cron/deny
0
reply
idiot
Badges: 1
Rep:
?
#5
Report 11 years ago
#5
Code:
#!/bin/bash
for ((;;)); do
	php -r "
	connect_to_database_and_so_on();
	mysql_query('UPDATE `users` SET turns=turns+1 WHERE `last_turn` < (TIME() - (60 * 60 * 24))');
	";
	sleep 24h
done
I'm a little rusty with SQL syntax, because it's been ages since I last went near a database. And obviously I don't know anything about your database, so that part is a bit of a shot in the dark. But this code *will* loop once per day, and it *will* execute whatever PHP you stick between the double quotes.
0
reply
Swinely
Badges: 2
Rep:
?
#6
Report Thread starter 11 years ago
#6
Thanks for that, might use it because crons seem to be rather annoying to get working. Rep for you too .

I'm using plesk control panel so crons = automated tasks, but I can't find any good tutorials, do I save the file as .php or .sh? And input the ftp path or the http path?
0
reply
tommorris
Badges: 2
Rep:
?
#7
Report 11 years ago
#7
Cron takes a Unix command and runs it at the set time. There are a variety of ways to trigger your scripts.

If you are using PHP, you can trigger a PHP script by setting the following as the command:
php -r path_to_your_script.php

Alternatively, if there is some function available over the Web, you could use curl:
curl "http://your.web.site.com/foo.php" >> /dev/null

If your script is a .sh, like idiot's, simply put the path to that script:
/home/yourusername/scripts/name.sh

For a comprehensive tutorial on cron, see this article.

You can also some kind of pseudo-cron script, like this one (disclaimer: I haven't tested this - it could be a load of crap), which just uses visits to your site as a way for the cron to be triggered. If your server doesn't support cron, this is one way to do it.
0
reply
Swinely
Badges: 2
Rep:
?
#8
Report Thread starter 11 years ago
#8
That would require they logged on though, crons are the official way of doing this as I need the updates to be automatic..... but not a single tutorial explains how using scheduled tasks. The server does support these but the setup is just proving to be total guesswork :|

edit: YES!!! I've finally got it working, the filepath that tech support gave me was utter bull! It works but the only problem is anyone logging onto that page will give everyone an extra turn.. but at least it works!

Thanks to everyone for their inputs, i do believe i've repped you all.
0
reply
samba
Badges: 3
Rep:
?
#9
Report 11 years ago
#9
Swinely: you can just password protect the dir with the cron, or instead of invoking php in the cron, invoke it in the file itself.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Brexit: Given the chance now, would you vote leave or remain?

Remain (1047)
79.02%
Leave (278)
20.98%

Watched Threads

View All
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise