Thread: PHP Game Coding

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    PHP Game Coding


    Hi, all!

    I have a problem that I thought is very little, but unfortunately when I started to edit the index.php all the content crashed.

    I have to edit a game website in PHP. The game is situated on the page "Play". It contains 4 questions and when they are answered right, it redirects to a registration form where everyone can fill his data.

    The problem is that now I don't want these questions (in the code case 1, 2, 3 and 4) and what I want is when someone open page Play only the registration form (case 5) to be loaded. The filled data is sent to a database.

    When I remove some of the steps, the website crashes. When I remove one of the functions which check if the questions are answered, the button Send doesn't work, so now after 3 days work I'm really stuck.

    I'm beginner in PHP coding and I will be very thankful if you can help me.

    Here is the idex.php code:

    PHP Code:
    <?
    <?
    require_once(
    'config.php');

        
    $names='';
        
    $first='';
        
    $last='';
        
    $dob='';
        
    $email='';
        
    $phone='';
        
    $error=0;
        
    $message='';

        
    $subject='';
        
    $content='';

        if(!empty(
    $_POST['contact']) && (int)$_POST['contact']==1){
            
    $names=trim(strip_tags($_POST['names']));
            
    $email=trim(strip_tags($_POST['email']));
            
    $phone=trim(strip_tags($_POST['phone']));
            
    $subject=trim(strip_tags($_POST['subject']));
            
    $content=trim(strip_tags($_POST['content']));
            if(empty(
    $names) || empty($email) || empty($content)){
                
    $error=1;
                
    $message.='Please fill all the fields with *<br>';
            }
            elseif(!
    filter_var($email,FILTER_VALIDATE_EMAIL)){
                
    $error=1;
                
    $message.='The email is not valid.<br>';
            }
            if(!
    $error){
                
    $headers="From:".$email_from."\r\n";

                
    $body 'Name: '.$names."\n";
                
    $body .= 'email: '.$email."\n";
                
    $body .= 'telephone: '.$phone."\n";
                
    $body .= 'IP address: '.$_SERVER['REMOTE_ADDR']."\n\n";
                
    $body .= 'sent on: '.date('r')."\n\n";

                
    $body .= 'about:\n'.$subject."\n\n";
                
    $body .= 'message:\n'.$content."\n\n";

                
    $sent=mail($email_to,($subject?$subject:'subject not entered'), $body$headers);

                if(
    $sent){
                    
    header('Location: ?p=contacts&ok=1');
                    exit;
                }
                else {
                    
    $error=1;
                    
    $message.='An error has occurred. Please, try later.<br>';
                }
            }


        }


        if(!empty(
    $_POST['register']) && (int)$_POST['register']==1){
            
    $first=trim(strip_tags($_POST['first']));
            
    $last=trim(strip_tags($_POST['last']));
            
    $dob=trim(strip_tags($_POST['dob']));
            
    $email=trim(strip_tags($_POST['email']));
            
    $phone=trim(strip_tags($_POST['phone']));
            
    $subscribe=intval($_POST['subscribe']);

            if(empty(
    $first) || empty($last) || empty($dob) || empty($email) || empty($phone)){
                
    $error=1;
                
    $message.='Please, fill all the fields.<br>';
            }
            elseif(!
    filter_var($email,FILTER_VALIDATE_EMAIL)){
                
    $error=1;
                
    $message.='The email is wrong.<br>';
            }
            elseif(
    mysql_result(mysql_query('select count(*) from _participants where email ="'.mysql_real_escape_string($email).'"'),0,0)){
                
    $error=1;
                
    $message.='The email is already used.<br>';
            }
            if(!
    $error){

                
    $sql='insert into _participants
                    (created, first, last, bdate, email, phone, subscribe, ip)
                    values (
                        NOW(),
                        "'
    .mysql_real_escape_string($first).'",
                        "'
    .mysql_real_escape_string($last).'",
                        "'
    .mysql_real_escape_string($dob).'",
                        "'
    .mysql_real_escape_string($email).'",
                        "'
    .mysql_real_escape_string($phone).'",
                        '
    .$subscribe.',
                        INET_ATON("'
    .$_SERVER['REMOTE_ADDR'].'")
                        )'
    ;
                
    $q=mysql_query($sql);

                if(
    $q){
                    
    $_SESSION['step']=6;
                    
    header('Location: ?p=play&s='.rand());
                    exit;
                }
            }

        }


        
    $pages=array();
        
    $pages['index']='Home';
        
    $pages['terms']='Rules';
          
    $pages['play']='Play';
        
    $pages['awards']='Awards';
        
    $pages['contacts']='Contacts';

        
        
    $page='index';
        if(!empty(
    $_GET['p']) && isset($pages[$_GET['p']])){
            
    $page=$_GET['p'];
        }
    ?><!doctype html>
    <html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width" />
        <title>Website</title>
        <link rel="stylesheet" type="text/css" media="all" href="style.css" />
        <script type='text/javascript' src='js/jquery.min.js'></script>
        <script type='text/javascript' src='js/site.js'></script>
    </head>
    <body class=<?=$page?>>
    <div id=wrap>
        <div id=header>
            <div id=web_head>
                <div id=web_logo>
                    Website
                </div>
                <div id=web_slogan>
                    Slogan
                </div>
            </div>
            <div id=nav>
                <?
                    
    foreach($pages as $p=>$title){
                        
    ?>
                            <a href="<?=($p=='index'?'index.php':'?p='.$p)?><?=($p=='index'?'class="first'.($p==$page?' active':'').'"':($p==$page?'class=active':''))?>><?=$title?></a>
                        <?
                    
    }
                
    ?>
            </div>
        </div>
        <div id=main>

        <?
            
    switch($page){
                case 
    'play':
                    echo 
    page_play();
                    break;
                case 
    'terms':
                    echo 
    page_terms();
                    break;
                case 
    'awards':
                    echo 
    page_awards();
                break;
                case 
    'contacts':
                    echo 
    page_contacts();
                    break;
                case 
    'index':
                default:
                    echo 
    page_index();
                    break;
            }
        
    ?>

        </div>
        <div id=footer></div>
    </div>
    </body>
    </html>

    <?
        
    function check_step() {
            
    $step=(int)$_SESSION['step'];
            switch(
    $step){
                case 
    1:
                    if(!empty(
    $_POST['q1']) && intval($_POST['q1'])==2){
                        
    $_SESSION['step']=2;
                    }
                    break;
                case 
    2:
                    if(!empty(
    $_POST['q2']) && intval($_POST['q2'])>&& intval($_POST['q2'])<4){
                        
    $_SESSION['step']=3;
                    }
                    break;
                case 
    3:
                    if(!empty(
    $_POST['q3']) && intval($_POST['q3'])==3){
                        
    $_SESSION['step']=4;
                    }
                    break;
                case 
    4:
                    if(!empty(
    $_POST['q4']) && intval($_POST['q4'])==3){
                        
    $_SESSION['step']=5;
                    }
                    break;
            }
        }

        function 
    play_step() {
            global 
    $message$first$last$dob$email$phone$error;

            if(empty(
    $_SESSION['step'])){
                
    $_SESSION['step']=1;
            }
            if(!empty(
    $_POST)){
                
    check_step();
            }

            
    $step=(int)$_SESSION['step'];

            
    $r='';

            switch(
    $step){
                case 
    5:
                    
    $r.='
                        <div class=q id=success>
                            <h1>Congratulations!</h1>
                            <h2>You have answered all the 4 questions correct. Please, fill the registration form.</h2>
                            '
    .(!empty($message)?'<div class=message>'.$message.'</div>':'').'
                            <div class=fields>
                                <label for=first>Name: *</label><input type=text name=first value="'
    .str_replace('"','&quot;',$first).'"><br>
                                <label for=last>Family name: *</label><input type=text name=last value="'
    .str_replace('"','&quot;',$last).'"><br>
                                <label for=dob>Date of birth: *</label><input type=text name=dob value="'
    .str_replace('"','&quot;',$dob).'" placeholder="дд.мм.гггг"><br>
                                <label for=email>E-mail: *</label><input type=text name=email value="'
    .str_replace('"','&quot;',$email).'"><br>
                                <label for=phone>Telephone: *</label><input type=text name=phone value="'
    .str_replace('"','&quot;',$phone).'"><br>
                                <label class=long for=subscribe>Do you wish to send you news?<br>
                                from Web </label> <span class=radio_block><input type=radio value=0 name=subscribe '
    .( (isset($_POST['subscribe']) && (int)$_POST['subscribe']==0) || !isset($_POST['subscribe'])?' checked':'').'> No <input type=radio value=1 name=subscribe '.(isset($_POST['subscribe']) && (int)$_POST['subscribe']==1?' checked':'').'> Yes</span><br><br>

                                <label class=long>I agree with the Terms<a href="?p=terms" target="_blank">Terms</a></label><br><br><br>
                                <input type=hidden value=1 name=register>
                                <button>Send</button>
                            </div>
                        </div>
                    '
    ;
                    break;
                case 
    4:
                    
    $r.='
                        <div class=q id=q_4>
                            <h1>4. Question 4?</h1>
                            <label class="label_radio" for="q4"><input type=radio name=q4 value=1 data-check=3> A. Answer 1</label>
                            <label class="label_radio" for="q4"><input type=radio name=q4 value=2 data-check=3> B. Answer 2</label>
                            <label class="label_radio" for="q4"><input type=radio name=q4 value=3 data-check=3> C. Answer 3</label>
                            <div class=products></div>
                            <div class=comment id=q4_1 style="display:none">Wrong answer! </div>
                            <div class=comment id=q4_2 style="display:none">Wrong answer! </div>
                            <div class=comment id=q4_3 style="display:none">Correct answer! </div>
                        </div>
                    '
    ;
                    break;
                case 
    3:
                    
    $r.='
                        <div class=q id=q_3>
                            <h1>3. Qustion 3?</h1>
                            <label class="label_radio" for="q3"><input type=radio name=q3 value=1 data-check=3> A. Answer 1</label>
                            <label class="label_radio" for="q3"><input type=radio name=q3 value=2 data-check=3> B. Answer 2</label>
                            <label class="label_radio" for="q3"><input type=radio name=q3 value=3 data-check=3> C. Answer 3</label>
                            <div class=products></div>
                            <div class=comment id=q3_1 style="display:none">Wrong answer! </div>
                            <div class=comment id=q3_2 style="display:none">Wrong answer! </div>
                            <div class=comment id=q3_3 style="display:none">Correct answer!</div>
                        </div>
                    '
    ;
                    break;
                case 
    2:
                    
    $r.='
                        <div class=q id=q_2>
                            <h1>2. Question 2?</h1>
                            <label class="label_radio" for="q2"><input type=radio name=q2 value=1 data-check=1> A. Answer 1</label>
                            <label class="label_radio" for="q2"><input type=radio name=q2 value=2 data-check=2> B. Answer 2</label>
                            <label class="label_radio" for="q2"><input type=radio name=q2 value=3 data-check=3> C. Answer 3</label>
                            <div class=products></div>
                            <div class=comment id=q2_1 style="display:none">Wrong answer!</div>
                            <div class=comment id=q2_2 style="display:none">Wrong answer!</div>
                            <div class=comment id=q2_3 style="display:none">Correct answer!</div>
                        </div>
                    '
    ;
                    break;
                case 
    1:
                default:
                    
    $r.='
                        <div class=q id=q_1>
                            <h1>1. Question 1?</h1>
                            <table>
                                <tr>
                                    <td>
                                        <h2>A. Answer 1<br><br></h2>
                                        <label class="label_radio" for="q1"><input type=radio name=q1 value=1 data-check=2>&nbsp;</label>
                                    </td>
                                    <td>
                                        <h2>B. Answer 2</h2>
                                        <label class="label_radio" for="q2"><input type=radio name=q1 value=2 data-check=2>&nbsp;</label>
                                    </td>
                                </tr>
                            </table>
                            <div class=products></div>
                            <div class=comment id=q1_1 style="display:none">Wrong answer!</div>
                            <div class=comment id=q1_2 style="display:none">Correct answer!</div>
                        </div>
                    '
    ;
                    break;
            }

            return 
    $r;
        }


        function 
    page_play() {
            global 
    $message$first$last$dob$email$phone$error;



            if(isset(
    $_SESSION['step']) && $_SESSION['step']==6){
                unset(
    $_SESSION['step']);
                
    $r='
                    <div class=q id=success>
                        <h1>Thank you for your registration!</h1>
                        
                '
    ;
            }
            else {
                
    $r='<form id=player action="" method=post>';
                
    $r.=play_step();
                
    $r.='<div id=continue style="display:none"></div>';
                
    $r.='<div id=try_again class=play_btn style="display:none">try again</div>';
                
    $r.='</form>';
            }
            return 
    $r;
        }

        function 
    page_contacts() {
            global 
    $message$names$phone$email$subject$content;
            if(!empty(
    $_GET['ok'])){
                
    $r='<div><h1>Your message was sent successfully.</h1></div>';
                return 
    $r;
            }

            
    $r='';
            
    $r.='<div><h1>Contacts</h1>
            
            '
    .(!empty($message)?'<div class=message>'.$message.'</div>':'').'
            <form id=contact_form method=post>
                <div class=long>
                    <label for="names">Name: *</label>
                    <input type=text name=names value="'
    .str_replace('"','&quot;'$names).'">
                </div>
                <div class=short>
                    <label for="phone">Telephone: </label>
                    <input type=text name=phone value="'
    .str_replace('"','&quot;'$phone).'">
                </div>
                <div class="short last">
                    <label for="email">E-mail: *</label>
                    <input type=text name=email value="'
    .str_replace('"','&quot;'$email).'">
                </div>
                <div class=long>
                    <label for="subject">About: </label>
                    <input type=text name=subject value="'
    .str_replace('"','&quot;'$subject).'">
                </div>
                <div class=long>
                    <label for="message">Free text: *</label>
                    <textarea name=content>'
    .str_replace('"','&quot;'$content).'</textarea>
                </div>
                <input type=hidden value=1 name=contact>
                <button class=play_btn>Send</button>
            </form>

            </div>
            '
    ;

            return 
    $r;
        }

        function 
    page_awards() {
            
    $r='';
            
    $r.='<div><h1>Awards</h1>

    </div>'
    ;

            return 
    $r;
        }

        function 
    page_terms() {
            
    $r='';
            
    $r.='<div><h1>Rules/Terms</h1>
    Some rules.</div>'
    ;

            return 
    nl2br($r);
        }

        function 
    page_index() {


        
    $r='';
            
    $r.='<h1>Play</h1>';
            
    $r.='Answer all the 4 questions';
            
    $r.='<a href="?p=play" class=play_btn>play now</a>';

        
    /*$r='
    <div style="font-size:14px;text-align:left;padding:10px"><h1 style="font-size:18px;padding:25px 0">Winners
    </div>
    ';
    */



            
    return $r;
        }
    ?>
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    You have a issue with some of the text in step/case 5 In the function play_step().
    You are using the character ' in text which is started and ended with same character.

    If you want to use this character, you will have to escape the character: \'

    Example showing the difference with and without escaping the character:
    PHP Code:
    <? 
    $t 
    'text with escape \' character';
    ?>
    PHP Code:
    <? 
    $t 
    'text without escape ' character';
    ?>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by MrFujin
    You have a issue with some of the text in step/case 5 In the function play_step().
    You are using the character ' in text which is started and ended with same character.

    If you want to use this character, you will have to escape the character: \'

    Example showing the difference with and without escaping the character:
    PHP Code:
    <? 
    $t 
    'text with escape \' character';
    ?>
    PHP Code:
    <? 
    $t 
    'text without escape ' character';
    ?>
    Thank you MrFujin for your efforts but that's not the point. Actually the text is in another language (different from English) and there are no such characters in the original. I have just translated the text roughly to get the meaning.

    I want to know is there a way to bypass the check_step function and only the registration form (in the code this is case 5) in page Play to stay and to be filled (without all the questions).

    Thank you in advance!
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    If you want to by-pass a function, then remove (comment out) the call to it.
    You should also change the default step/case value to 5.

IMN logo majestic logo threadwatch logo seochat tools logo