ferrin Posted May 5, 2010 Share Posted May 5, 2010 i really need some help, i've tried everything and as someone who hasn't been coding long at all, i'm stumped. issue one: my project is simple and designed to be fun as well as help me learn coding. basically, i'm making a small, text based rpg pet game. think pokemon. you start out at a page where you click to 'walk around' and random pokemon pop up. when you click a pokemon, you're directed to a page that informs you you've caught it, and then saves the information to the database. this is my first remotely big coding thing. i'm really happy with how it's gone so far. i just -cannot- find a way to do one stupid, simple thing. i need to send a session or cookie or basically, find some way of sending one variable (the petsname variable, which of course indicates the name of the creature) to the next page. the next page (assign.php) is designed to take the information in the variable and assign it to a session (session(petsname)), then it automatically redirects to another page. you don't even see the page that assigns the session, so it looks like you go from the first page (explore.php) where you found the pet, to the page where it assigns the attributes and adds to the database, then tells you about your pet (gotpet.php). the reason i need to send the variable is because the gotpet.php page has a switch (multidemensional?) that has the data of all the pokemon stored. depending on the variable sent to it, the information is stored. what i need to do: make so that the user can only get the pokemon once. ie: can't refresh the gotpet.php page or whatever to have the information sent twice. this is why i want to insert a cookie or session or something into the hyperlink associated with the pet. the cookie or session must be destroyed upon having received the pet. it needs to be secure, of course. second issue: if you look at the explore.php page, you'll notice an ugly iframe chat thing. i had originally wanted to put the chat into a div, couldn't figure out how (i was told to use ajax, but ajax is far beyond my current level of code abilities) without it refreshing the entire page vs. the div only, so. yeah. it's ugly and to admit to my utter stupidity, i have no clue how to make the chat smaller/fit/look nice. attached is a zip of the entire chat directory contents. the chat is made from a tutorial, some parts i don't understand, some i do. i would GREATLY appreciate any help from anyone. seriously. i've been stuck on this for days and it's driving me crazy. i've exhaustively googled and asked people on other help sites, but they generally seem to be irritated at me for the fact that i don't have the level of skill for some of this stuff. i am a willing learner, i'm not just asking someone to do this for me. i apologize for the long writing here, but i'm extremely frustrated and this is the last place i've found to try and get some help. thank you in advance. following is the code for the pet pages: explore.php getpet.php assign.php explore.php NOTE: i would like to replace the individual links (charmander.php, etc) with just the assign.php once i have this figured out. right now it defaults to individual creature pages, which are hardly secure and take up too much space. the only one currently working, though, is the charmander.php page. http://fiendfashion.biz/ferrin/explore.php <?php // include "db.php"; ?> <html> <head><link rel="stylesheet" href="style.css"> <title><?php echo $_SESSION['username']; ?>'s Travels in Neverland</title> </head> <body> <iframe src="chat/source/index.php" width="350px" height="270px" style="position:absolute; top:10px; left:5px"> Your browser does not support iframes. Chat may be accessed <a href="chat/source/index.php" target="new">here</a>. </iframe> <?php // random gen function function dodosrandgen() { // set number of links you want to show at once $show_link = 1; // separate the links by, i.e. comma would be ","; $separator = ""; // $links[0][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/charmander.php\"><img src=\"images/charmander.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[0][priority] = 1; $links[1][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/vulpix.php\"><img src=\"images/vulpix.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[1][priority] = 1; $links[2][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/weedle.php\"><img src=\"images/weedle.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[2][priority] = 10; $links[3][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/caterpie.php\"><img src=\"images/caterpie.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[3][priority] = 10; $links[4][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/rattata.php\"><img src=\"images/rattata.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[4][priority] = 10; $links[5][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/spearow.php\"><img src=\"images/spearow.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[5][priority] = 10; $links[6][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><img src=\"images/null.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></td></tr></table>"; $links[6][priority] = 45; $links[7][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><img src=\"images/null.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></td></tr></table>"; $links[7][priority] = 45; $links[8][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/jigglypuff.php\"><img src=\"images/jigglypuff.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[8][priority] = 1; $links[9][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/oddish.php\"><img src=\"images/oddish.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[9][priority] = 9; $links[10][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/pidgey.php\"><img src=\"images/pidgey.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[10][priority] = 9; $links[11][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/ekans.php\"><img src=\"images/ekans.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[11][priority] = 9; $links[12][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/hoothoot.php\"><img src=\"images/hoothoot.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"50\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[12][priority] = 9; $links[12][link] = "<table width=\"250\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"http://fiendfashion.biz/ferrin/fairy1.php\"><img src=\"images/fairy1.gif\" border=\"0\" align=\"right\" width=\"50\" height=\"60\" style=\"filter:alpha(opacity=40)\" onMouseover=\"high(this)\" onMouseout=\"low(this)\"></a></td></tr></table>"; $links[12][priority] = 5; // HERE COMES THE BODY if($links) { for($i = -1; $i < count($links); $i++) { for($j = 0; $j < $links[$i][priority]; $j++) { $newlinksindex = count($newlinks) + 1; $newlinks[$newlinksindex] = $links[$i][link]; } } } // end of if link exist // debug if($show_link >= count($newlinks)) $show_link = count($newlinks); $showarray[0] = ""; $showindex = 0; // Select a random element from the new array do { $randindex = rand(0, count($newlinks)); if(!in_array($newlinks[$randindex], $showarray) && $newlinks[$randindex] != "") { $showarray[$showindex] = $newlinks[$randindex]; //print_r($showarray); $showindex++; } }while(count($showarray) <= $show_link); // finally print out for($i = 0; $i < $show_link; $i++ ) { if($show_link == 1 || (($i + 1) == $show_link)) print $showarray[$i]; else print $showarray[$i].$separator; } //print_r($ranarray); //print_r($links); //print_r($newlinks); } // end of the dodosrandgen function ?> <div class="divider" id="randomright"> <?php dodosrandgen(); ?> </div> <div class="divider" id="random"><p align="center"> <br><br><u>Walking through Neverland</u><br><br> What a curious place. You can <a href="explore.php">look around</a> some more,<br />or if you see something you want, pick it up.<br /><br /> <br /><a href="inventory">Your Things</a> | <a href="pets">Your Pets</a> | <a href="http://www.fiendfashion.biz/game/home.php">Your Profile</a> | <a href="logout.php">Logout</a></p></div> <div class="divider" id="randomleft"> <?php dodosrandgen(); ?> </body> </html> getpet.php http://fiendfashion.biz/ferrin/test-gotpet.php <?php session_start(); $_SESSION['loggedin'] . '<br />'; $_SESSION['gotpet'] . '<br />'; $_SESSION['petsname'] . '<br />'; $petsname = $_SESSION['petsname']; //include "db.php"; // 'get pet' form echo "<head><link rel=\"stylesheet\" href=\"style.css\"></head> <body> <br><br> <div class=\"divider\" id=\"getthepet\" align=\"center\"> <img src=\"images/charmander.gif\" alt=\"Char! Char!\"> <form action=\"test-gotpet.php\" method=\"post\" /> <input type=\"radio\" name=\"userchoice\" value=\"Getpet\" title=\"userchoice\" /> Get 'Em!<br /><br /> <input type=\"radio\" name=\"userchoice\" value=\"Leave\" title=\"userchoice\" /> Leave<br /><br /> <input type=\"submit\" value=\"submit\" name=\"submit_btn\"></form> </div> </body> \n"; ini_set('display_errors', 1); error_reporting(E_ALL); if ( $_POST && isset($_POST['userchoice']) ) { $userChoice = htmlspecialchars($_POST['userchoice']); switch( $userChoice ) { case 'Getpet': // // now find out which pet was selected // and list info // using a switch function switch ( $petsname ) { case "caterpie": $petinfo = 'A small caterpillar'; $type = 'bug'; $attack1 = 'stringshot'; $attack2 = 'tackle'; break; case "charmander": $petinfo = 'A firey salamandar'; $type = 'fire'; $attack1 = 'growl'; $attack2 = 'scratch'; break; case "ekans": $petinfo = 'a vicious snake'; $type = 'poison'; $attack1 = 'tackle'; $attack2 = 'growl'; break; default: echo "ERROR - unknown creature - please inform admin. "; break; } // change this later- username $username = 'guest'; // add age $age = rand(1, 3); // //get random stats $str = rand(10, 13); $def = rand(8, 11); // // determine hp/mp by def/str - mp first // if($str == '13') // set high mp { $mp = rand(13, 15);} elseif($str == '12') { $mp = rand(11, 13);} // set medium mp else { $mp = rand(9, 11);} // // next determine hp // if($def == '13') // set high mp { $hp = rand(13, 15);} elseif($def == '12') { $hp = rand(11, 13);} // set medium mp else { $hp = rand(9, 11);} // // get gender $gen = rand(1, 2); if($gen == '1'){$gender = 'female';} else {$gender = 'male';} // // save info to database // $con = mysql_connect ("gamelogin.db.6086537.hostedresource.com","gamelogin","-----") or die(mysql_error()); $db = mysql_select_db("gamelogin",$con) or die(mysql_error()); $sql = "INSERT INTO `user_pets` (`type`, `petname`, `username`, `age`, `hp`, `mp`, `str`, `def`, `attack1`, `attack2`) VALUES ('$type', '$petsname', '$username', '$age', '$hp', '$mp', '$str', '$def', '$attack1', '$attack2')"; mysql_query( $sql ) or die ( mysql_error() ); // // tell user pet info // echo "<div class=\"divider\" id=\"gotthepet\" align=\"center\">"; echo "You got a $petsname!<br><br>"; // announces user got the creature echo "{$petinfo}<br><br>"; echo "Your new pet, {$petsname}, is a {$type}-type!<br>"; echo "It is {$age} years old.<br>"; echo "It's abilities are {$attack1} and {$attack2}.<br>"; echo "{$str} is it's strength and {$def} is it's defense.<br>"; echo "It's health is {$hp}, while it has {$mp} move points.<br>"; echo "{$petsname}'s gender is {$gender}.<br><br>"; echo "<a href=\"explore.php\">Leave</a>"; echo "</div>"; // // ^-- end add pet code // //need to add txt file // break; echo "</div>"; case 'Leave': // do something header ('explore.php'); break; } }?> assign.php <?php session_start(); $_SESSION['loggedin'] = "logged"; $_SESSION['gotpet'] = "gotpet"; $_SESSION['petsname'] = "charmander"; header( 'Location: test-gotpet.php' ) ; ?> [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
phpchamps Posted May 5, 2010 Share Posted May 5, 2010 Hello, For your first issue... i think you want to prevent duplicate submission of data. If this is what you want to do then you will have to use PRG pattern. Please go through below mentioned link. http://en.wikipedia.org/wiki/Post/Redirect/Get For your second issue.. i am sorry but right now i dont have time to read it.. will post answer once i read it.... Hope it helps.... Quote Link to comment Share on other sites More sharing options...
ferrin Posted May 5, 2010 Author Share Posted May 5, 2010 oh, okay, i see. so basically, instead of using POST, i send a GET command with a header location redirect from the creature url on explore.php, to the assign.php, which then redirects the user to getpet.php? or in doing this, would i no longer need the inbetween? the assign.php? it would be great if clicking the url of the creature could automatically assign the $petsname variable to the pet name, then direct them to the getpet.php, which would retrieve the variable, assign info accordingly, and then refreshing the page would send them back to explore.php. the current problem is that if a user refreshes the page, they can get the creature again. is PRG secure? that and i have no clue how to write out the above code. if you look at the structure of the creature links, when i tried to add any further commands it gives me a t-string and whitespace error. i appreciate the help, by the way. thank you. Quote Link to comment Share on other sites More sharing options...
ferrin Posted May 5, 2010 Author Share Posted May 5, 2010 i can't figure out how to edit my last post, so please forgive me for posting twice. but if i use the PRG method, wouldn't it still be possible for say, someone to right-click and open a link in a new tab, then click it again on the main page, thus getting two creatures out of one link? ie, cheating? Quote Link to comment 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.