Dubbeldam Posted December 1, 2011 Share Posted December 1, 2011 Hey guys, I'm working on something which lets a user create an account and letting him log in. For the reason of me learning how such a code works. That worked out fine. Now being on Mafiawars on facebook I noticed that every hour money increased, whether you are logged in or not. I tried to create such a part... but with failure. So here my question, Is it possible to increase a value by another value every hour in a table without the user logged onSo let's say: The field Hourlymoney: 50The field money: 400 Now is it possible to add 50 every hour (13:00, 14:00, 15:00 etc...) to the 400. Without the user being logged in whatsoever. Thanks ! Link to comment Share on other sites More sharing options...
morrisjohnny Posted December 1, 2011 Share Posted December 1, 2011 depending on the type of hosting you are using depends on the answer Linux: Create a CronJob (there will be an option in the CPanel usually) http://en.wikipedia.org/wiki/CronWindows: Task in Task Scehule or their should be something simlar in the CPanel. At home (I'm at work) i created something that checked the last time the user logged in and then worked out how much more money they required. back in the day when i created a mafia game. i'm sure i have the scripts lieing around somewhere if i remeber i wil post back tonight ! (around 6 hours time) Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 1, 2011 Author Share Posted December 1, 2011 Thanks! I'd appreciate it. I'll check out CPanel etc. Btw, where can I see HAX? Looks interesting... Link to comment Share on other sites More sharing options...
ShadowMage Posted December 1, 2011 Share Posted December 1, 2011 An easier way would be to store the last time the money was incremented. Then the next time the user logs on, compare the current time to the last time the money was incremented and figure out how many hours have passed. Take that number and multiply it by the increment value and add it to the total. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 1, 2011 Share Posted December 1, 2011 It's more flexible to use a cron job. The increment can change every hour and it will still be correct, and other players can check the current balance of anyone without them logging on. Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 2, 2011 Author Share Posted December 2, 2011 I indeed just found out of the use of a cron job. So time for RESEARCH! Me gusta Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 2, 2011 Author Share Posted December 2, 2011 Ok I am doing something wrong, but can't figure out what. If I add a button on the page called 'AddMoney', and refer it to 'addmoney-exec.php' it works. It adds the money..... Now I want to have it automatically every hour. Fine, this is what I did:executecron.php <?php$data = file("C:/xampp/htdocs/mafiawarstest/moneyadd-exec.php");?> cron.bat C:\xampp\PHP\PHP.exe C:\xampp\htdocs\mafiawarstest\executecron.php On windows scheduled tasks-New basistask-Name: Addmoney-Trigger: daily / 14:00 (for example)-Action: Open program - program/script: cron.bat Now I see that command prompt opens up & closes but besides that nothing happens... Maybe because I run the site on 127.0.0.1/mafiawarstest/index.php etc... instead of c:/.... ? Link to comment Share on other sites More sharing options...
morrisjohnny Posted December 2, 2011 Share Posted December 2, 2011 Hello DubbelDam, Sorry i forgot last night it's in my phone to remind me tonight. Hax? it kind of died. but its still online i believe agian will pop back tonight with details. Try referencing the link directly for example http://www.(SERVERNAME)/mafiawarstest/moneyadd-exec.php you will need to of course make sure no log-in is required to this page. or no sessions are set as these will not be set by the serverbasically you want to do foreach($users as $user) mysql_query(UPDATE `tablename` SET 'money'='money'+$moneyamount where id=$user->id); there is a MUCH better way to do that but again thats from the top of my head. If you have msn/skype your welcome to skype/msn me i think i have ALOAD of files that could help you. Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 2, 2011 Author Share Posted December 2, 2011 Thank you very much for your help and I really don't mind if you forget. I appreciate any help available.I did already do the UPDATE members SET money etc. etc..... I think where I can be wrong is the log-in, as it works as link through the website but not as stand alone. This is what I have.... (Don't forget I do this for practice, and am pretty much a newbie so a lot may look messy) <?php //Start sessionsession_start(); //Include database connection detailsrequire_once('config.php'); //Array to store validation errors$errmsg_arr = array(); //Validation error flag$errflag = false; //Connect to mysql server$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);if(!$link) { die('Failed to connect to server: ' . mysql_error());} //Select database$db = mysql_select_db(DB_DATABASE);if(!$db) { die("Unable to select database");}//$email = $_SESSION['SESS_EMAIL']; //Select everythingmysql_query("SELECT * FROM members")or die(mysql_error()); //Update the hourly moneymysql_query("UPDATE members SET money=money+moneyhourly WHERE email=email")?> Link to comment Share on other sites More sharing options...
morrisjohnny Posted December 2, 2011 Share Posted December 2, 2011 <?php //Start sessionsession_start(); //Include database connection detailsrequire_once('config.php'); //Array to store validation errors$errmsg_arr = array(); //Validation error flag$errflag = false; //Connect to mysql server$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);if(!$link) { die('Failed to connect to server: ' . mysql_error());} //Select database$db = mysql_select_db(DB_DATABASE);if(!$db) { die("Unable to select database");}//$email = $_SESSION['SESS_EMAIL']; //Select everythingmysql_query("SELECT * FROM members")or die(mysql_error()); //Update the hourly moneymysql_query("UPDATE members SET money=money+moneyhourly")?> try that (I've jsut removed where email = email)then load the page manually. is it updating anything/one result/zero result?In your config file does it check to see if the user is logged in or? Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 2, 2011 Author Share Posted December 2, 2011 Config.php isn't that special... <?phpdefine('DB_HOST', 'localhost'); define('DB_USER', 'myname'); define('DB_PASSWORD', 'password'); define('DB_DATABASE', 'mydatabase');?> As far as the code goes with where email is email, pretty much same result. Still adds the moneyhourly to money so don't see any problems with that. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 2, 2011 Share Posted December 2, 2011 In your executecron file you use the file function, which only reads the contents of the file. That's not going to execute the code. Use the include function if you want to execute the code. In the code that you are running as a cron job, do not use the session, remove the session_start function. The session doesn't apply when you're running PHP from a command line. You may want to also check to make sure that code is running from the command line, if it's just a regular PHP script then someone can figure out the address and keep refreshing it to give everyone as much money as they want. There are several examples in the comments on this page to check if PHP is running on a command line versus a web server: http://www.php.net/manual/en/features.commandline.php Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 3, 2011 Author Share Posted December 3, 2011 It works! I indeed had to do the include instead of file And thanks for the tip about the command line. I'll have to check that out Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 4, 2011 Author Share Posted December 4, 2011 pff more problems If I add mysql_query("UPDATE members SET health=health+3"); It works! But every user has a maxhealth aswell, which cannot be exceeded. So I need to use an IF value, what I tried (among many other things) if (mysql_query("SELECT health FROM members") < mysql_query("SELECT healthmax FROM members") ) {mysql_query("UPDATE members SET health=health+3");}if (mysql_query("SELECT health FROM members") > mysql_query("SELECT healthmax FROM members") ) {mysql_query("UPDATE members SET health=healthmax");} But that doesn't work Does anyone have any ideas? Link to comment Share on other sites More sharing options...
JamesB Posted December 4, 2011 Share Posted December 4, 2011 this might work using 1 query: UPDATE members SET health = IF(health < healthmax, health + 3, IF(health > healthmax, healthmax, health)) keep in mind that mysql_query() will not return the health amount, it will return a resource or boolean false: http://php.net/manual/en/function.mysql-query.php "For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error." to get the value(s) from a resource, you need to use mysql_fetch_row, mysql_fetch_assoc, or mysql_fetch_array. Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 4, 2011 Author Share Posted December 4, 2011 Hm it's getting somewhere.... When I put the health on 10/100 it goes to 13... but if i do it again it goes from 13 to 100? mysql_query("UPDATE members SET health = IF(health < healthmax, health + 3, IF(health > healthmax, healthmax, health))")or die(mysql_error()); Shouldnt the first IF() be closed before the other IF? Link to comment Share on other sites More sharing options...
JamesB Posted December 4, 2011 Share Posted December 4, 2011 well not really, the idea was that the 2nd IF() only gets ran if the result of the 1st IF() was false. i've never actually used 2 IF()s like that in SQL before, but i have in php/js and the logic works fine. after the health has been set to 13, are you sure the healthmax is more than 13? Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 5, 2011 Author Share Posted December 5, 2011 Ah I see about using the IF() function, thanks for clearing that up. And yes healthmax remains 100... its weird,When I put health on 1 it goes to 4, from 4 it goes to hundredWhen I put health on 2 it goes to 100When I put health on 3 it goes to 100When I put health on 4 it goes to 100When I put health on 5 it goes to 100When I put health on 6 it goes to 100....7, 8, 9 also 100...I tried 10 again, it goes to 13, then to 100...I tried it all the way to 17... and from 11-17 all go to 100 again. So only 1 and 10 seem to be working? 20 also didn't work correctly. Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 5, 2011 Author Share Posted December 5, 2011 Ah man, I figured it out just moments after I posted the above comment! I had set it to VARCHAR, latin1_swedish_ci while it had to be INT instead of VARCHAR haha sorryyyEDIT:Wait, almost! When I put 99 as health, and add 3 it goes to 102 instead of 100! Link to comment Share on other sites More sharing options...
JamesB Posted December 5, 2011 Share Posted December 5, 2011 try taking off 3 from the 2 healthmax checks: UPDATE members SET health = IF(health < healthmax - 3, health + 3, IF(health > healthmax - 3, healthmax, health)) Link to comment Share on other sites More sharing options...
justsomeguy Posted December 5, 2011 Share Posted December 5, 2011 How about 2 queries: UPDATE members SET health=health+3UPDATE members SET health=maxhealth WHERE health > maxhealth Link to comment Share on other sites More sharing options...
Dubbeldam Posted December 5, 2011 Author Share Posted December 5, 2011 Thanks guys! It works Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.