Intrest Posted May 4, 2009 Share Posted May 4, 2009 Hey there, I'm working on a C++ project, a small multiplayer game, as the application starts up in pings mydomain.com/start.php. and as it shuts down it pings mydomain.com/stop.php I'm trying to get start.php to add 1 to the current number in playersonline.txt and stop.php to minus one from the current number in playersonline.txt And then I think by using fopen I can read playersonline.txt in my index page. Any idea about how I'd do this(code wise) since I don't really know PHP too well. Thanks, Andrew Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/ Share on other sites More sharing options...
gevans Posted May 4, 2009 Share Posted May 4, 2009 Take a look at fopen, and go from there http://uk2.php.net/manual/en/function.fopen.php Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825499 Share on other sites More sharing options...
Intrest Posted May 4, 2009 Author Share Posted May 4, 2009 Yeah but how would I add 1 to the number in the file? Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825504 Share on other sites More sharing options...
gevans Posted May 4, 2009 Share Posted May 4, 2009 That's why I said 'and go from there' Using the other functions available you can assign the content of the file to a variable. Increment the value of that variable, write over the file with the new value, close the file. Just thought you might like to work it out yourself Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825507 Share on other sites More sharing options...
Intrest Posted May 4, 2009 Author Share Posted May 4, 2009 Alright, I''ll give it a shot, I'll come back later if I can't work it out. Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825509 Share on other sites More sharing options...
gevans Posted May 4, 2009 Share Posted May 4, 2009 No worries, if you're writting your game in C++ this should be fairly straight forward, just work your way through the filesystem functions Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825510 Share on other sites More sharing options...
Intrest Posted May 4, 2009 Author Share Posted May 4, 2009 In C++ +=1 would add 1 to the current number, in PHP it seems to just make it go to 11111111. Here is the code I'm using(from the example of fwrite): <?php $filename = 'test.txt'; $somecontent +=1; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ?> Thanks, Andrew Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825516 Share on other sites More sharing options...
gevans Posted May 4, 2009 Share Posted May 4, 2009 $your_number++ that's increment Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825517 Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 WHat code are you using? In PHP its: $var += 1; Reading the documention will help you follow the correct syntax http://uk.php.net/manual/en/language.operators.assignment.php Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825518 Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 Yeah, it has to be an int though. If it's a string, it'll just append it. Try doing this: echo var_dump($var); //and if it's a string try this (int)$var += 1; Also, I know this isn't what you asked, but if the game was shut down unexpectedly, the PHP script wouldn't be called, so you'd have players online that wouldn't actually beonline. Wouldn't it be better to routinely open the script say every ten minutes, which updated a timestamp for that user? Then you can deduce whether they're online or not. That's what I do for the "who's online" thing on my site. Works pretty well. Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825523 Share on other sites More sharing options...
Intrest Posted May 4, 2009 Author Share Posted May 4, 2009 The code: <?php $filename = 'test.txt'; $somecontent +=1; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ?> Just seems to add 1's and not increment one number, like 1,2,3,4,5 and @jackpf: Yes, but for now I'm just trying to get the basics working. Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825524 Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 variables do not hold their value once a script has finished being parsed. So everytime your script is run all this line is doing $somecontent +=1; is setting $somecontent to the value of 1. After that you're appending this value to the contents of test.txt. This is why you get a series of 1's not 123456 etc. What you'll want to do is this: <?php $filename = 'test.txt'; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a+')) { echo "Cannot open file ($filename)"; exit; } // read contents of the file $contents = fread($handle, filesize($filename)+1); // get the last number $last_num = substr($contents, -1); // increment last number by 1 $last_num += 1; // Write $last_num to our opened file. if (fwrite($handle, $last_num) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($last_num) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825532 Share on other sites More sharing options...
Intrest Posted May 4, 2009 Author Share Posted May 4, 2009 with that I get 12345678910 and then 12345678910 again if I load add.php 20 times. Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825534 Share on other sites More sharing options...
matt.sisto Posted May 4, 2009 Share Posted May 4, 2009 I pulled this script for you from a tutorial on google, incrementing by 1 is easy, and I think you would need to write an if statement to implement the increment, if(ping){ $x++}. Hope this sort of helps. $x = 4; echo "The value of x with post-plusplus = " . $x++; echo "<br /> The value of x after the post-plusplus is " . $x; Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825537 Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 yes the script has a flaw in it. its because this line: $last_num = substr($contents, -1); Only grabs the last character from the file. So when 10 gets written to the file, The next time you run the script, 0 will be the last character and this is why the counter restarts again. A better way would be <?php $filename = 'test.txt'; $num = file_get_contents($filename); $num += 1; if (is_writable($filename)) { // finally write new contents to test.txt file_put_contents($filename, $num); echo "Success, wrote ($num) to file ($filename)"; } else { echo "The file $filename is not writable"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825538 Share on other sites More sharing options...
Intrest Posted May 4, 2009 Author Share Posted May 4, 2009 Alright that works! But if I ping it from C++ will it still work? Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825544 Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 Yes it should do. However why didnt you use C++ for this, if thats all you're doing. Quote Link to comment https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.