Jump to content

Basic PHP help


Intrest

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/156762-basic-php-help/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825516
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825523
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825524
Share on other sites

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";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825532
Share on other sites

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. 8)

$x = 4;
echo "The value of x with post-plusplus = " . $x++;
echo "<br /> The value of x after the post-plusplus is " . $x;

Link to comment
https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825537
Share on other sites

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";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/156762-basic-php-help/#findComment-825538
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.