oPg Gaming Forum
May 25, 2012, 10:36:39 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
Make payments with PayPal - it's fast, free and secure!
News: Stay tuned to opggaming.com for new features, modifications, and improvements.
 
   Home   opggaming Help Arcade Search Calendar stats SourceBans Login Register  


hd-gaming
Pages: [1] 2   Go Down
  Print  
Author Topic: Mah first PHP doey  (Read 1063 times)
0 Members and 1 Guest are viewing this topic.
Jedakiah
Hero Member
*****
Posts: 4632



WWW
« on: March 05, 2007, 04:01:14 PM »

I created a PHP calculator.  It's perty simple.  But it is the first thing I'vve done in PHP that I coded completely by myself. 

I believe however that if I were to put it on my site it would grant any hacker the ability to to delete my files n' stuff because the forms are unvalidated.  If that is the case, how may I fix this?  Obviously the forms only need to accept numbers...  how can I add that functionality?
Code:
<?php
    
if ($_REQUEST['multiply'] == "x") {
      
$result = ($_REQUEST['input'] * $_REQUEST['_input']);
  $action "x";
    }
    if (
$_REQUEST['divide'] == "/") {
  if ($_REQUEST['input'] == "0") {
    $result = ($_REQUEST['input']  / "1");
$zeroDivide "1";
  }
  else {
        
$result = ($_REQUEST['input'] / $_REQUEST['_input']);
    $action "/";
  }
    }
    if (
$_REQUEST['subtract'] == "-") {
      
$result = ($_REQUEST['input'] - $_REQUEST['_input']);
  $action "-";
    }
    if (
$_REQUEST['add'] == "+") {
      
$result = ($_REQUEST['input'] + $_REQUEST['_input']);
  $action "+";
    }
if ($_REQUEST['C'] == "C") {
      
$result "0.";
    }
?>

<form action="calc.php" method="post">
  <table width="230" border="0" cellspacing="0" cellpadding="0">
   
    <tr>
      <td colspan="2">Input something in fields 1 &amp; 2 to use.</td>
    </tr>
    <tr>
      <td width="200"><input name="input" type="text" class="calcfield" id="input" tabindex="1" value="<?php 
   
if ($result == "") {
   echo 
"0.";
   }
   else {
   echo 
"$result";
   }
?>
" maxlength="10" /></td>
      <td width="30">
        <input name="divide" type="submit" class="operbutton" id="divide" tabindex="4" value="/" />      </td>
    </tr>
    <tr>
      <td><input name="_input" type="text" class="calcfield" id="_input" tabindex="2" value="0." maxlength="10" /></td>
      <td><input name="multiply" type="submit" class="operbutton" id="multiply" tabindex="5" value="x" /></td>
    </tr>
    <tr>
      <td><input name="return" type="text" class="calcfield" id="return" tabindex="3" value="<?php
  
if ($zeroDivide == "1") {
    echo 
"You cannot divide by 0.";
  }
  else {
if ($result == "") {
  echo "Your equation equals 0.";
}
else {
  echo ($_REQUEST['input']);
  echo " $action ";
  echo ($_REQUEST['_input']);
  echo " = $result";
}
  }
?>
" />   </td>
      <td><input name="subtract" type="submit" class="operbutton" id="subtract" tabindex="6" value="-" /></td>
    </tr>
    <tr>

      <td><input name="Submit" type="submit" class="return" id="C" tabindex="8" value="C" /></td>
      <td><input name="add" type="submit" class="operbutton" id="add" tabindex="7" value="+" /></td>
    </tr>
  </table>
« Last Edit: March 05, 2007, 04:04:19 PM by Jedakiah » Logged

Kjuib
Sr. Member
****
Posts: 498


cube (kjuib) - n. a 3-Dimensional Square


WWW
« Reply #1 on: March 05, 2007, 06:11:49 PM »

I don't think you really have to worry about that. The biggest problem you would have with variables is if you use them to query a database. then you have to escape special characters so haxxors don't use code injection techniques.

call it good.
Logged

pre?venge (pri-venj') - v. - To inflict punishment in return for (injury or insult) before the (injury or insult) happens
Jedakiah
Hero Member
*****
Posts: 4632



WWW
« Reply #2 on: March 05, 2007, 08:27:23 PM »

Thank you your cubieness.  But, could you enlighten me on how to make them only accept numbers? 
Logged

Kjuib
Sr. Member
****
Posts: 498


cube (kjuib) - n. a 3-Dimensional Square


WWW
« Reply #3 on: March 05, 2007, 09:43:41 PM »

This should check to see if any digits are not numbers.
There is not a HTML method to limiting to numbers, but this should validate with the php once it is submitted.

Code:
<?php

$charArray 
explode(""$_REQUEST['input'])
$i=0;
$justNumbers true
while ($i strlen($_REQUEST['input'])) {
  if ((
$charArray[$i] < 0) || ($charArray[$i] > 9)) {
    
$justNumbers false;
  }
  
$i++;
}

?>

Logged

pre?venge (pri-venj') - v. - To inflict punishment in return for (injury or insult) before the (injury or insult) happens
Jedakiah
Hero Member
*****
Posts: 4632



WWW
« Reply #4 on: March 05, 2007, 10:12:35 PM »

Okay, my intent was just to ensure that submitted data would be limited to number-only execution.  I read about a very short PHP script that could be submitted through a form much like my'n that would delete the entire directory. I don't know if it is a security vulnerability with the latest versions of PHP though, but to be safe I thought I'd ask.  Thank you sir. 

Now for the main reason I showed you all that code, what would you do different?  Is there anything I did that I should have done different or somethings that I could optimize?  I'm incredibly new to this and welcome all feedback. 
Logged

Kjuib
Sr. Member
****
Posts: 498


cube (kjuib) - n. a 3-Dimensional Square


WWW
« Reply #5 on: March 06, 2007, 07:10:56 AM »

If I was writing the same application, here is what I would have done.
Code:
<?php

// Just call the $_POST or $_REQUEST once (I think this saves processing power
$input1 mysql_real_escape_string($_POST['input']);
$input2 mysql_real_escape_string($_POST['_input']);

// I think the POST should work, I do not know the difference from a POST to a REQUEST
if ($_POST['multiply'] == "x") {
  
$result = ($input1 $input2);
  
$action "x";
} else if (
$_POST['divide'] == "/") {
// Using 'else if' will speed up the code because it only has to check for the action until it finds it.

...

The rest looks just fine.
Logged

pre?venge (pri-venj') - v. - To inflict punishment in return for (injury or insult) before the (injury or insult) happens
Kjuib
Sr. Member
****
Posts: 498


cube (kjuib) - n. a 3-Dimensional Square


WWW
« Reply #6 on: March 06, 2007, 07:11:28 AM »

I read about a very short PHP script that could be submitted through a form much like my'n that would delete the entire directory.

What was the link. I would like to read that.
Logged

pre?venge (pri-venj') - v. - To inflict punishment in return for (injury or insult) before the (injury or insult) happens
Gidgidonihah
Seriously, what does it mean?
Moderator
Hero Member
*****
Posts: 2163



« Reply #7 on: March 12, 2007, 04:50:00 PM »

The difference between post and request is post only accepts variable from, well, post variables.
Request is either post or get.
Logged

Jedakiah
Hero Member
*****
Posts: 4632



WWW
« Reply #8 on: March 12, 2007, 08:24:14 PM »

What was the link. I would like to read that.
It was somewhere in here.

What is "else if"? 
Logged

Kjuib
Sr. Member
****
Posts: 498


cube (kjuib) - n. a 3-Dimensional Square


WWW
« Reply #9 on: March 12, 2007, 10:35:52 PM »

Code:
<?php

if (num 1) {

} else if (
num == 2) {

} else if (
num 3) {

}

This will just run a bit quicker then having 3 if statements. It only has to find the first on that matches then stops trying the ifs
Logged

pre?venge (pri-venj') - v. - To inflict punishment in return for (injury or insult) before the (injury or insult) happens
Latro
Guest
« Reply #10 on: March 13, 2007, 07:09:53 AM »

its like saying "or"  in code.
Logged
Kjuib
Sr. Member
****
Posts: 498


cube (kjuib) - n. a 3-Dimensional Square


WWW
« Reply #11 on: March 13, 2007, 09:31:00 AM »

bring it latro... you don't scare me... Angry
Logged

pre?venge (pri-venj') - v. - To inflict punishment in return for (injury or insult) before the (injury or insult) happens
Jedakiah
Hero Member
*****
Posts: 4632



WWW
« Reply #12 on: March 13, 2007, 11:32:36 AM »

Hey! Thank you sir. 
Logged

Gidgidonihah
Seriously, what does it mean?
Moderator
Hero Member
*****
Posts: 2163



« Reply #13 on: March 14, 2007, 04:56:24 PM »

I'm sorry.  I forgot to mention that $_REQUEST isn't just $_POST and $_GET, it's $_COOKIE, too!
Logged

ikonoclast
oPg Server Admins
Hero Member
****
Posts: 2983



« Reply #14 on: March 15, 2007, 12:37:32 AM »

I'm sorry.  I forgot to mention that $_REQUEST isn't just $_POST and $_GET, it's $_COOKIE, too!

whoopie dee doo.
Logged


All animals are equal, but some animals are more equal than others.
Pages: [1] 2   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC | Sitemap Valid XHTML 1.0! Valid CSS!


Google visited last this page May 07, 2012, 07:49:55 AM