TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-22-2008, 08:11 PM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 12
Thanks: 0
karq is on a distinguished road
Default Tried to make a guessing game, what do u think?

So I just started a week ago to learn php. Today I tried to make a guessing game.My first tryout :D
What do u think?

PHP Code:
html>
<body>
<form action="opin.php" method="GET">
Vali arv:<input type="text" name="nr"/>
<input type="submit" value="Arva"/>
</form>
<?php
//I used Estonian in this script, U propally wont understand some thing's :D
$number=$_GET["nr"];
$arva=(rand(1,20));
if (isset(
$arva)) {            //Kas genereeriti arv? Kui jh siis jätkame.
if ($number<$arva) {
echo 
"Vähe pakkusid";
    }
else if (
$number>$arva) {
    echo 
"Liiga palju pakkusid";

}
else if (
$Number==$arva) {
     echo 
"Panid täppi!";

}
}
else {
    echo 
"Midagi läks pekki";
}
?>
</body>
</html>
karq is offline  
Reply With Quote
Old 01-22-2008, 08:25 PM   #2 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

One problem, karq, and that is your number (not the guess) doesn't persist. This means that if the number is 1 the first time and I say 3, the returned message is that the number is smaller. So, next time I pick 2 ... only to have it tell me the number is bigger.

Try using a cookie to make the random number persist.

PHP Code:
<?php

// This MUST be at the top before ANY output happens!
if(!isset($_COOKIE['arva'])) {
  
// The zero makes it expire when the browser session terminates.
  
setcookie('arva'rand(1,20), 0);
}
$arva intval($_COOKIE['arva']);
?>
Now, put that at the top and remove the parts within your section that assign values to arva. Now it'll persist between guesses and you can get it right.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-22-2008, 09:28 PM   #3 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Thanks to my ability to speak PHP I can more or less understand the estonian, and you could also use sessions
TlcAndres is offline  
Reply With Quote
Old 01-23-2008, 05:03 PM   #4 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 12
Thanks: 0
karq is on a distinguished road
Default

So I made 2 games 1 with cookies and 1 with sessions.
Here are the codes:
Cookies:
PHP Code:
<?php
if (!isset($_COOKIE["arva"])) {
    
setcookie('arva',rand(1,20),0);
}
$arvas=intval($_COOKIE["arva"]);
?>

<html>
<body>
<center><form action="arvamismang_cookidega.php" method="GET">
Vali arv:<input type="text" name="nr"/>
<input type="submit" value="Arva"/>
</form></center>
<?php
//I used Estonian in this script, U propally wont understand something's :D

$number=$_GET["nr"];
if (isset(
$arvas)) {
    if (
$number<$arvas) {
            echo 
"<center>Paku rohkem</center>";
        }
    if (
$number>$arvas) {
            echo 
"<center>Paku vähem</center>";
        }
    if (
$number==$arvas) {
            echo 
"<center>Panid täppi!!!</center>";

    }
}
else {
    echo 
"<center>Kuskil on viga vist :S</center>";
}

?>
</body>
</html>
Sessions:
PHP Code:
<?php
session_start
();
if (!isset(
$_SESSION["arva"])) {

    
$_SESSION["arva"]=rand(1,20);
}
$arvas=intval($_SESSION["arva"]);
?>

<html>
<body>
<center><form action="arvamismang_cookidega.php" method="GET">
Vali arv:<input type="text" name="nr"/>
<input type="submit" value="Arva"/>
</form></center>
<?php
//I used Estonian in this script, U propally wont understand something's :D
$number=$_GET["nr"];
if (isset(
$arvas)) {
    if (
$number<$arvas) {
            echo 
"<center>Paku rohkem</center>";
        }
    if (
$number>$arvas) {
            echo 
"<center>Paku vähem</center>";
        }
    if (
$number==$arvas) {
            echo 
"<center>Panid täppi!!!</center>";
                         unset(
$_SESSION["arva"]);
    }
}
else {
    echo 
"<center>Kuskil on viga vist :S</center>";
}

?>
</body>
</html>
Big thanks to RobertK for help.
Maybe U can explane to me why thosent it change the number with this code:
PHP Code:
<?php
if (!isset($_COOKIE["arva"])) {
    
setcookie('arva',rand(1,20),0);
}
$arvas=intval($_COOKIE["arva"]);
?>
But those change every time in this code:

PHP Code:
$arva=(rand(1,20));
if (isset(
$arva)) {            //Kas genereeriti arv? Kui jh siis jätkame.
if ($number<$arva) {
echo 
"Vähe pakkusid";
    } 
Now I have no idea what do too next.

Last edited by karq : 01-23-2008 at 08:36 PM.
karq is offline  
Reply With Quote
Old 01-23-2008, 06:38 PM   #5 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

Sure.

What the script does, is it checks if a cookie named "arva" is set. If it is your first visit/guess with a new browser session (IE, the browser was closed before running the script) then it knows the cookie isn't there, and it sets "arva" to be a random number. It then pulls from the cookie the random number for the script's use.

The other one, your original one, always sets "arva" to be a random value. This value is never saved anywhere in your script. Thus, if you run it again you have a different number--in fact, your last answer could have been right this time.

That is the basic explanation of the two executions.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:11 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design