Jump to content

Problem with php code and refresh


Phpfr3ak

Recommended Posts

The issue is that if you f5 once attacking another player you can attack them again without any error message... Below is the code, is there a way to prevent the refresh from making the attack happen again?

<?php

$sql = "SELECT * FROM messages WHERE to_username = '$defender_info[username]' AND from_username = '$playerdata[username]' AND type = 'Combat' ORDER BY id DESC LIMIT 1";
$que = mysql_query($sql) or die(mysql_error());
$res = mysql_fetch_array($que);

$now = time();
$lastattack = strtotime($res['timestamp']);
$timesince = $now - $lastattack;

$nextatk = round((10800 - $timesince) / 60);

if($timesince < 10800){
    echo "You can only attack the same player once every 3 hours.<br><br> You can attack this player again in $nextatk minutes.";
}

else{

$sql = "SELECT hits FROM users WHERE id = $playerID";
$que = mysql_query($sql) or die(mysql_error());
$res = mysql_fetch_array($que);

$hits = $res['hits'];
if($hits < 1){
        echo "You have no available hits. New hit allowances are granted every ten minutes on the :05's";
    }
    else{
?>

Link to comment
Share on other sites

The best way to deal with this is to log the attack in it's own table called Attack_Log or whatever.

Fields called attack_id, attacker_id, defender_id, attack_time.

Then insert the data after the attack has took place, when the attack is completed and logged.

 

Search the log for the attacker_id and defender_id and then compare the attack time to the current time and check the difference, if it is less than allowed cancel attack and show error message of some sort, if not the continue with attack.

 

How that makes sense?

 

Regards, Paul.

Link to comment
Share on other sites

Thats exactly how i have it set up at the moment via the message is sent as an attack message instead of say player message... yet its still not working if i leave the page and go to attack again the error message is there... yet if i refresh the page after the attack i can use all the hits/as many attacks as i want.

Link to comment
Share on other sites

Are you actually inserting the attack into the log on the same page as  you are attacking another player/NPC?

 

If so, show us that code also, it may be that it is not inserting correctly.

 

I'm not too sure atm, with only a small snippet of code on hand.

 

Regards, Paul.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.