Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

how to check for positive int with is_numeric

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";
}


?>
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.
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.
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.";
 }
 
          
(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.

Je t'aime
[Image: fpZ5a5g.png]

(Apr 22, 2022 04:39 PM)-Scrub Wrote:  Jokes on you, I'm not smart enough to know what he's talking about.
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" />
(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.

Je t'aime
[Image: fpZ5a5g.png]

(Apr 22, 2022 04:39 PM)-Scrub Wrote:  Jokes on you, I'm not smart enough to know what he's talking about.
PHP Code

<?php 
if(ctype_digit((string)$a) && $a!="0") 
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
@nachfolger
i say "first" not only.... with a check in html5 or javascript, the form wont be send for nothing.
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by PDGLOBALMar 28, 2018 01:15 AM 4 133 Mar 28, 2018 01:15 AM
Last Post: PDGLOBAL
[Javascript]  by KonstantinosMar 1, 2018 02:31 PM 4 155 Mar 1, 2018 02:31 PM
Last Post: Konstantinos
[PHP] by Nozemi™Dec 31, 2014 12:15 PM 6 169 Dec 31, 2014 12:15 PM
Last Post: Nozemi™