Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

More help in PHP


t0xikc0mputer's Avatar
Member
0 0

Hello all,

Essentially I am learning how to do the HBH timed challenges with cURL. I have read The article on it, but I am trying to convert the code to an array so that I can divide it within the program. (Each line of HTML would be an element of the array) right now, the issue is that all the code is one element. Does anyone know how to set it so that it breaks at a newline? I know how to do this in perl, but that is about it.

Also, I realize that this may not be the correct way to accomplish the challenges, I would be happy for you to let me know that, but I would still like an answer so that I can learn from this experience in more ways than one.

Thanks in advance,

t0xik


ghost's Avatar
0 0

t0xikc0mputer wrote: Also, I realize that this may not be the correct way to accomplish the challenges, I would be happy for you to let me know that, but I would still like an answer so that I can learn from this experience in more ways than one.

You can explode by the carriage return character "\n". $myarray = explode("\n", $mystring);


ynori7's Avatar
Future Emperor of Earth
0 0

Henty wrote: You can explode by the carriage return character "\n".

Actually that's a line feed character.


t0xikc0mputer's Avatar
Member
0 0

Thank you everyone for your wonderfully helpful responses. I have been able to accomplish the beginning part of my task, but I am faced with another problem: when I try to make a "fusion_user" cookie, nothing comes up on the page. But when I take that cookie, and only that one away, the page is up and running, but not logged in. I use a 000webhost server. Here is the code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.hellboundhackers.org/challenges/timed/timed1/index.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIE, /*"fusion_user=*****.********;*/"fusion_lastvisit=***********; fusion_visited=TRUE; PHPSESSID=**************; ");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15 ( .NET CLR 3.5.30729)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
curl_close($ch);
echo $page;
?>```

When I take away the comments around the fusion_user cookie, the page loads fine, but otherwise nothing shows. No error, nothing.

Thanks very much for the help already. I hope to be able to complete the timed challenges soon.

~t0xik

EDIT: Also, Moshbat, I didn't quite understand your "regex" thing, but I have looked into it.

ghost's Avatar
0 0

t0xikc0mputer wrote: When I take away the comments around the fusion_user cookie, the page loads fine, but otherwise nothing shows. No error, nothing.

EDIT: Also, Moshbat, I didn't quite understand your "regex" thing, but I have looked into it.

That script works fine for me with the fusion_user cookie still in place. Try getting fresh cookies again and make sure you delete the extra double quotation after fusion_user (but I assume you just put it their after commenting out).

If that doesn't work, I can only suggest installing php locally to test again. If you're using windows, WAMP is the simplest package.

Keep working at regex too. It's not just a PHP thing. Once you learn it, you can carry it across into most of the other big languages for string parsing.

EDIT:

ynori7 wrote: Actually that's a line feed character.

You're correct, \n is the line feed, \r is the carriage return.


ADIGA's Avatar
Member
0 0

Regarding the cookie thing …. why don't you just login using curl with the options of cookie file and cookie jar added, that way you done need to set any cookie as it will be stored in the file and used at any page you visit on the same script run.


ghost's Avatar
0 0
<?php
include "/home/froger/public_html/_private/exampleconnect.php";

$sql = mysql_fetch_array(mysql_query("SELECT subno, currenthp, clan, x, y, z FROM example WHERE subno='{$subno}'"));
$map = mysql_fetch_array(mysql_query("SELECT tx, ty, tz, ID, Zone FROM Test WHERE X='{$sql['x']}' AND Y='{$sql['y']}' AND Z='{$sql['z']}'"));

if ($map['ID'] == 'teleport')
  {
  if ($map['Zone'] != 'Cave Entrance')
    {
    $query = mysql_query("UPDATE example SET x='{$map['tx']}', y='{$map['ty']}', z='{$map['tz']}' WHERE subno='{$subno}'");
    $sql = mysql_fetch_array(mysql_query("SELECT subno, currenthp, x, y, z FROM example WHERE subno='{$subno}'"));
    } else {
    $query = mysql_query("UPDATE example SET x='{$map['tx']}', y='{$map['ty']}', z='{$map['tz']}' WHERE subno='{$subno}'");
    $sql = mysql_fetch_array(mysql_query("SELECT subno, currenthp, x, y, z FROM example WHERE subno='{$subno}'"));
    }
  }

$back = imagecreate(72,72);
$player = imagecreatefromgif('http://example.com/images/player3.gif');


$rowz = mysql_query("SELECT X, Y, Z, Zone, townid FROM Example2 WHERE (X BETWEEN " . ($sql['x']-4) . " AND " . ($sql['x']+4) . ") AND (Y BETWEEN " . ($sql['y']-4) . " AND " . ($sql['y']+4) . ") AND Z='{$sql['z']}' ORDER BY Y, X");

$i = 0;
$l = 0;

while ($img = mysql_fetch_array($rowz))
  {
  if ($y == NULL)
    {
    $y = $img['Y'];
    }
  if ($y != $img['Y'])
    {
    $y++;
    $i++;
    }
    $loc = mysql_fetch_array(mysql_query("SELECT subno FROM example WHERE x='{$img['X']}' AND y='{$img['Y']}' AND z='". $sql['z'] . "'"));
    $clan = mysql_fetch_array(mysql_query("SELECT subno FROM clan WHERE name='{$sql['clan']}' AND subno='{$loc['subno']}'"));
    $watch = mysql_fetch_array(mysql_query("SELECT subno FROM watch WHERE subno='{$loc['subno']}' AND mysubno='{$subno}'"));
    $monsterloc = mysql_fetch_array(mysql_query("SELECT monstername, subno FROM MonsterLocation WHERE x='{$img['X']}' AND y='{$img['Y']}' AND z='{$sql['z']}'"));
    $treasure = mysql_fetch_array(mysql_query("SELECT id FROM lootlocation WHERE x='{$img['X']}' AND y='{$img['Y']}' AND z='". $sql['z'] . "'"));
    
    if (($monsterloc['subno'] == NULL) && ($sql['currenthp'] > 0))
      {
      $id = mysql_fetch_array(mysql_query("SELECT uniqueid FROM MonsterLocation WHERE x='{$img['X']}' AND y='{$img['Y']}' AND z='{$img['Z']}'"));
      $attack = mysql_query("UPDATE MonsterLocation SET subno='{$subno}' WHERE uniqueid='{$id['uniqueid']}'");
      }

    $z = $i * 8;
    $o = $l * 8;
    $l++;
      
    if ($l >= 9)
      {
      $l = 0;
      }

    if (($loc['subno'] == NULL) && ($clan['subno'] == NULL) && ($watch['subno'] == NULL) && ($monsterloc['monstername'] == NULL) && ($treasure['id'] == NULL) && ($img['Zone'] != 'Town'))
    switch ($img['Zone'])
      {
      case 'Ground':
         if ($grass == NULL)
           {
           $grass = imagecreatefromgif('http://example.com/images/grassyground.gif');
           } 

         $image = imagecopymerge($back, $grass, $o, $z, 0, 0, 8, 8, 100);  
         break;
      case 'Dead Grass':
        if ($dgrass == NULL)
          {
          $dgrass = imagecreatefromgif('http://example.com/images/deadgrass.gif');
          } 

        $image = imagecopymerge($back, $dgrass, $o, $z, 0, 0, 8, 8, 100);      
        break;
      case 'Ladder':
        if ($ladder == NULL)
          {
          $ladder = imagecreatefromgif('http://example.com/images/ladder.gif');
          } 

        $image = imagecopymerge($back, $ladder, $o, $z, 0, 0, 8, 8, 100);  
        break;
      case 'Pillar':
        if ($pillar == NULL)
          {
          $pillar = imagecreatefromgif('http://example.com/images/pillar.gif');
          } 

        $image = imagecopymerge($back, $pillar, $o, $z, 0, 0, 8, 8, 100); 
        break;
      case 'Sign':
        if ($sign == NULL)
          {
          $sign = imagecreatefromgif('http://example.com/images/sign.gif');
          } 

        $image = imagecopymerge($back, $sign, $o, $z, 0, 0, 8, 8, 100);  
        break;
      case 'Bridge':
        if ($bridge == NULL)
          {
          $bridge = imagecreatefromgif('http://example.com/images/bridge.gif');
          } 

        $image = imagecopymerge($back, $bridge, $o, $z, 0, 0, 8, 8, 100); 
        break;
      case 'Rock':
        if ($rock == NULL)
          {
          $rock = imagecreatefromgif('http://example.com/images/rock.gif');
          } 

        $image = imagecopymerge($back, $rock, $o, $z, 0, 0, 8, 8, 100); 
        break;
      case 'Mountain': 
        if ($mountain == NULL)
          {
          $mountain = imagecreatefromgif('http://example.com/images/mountain.gif');
          } 

        $image = imagecopymerge($back, $mountain, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Cave Wall': 
        if ($cavew == NULL)
          {
          $cavew = imagecreatefromgif('http://example.com/images/mountain.gif');
          } 

        $image = imagecopymerge($back, $cavew, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Trail':
        if ($bridge == NULL)
          {
          $bridge = imagecreatefromgif('http://example.com/images/bridge.gif');
          } 

        $image = imagecopymerge($back, $bridge, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Portal':
        if ($portal == NULL)
          {
          $portal = imagecreatefromgif('http://example.com/images/portal.gif');
          } 

        $image = imagecopymerge($back, $portal, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Water':
        if ($water == NULL)
          {
          $water = imagecreatefromgif('http://example.com/images/water.gif');
          } 

        $image = imagecopymerge($back, $water, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Small Tree':
        if ($stree == NULL)
          {
          $stree = imagecreatefromgif('http://example.com/images/smalltree.gif');
          } 

        $image = imagecopymerge($back, $stree, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Large Tree':
        if ($ltree == NULL)
          {
          $ltree = imagecreatefromgif('http://example.com/images/largetree.gif');
          } 

        $image = imagecopymerge($back, $ltree, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Small Pine Tree':
        if ($sptree == NULL)
          {
          $sptree = imagecreatefromgif('http://example.com/images/smallpinetree.gif');
          } 

        $image = imagecopymerge($back, $sptree, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Dragonwood':
        if ($sptree == NULL)
          {
          $sptree = imagecreatefromgif('http://example.com/images/smallpinetree.gif');
          } 

        $image = imagecopymerge($back, $sptree, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Large Pine Tree':
        if ($lptree == NULL)
          {
          $lptree = imagecreatefromgif('http://example.com/images/largepinetree.gif');
          } 

        $image = imagecopymerge($back, $lptree, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Rubble':
        if ($rubble == NULL)
          {
          $rubble = imagecreatefromgif('http://example.com/images/cavewallrubble.gif');
          } 

        $image = imagecopymerge($back, $rubble, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Dock':
        if ($dock == NULL)
          {
          $dock = imagecreatefromgif('http://example.com/images/dock.gif');
          } 

        $image = imagecopymerge($back, $dock, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Lava':
        if ($lava == NULL)
          {
          $lava = imagecreatefromgif('http://example.com/images/lava.gif');
          } 

        $image = imagecopymerge($back, $lava, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Gate':
        if ($gate == NULL)
          {
          $gate = imagecreatefromgif('http://example.com/images/gate.gif');
          } 

        $image = imagecopymerge($back, $gate, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Dead Tree':
        if ($deadt == NULL)
          {
          $deadt = imagecreatefromgif('http://example.com/images/deadtree.gif');
          } 

        $image = imagecopymerge($back, $deadt, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Night Ground':
        if ($deadt == NULL)
          {
          $nightg = imagecreatefromgif('http://example.com/images/nightground.gif');
          } 

        $image = imagecopymerge($back, $nightg, $o, $z, 0, 0, 8, 8, 100);
        break;
      }

    if (($treasure['id'] != NULL) && ($monsterloc['monstername'] == NULL) && ($loc['subno'] == NULL))
      {
      if ($treasures == NULL)
        {
        $treasures = imagecreatefromgif('http://example.com/images/chest.gif');
        } 

      $image = imagecopymerge($back, $treasures, $o, $z, 0, 0, 8, 8, 100);
      }

    if ($monsterloc['monstername'] != NULL)
    switch ($monsterloc['monstername'])  {
      case 'Rat':
        if ($rat == NULL)
          {
          $rat = imagecreatefromgif('http://example.com/images/rat.gif');
          }
      
        $image = imagecopymerge($back, $rat, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Bear':
        if ($bear == NULL)
          {
          $bear = imagecreatefromgif('http://example.com/images/bear.gif');
          }

        $image = imagecopymerge($back, $bear, $o, $z, 0, 0, 8, 8, 100);
        break;
     case 'Goblin Peon':
        if ($goblinp == NULL)
          {
          $goblinp = imagecreatefromgif('http://example.com/images/goblinpeon.gif');
          }

        $image = imagecopymerge($back, $goblinp, $o, $z, 0, 0, 8, 8, 100);
        break;
     case 'Zombie':
        if ($zombie == NULL)
          {
          $zombie = imagecreatefromgif('http://example.com/images/zombie.gif');
          }

        $image = imagecopymerge($back, $zombie, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Goblin King':
        if ($goblink == NULL)
          {
          $goblink = imagecreatefromgif('http://example.com/images/goblinking.gif');
          }

        $image = imagecopymerge($back, $goblink, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Goblin Warrior':
        if ($goblinw == NULL)
          {
          $goblinw = imagecreatefromgif('http://example.com/images/goblinwarrior.gif');
          }

        $image = imagecopymerge($back, $goblinw, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Ghost':
        if ($ghost == NULL)
          {
          $ghost = imagecreatefromgif('http://example.com/images/ghost.gif');
          }

        $image = imagecopymerge($back, $ghost, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Zombie Assassin':
        if ($zombiea == NULL)
          {
          $zombiea = imagecreatefromgif('http://example.com/images/zombieassassin.gif');
          }

        $image = imagecopymerge($back, $zombiea, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Green Dragon':
        if ($greend == NULL)
          {
          $greend = imagecreatefromgif('http://example.com/images/greendragon.gif');
          }

        $image = imagecopymerge($back, $greend, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Goblin Sorcerer':
        if ($goblins == NULL)
          {
          $goblins = imagecreatefromgif('http://example.com/images/goblinsorcerer.gif');
          }

        $image = imagecopymerge($back, $goblins, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Orc Warrior':
        if ($orcw == NULL)
          {
          $orcw = imagecreatefromgif('http://example.com/images/orcwarrior.gif');
          }

        $image = imagecopymerge($back, $orcw, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Fire Wizard':
        if ($firewizard == NULL)
          {
          $firewizard = imagecreatefromgif('http://example.com/images/firewizard.gif');
          }

        $image = imagecopymerge($back, $firewizard, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Demon Imp':
        if ($demonimp == NULL)
          {
          $demonimp = imagecreatefromgif('http://example.com/images/demonimp.gif');
          }

        $image = imagecopymerge($back, $demonimp, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Revanant Demon':
        if ($revanantd == NULL)
          {
          $revanantd = imagecreatefromgif('http://example.com/images/revanantdemon.gif');
          }

        $image = imagecopymerge($back, $revanantd, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Ancient Dragon':
        if ($ancientd == NULL)
          {
          $ancientd = imagecreatefromgif('http://example.com/images/ancientdragon.gif');
          }

        $image = imagecopymerge($back, $ancientd, $o, $z, 0, 0, 8, 8, 100);
        break;
      case 'Snake':
        if ($snake == NULL)
          {
          $snake = imagecreatefromgif('http://example.com/images/snake.gif');
          }

        $image = imagecopymerge($back, $snake, $o, $z, 0, 0, 8, 8, 100);
        break;
    }

    if ($loc['subno'] != NULL)
      {
      $hp = mysql_fetch_array(mysql_query("SELECT currenthp FROM example WHERE subno='{$loc['subno']}'"));

      if ($hp['currenthp'] > 0)
        {
        if (($watch['subno'] != NULL) && ($clan['subno'] == NULL))
          {
          if ($player4 == NULL)
            {
            $player4 = imagecreatefromgif('http://example.com/images/watch.gif');
            } 

          $image = imagecopymerge($back, $player4, $o, $z, 0, 0, 8, 8, 100);
          } elseif (($clan['subno'] != NULL) && ($sql['subno'] != $clan['subno'])) {
          if ($player5 == NULL)
            {
            $player5 = imagecreatefromgif('http://example.com/images/clan.gif');
            } 

          $image = imagecopymerge($back, $player5, $o, $z, 0, 0, 8, 8, 100);
          } else {
          if ($sql['subno'] == $loc['subno'])
            {
            $image = imagecopymerge($back, $player, $o, $z, 0, 0, 8, 8, 100);
            } else {
            if ($players == NULL)
              {
              $players = imagecreatefromgif('http://example.com/images/Player.gif');
              } 

            $image = imagecopymerge($back, $players, $o, $z, 0, 0, 8, 8, 100);
            }
          }
        } else {
        if ($corpse == NULL)
          {
          $corpse = imagecreatefromgif('http://example.com/images/corpse.gif');
          } 

        $image = imagecopymerge($back, $corpse, $o, $z, 0, 0, 8, 8, 100);
        }
      }

    if ($img['Zone'] == 'Town')
      {
      if ($img['townid'] == '1')
        {
        if ($tlb == NULL)
          {
          $tlb = imagecreatefromgif('http://example.com/images/topleftbyren.gif');
          } 

        $image = imagecopymerge($back, $tlb, $o, $z, 0, 0, 8, 8, 100);
        }
      if ($img['townid'] == '2')
        {
        if ($trb == NULL)
          {
          $trb = imagecreatefromgif('http://example.com/images/toprightbyren.gif');
          } 

        $image = imagecopymerge($back, $trb, $o, $z, 0, 0, 8, 8, 100);
        }
      if ($img['townid'] == '3')
        {
        if ($blb == NULL)
          {
          $blb = imagecreatefromgif('http://example.com/images/bottleftbyren.gif');
          } 

        $image = imagecopymerge($back, $blb, $o, $z, 0, 0, 8, 8, 100);
        }
      if ($img['townid'] == '4')
        {
        if ($brb == NULL)
          {
          $brb = imagecreatefromgif('http://example.com/images/bottrightbyren.gif');
          } 

        $image = imagecopymerge($back, $brb, $o, $z, 0, 0, 8, 8, 100);
        }
      }
    }
mysql_free_result($rowz);


header("Content-Type: image/gif");
imagegif($back);
imagedestroy($back);
?>

Hello, is there anyway to optimize this code? You see, it just makes a simple 2D image composed of a whole bunch of small 8pix X 8pix images that makes a 9X9 image.. I am using image copy merge to help accomplish this. It makes the image because I have things set in place in my database in X, Y, Z co ordinates.

I have been having problems as of recently though. This script is taking to long to load as more users come to my site. I made this script a long time ago, optimized it as much as I could. Is there something I am not seeing? Am I using the wrong methods to do this? Any tips to lead me in the right direction would be greatly appreciated.


j4m32's Avatar
Member
0 0

I think you've got the wrong thread? Oh well.

No offense, if you can't see or tell why it's so slow with the blatent sledge hammer approach used, you might need to learn a bit more about debugging and developing.

What ever server you're running this on it's going to take a while under most circumstances without some rethinks on a few things, especially on a shared service where CPU time is restricted.

I haven't looked in great detail at your design choices but I took a glance through the source…

It's not going to be speedy even with a few users, even with the improvements I am about to suggest it may not execute very much quicker because it depends on the load on this server / what not.

For starters you're wasting a lot of time regenerating the background, which to all intents and purposes could be made static so long as most of the terrain or whatever is not going to change much.

So instead of enumerating every tile, load the backdrop, and then draw on top the sprites you want. You can make a masked set of sprites and set the transparent colour appropriately. Then you need only enumaerate the users 'online', so to speak, and then you only have to do N itterations rather than X*Y itterations of the drawing loop.

Secondly, surely you can reduce the number of SQL statements and just join a lot of them together or make a more comprehensive table? Or have I missed something?…

Even if you wanted to expand this, movement would still not an issue if you then just have: a fixed 'viewport', an X, Y for the screen centre if the map for each player, that is if it then becomes larger than the viewport you've chosen and then all your script would need to do is place the objects / dynamic crap on the top.

What you can do is make a script that can just generate the backdrop map images server side if you wish. I think it would make it a lot quicker to load. Store them in a directory, then it's easier to manage.

Might be worth using your browsers' network time graph feature to see how long it takes the server to respond from the request, unless you would prefer printing timestamps to a file and reading them…

The final problem - depending how you want this to work - is database synchronisity and envorcing any game rules (if you like). Take user A moving to a square that also user B wishes to move to, which takes prescidence? blah blah. There is probably pleanty more.

Hope this has helped, somewhat. I would have opted for something somewhat more dynamic in that the PHP script would not generate the whole image but rather just handle all the data necessary, but this is a nice concept for reducing load on a client with less processing capability - I see the idea.

Jim,


ghost's Avatar
0 0

I posted here, so not to start another thread when the topic relates to php help. Lol Yea, I obviously understand why this script is taking forever to load lol. One issue is the amount of queries with the while statement. Another issue is as you said, recreating the whole map rather than just replacing what has changed. I have rules implemented for the game when moving around. This is something just to display the image of the game onto a 2D image. Hmmmm I was mainly trying to steer clear of re programming the entire image function. Guess that isn't an option, especially with this CPU intensive script.