Hack Forums
[PHP] how to check for positive int with is_numeric - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] how to check for positive int with is_numeric (/showthread.php?tid=5814054)



how to check for positive int with is_numeric - Lockheed - Mar 20, 2018

Basically, im trying to get is_numeric to return true/false, but it needs to only accept positive integers

So far it is returning true to positive integers and false to letters however its viewing -5 as an acceptable input. is_int() isn't what im looking for at this stage.

i have the following:
Cheers

PHP Code

<?php

$numero 
= "-5";
$isnumero = is_numeric($numero);

if (
$isnumero == 1 && (int($isnumero) >= 1)){
    echo 
"Numeric = True";
}else{
    echo 
"Numeric challenge = False";
}


?>



RE: How to destroy a session and then recreate? - m0noxide - Mar 20, 2018

PHP Code

<?php

$numero 
= "-5";
$isnumero = is_numeric($numero);

if (
$isnumero) {
    
$numero = (int)$numero;
    if (
$numero >= 1) {
        echo 
"Numeric = True";
    } else {
        echo 
"Numeric Challenge = False";
     }
} else {
    echo 
"Numeric challenge = False";
}


?>

First, you want to make sure that the string there "-5" is numeric
If it is, then convert it to an integer so we can properly check it.


RE: How to destroy a session and then recreate? - Willful - Mar 20, 2018

Here's what I would recommend trying if you don't decide to do some AJAX approach.

  1. On this page, set referrer in your session.

  2. On your home page, check for the referrer in your session



This way after you click onto your homepage, you should check if referrer is not equal to the current uri.
If they're not equal, you probably want to destroy the session.

PHP Code

<?php 
// use HTTP_REFERER, or HTTP_HOST and REQUEST_URI depending on how you want to look at your uri
$current_uri = $_SERVER['HTTP_REFERER']; 

if (!isset(
$_SESSION['referrer'])) {
 
   // store uri in session
 
   $_SESSION['referrer'] = $current_uri;

} else {
 
   $previous_uri = $_SESSION['referrer'];
 
   if ($previous_uri != $current_uri) {
 
       // destroy session

 
   }
} 

Hope this might help you move towards the right direction.


RE: how to check for positive int with is_numeric - Lockheed - Mar 21, 2018

Alright, im 99% complete i just need help with one last thing.
Ive now got is_numeric working however i cant seem to workout how to make a form only appear if is_numeric returns true. Ive put a comment in on the form im trying to achieve this with.

Thanks in advanced for your help, appreciate every bit of it.

PHP Code

<?php
 $current_uri 
= $_SERVER['HTTP_REFERER']; 

 if (!isset(
$_SESSION['referrer'])) {
 
    $_SESSION['referrer'] = $current_uri;
 } else {
 
    $previous_uri = $_SESSION['referrer'];
 
    if ($previous_uri != $current_uri) {
 
        unset($_SESSION['guesscount']);

 
   }
} 
 
       if (isset($_POST['number'])) {
 
           $_SESSION['maxbeans'] = $_POST['number'];
 
           $_SESSION['beans'] = rand(1, $_SESSION['maxbeans']);
 
$userguess = $_POST['number'];
 
            ?>

 <form action="" method="post">
 <p>Make your first guess <input name="guess" type="number"> <button type="submit" value="Guess!">Guess!</button><input type="hidden" name="step" value="2"></p>
 </form> // I want this form to only show if is_numeric() returns true below.

            <?php
        
}
 
       if (isset($_POST['guess'])) {
 
           $_SESSION['guesscount'] = isset($_SESSION['guesscount']) ? $_SESSION['guesscount']+1 : 1;
 
           $userguess = $_POST['guess'];
 
$isuserguess = is_numeric($userguess);
 if (
$isuserguess){
 
$userguess = (int)$userguess;
 if(
$userguess >= 0){
 echo 
"Valid number";
 } else {
 echo 
"Invalid number";
 }
 } else {
 echo 
"Not a number.";
 }
 
          



RE: how to check for positive int with is_numeric - Nachfolger - Mar 21, 2018

(Mar 21, 2018 05:05 AM)Lockheed Wrote:  Alright, im 99% complete i just need help with one last thing.
Ive now got is_numeric working however i cant seem to workout how to make a form only appear if is_numeric returns true. Ive put a comment in on the form im trying to achieve this with.

Thanks in advanced for your help, appreciate every bit of it.

PHP Code

<?php
 $current_uri 
= $_SERVER['HTTP_REFERER']; 

 if (!isset(
$_SESSION['referrer'])) {
 
    $_SESSION['referrer'] = $current_uri;
 } else {
 
    $previous_uri = $_SESSION['referrer'];
 
    if ($previous_uri != $current_uri) {
 
        unset($_SESSION['guesscount']);

 
   }
} 
 
       if (isset($_POST['number'])) {
 
           $_SESSION['maxbeans'] = $_POST['number'];
 
           $_SESSION['beans'] = rand(1, $_SESSION['maxbeans']);
 
$userguess = $_POST['number'];
 
            ?>

 <form action="" method="post">
 <p>Make your first guess <input name="guess" type="number"> <button type="submit" value="Guess!">Guess!</button><input type="hidden" name="step" value="2"></p>
 </form> // I want this form to only show if is_numeric() returns true below.

            <?php
        
}
 
       if (isset($_POST['guess'])) {
 
           $_SESSION['guesscount'] = isset($_SESSION['guesscount']) ? $_SESSION['guesscount']+1 : 1;
 
           $userguess = $_POST['guess'];
 
$isuserguess = is_numeric($userguess);
 if (
$isuserguess){
 
$userguess = (int)$userguess;
 if(
$userguess >= 0){
 echo 
"Valid number";
 } else {
 echo 
"Invalid number";
 }
 } else {
 echo 
"Not a number.";
 }
 
          
On topic of the -5 returning as an int: A int is not only positive numbers, it's also negative. (−2,147,483,648 to 2,147,483,647)

Back to your question, like this:
PHP Code

<?php if (is_numeric($number) && $number >= 1) { ?>
   // form goes here
<?php } ?>
Or like this:
PHP Code

<?php if (is_numeric($number) && $number >= 1) {
   echo 
" <form> ... </form> ";
}
?>
With the latter suggestion, you need to escape all doublequotes, which is why I like using the first suggestion.

Edit: Looking at your code, you're doing a lot of weird things. Why are you forcing the type onto $userguess? You should just strip the non-numeric characters, if they put "I42" by accident it will just return 0. Also you're also abusing If statements, you could honestly remove like half of those and combine them.


RE: how to check for positive int with is_numeric - .kr. - Mar 21, 2018

you test it in html5 at first

Code
<input type="number" min="0" step="1" style="display:none;"/>


or maybe like
Code
<input type="number" min="0" step="1" required="required" />



RE: how to check for positive int with is_numeric - Nachfolger - Mar 21, 2018

(Mar 21, 2018 05:35 AM).kr. Wrote:  you test it in html5 at first

Code
<input type="number" min="0" step="1" style="display:none;"/>


or maybe like
Code
<input type="number" min="0" step="1" required="required" />
Wrong. Relying on input constraints alone is retarded. All anyone would have to do is inspect element and literally remove them.


RE: how to check for positive int with is_numeric - Squito - Mar 21, 2018

PHP Code

<?php 
if(ctype_digit((string)$a) && $a!="0") 



RE: how to check for positive int with is_numeric - .kr. - Mar 22, 2018

@nachfolger
i say "first" not only.... with a check in html5 or javascript, the form wont be send for nothing.


RE: how to check for positive int with is_numeric - Charger - Mar 23, 2018

(Mar 22, 2018 12:27 PM).kr. Wrote:  @nachfolger
i say "first" not only.... with a check in html5 or javascript, the form wont be send for nothing.

Right, the same validation that occurs serverside should happen in the frontend as well.