Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8365597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:41:51+00:00 2026-06-09T12:41:51+00:00

How can I quickly validate if a GET or POST variable in CodeIgniter is

  • 0

How can I quickly validate if a GET or POST variable in CodeIgniter is both set and numeric for use as error or status messages in views?

It is very tedious to do something like this each time each time I want to check variables:

if ($this->input->get('error', True)) {
    if (is_numeric($this->input->get('error', True))) {
        $data['error'] = $this->input->get('error', True);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:41:52+00:00Added an answer on June 9, 2026 at 12:41 pm

    get_numeric_input() for CodeIgniter

    mixed get_numeric_input ( string $name [, bool $required = True [, string $source = “GET” [, bool *$xss_clean* = True ]]] )

    Below is a function that I created because I was tired of checking if GET and POST variables existed and were numeric.

    This was mainly used when handling errors or status messages, because I could use redirect("original_page.php?error=1"); to pass an error to the original page. On the original page, I could simply do if (isset($error)) { … } and display a message depending on the value. However, it was necessary to check these variables before sending them to the view in the interest of security. This process proved to be quite repetitive and tedious.

    This function below is to be added to the bottom of wwwroot/application/system/core/Input.php

    It is to be used as follows:

    Example 1:

    function index() {
       if ($error = $this->input->get_numeric_input('error', True, "GET", True)) {
          $data['error'] = $error;
       }
    }
    

    In this example, if $_GET['error'] is both set and numeric, it will set $data['error'] to that value. If it is either not set and/or not numeric, it will terminate the script.

    Example 2:

    function index() {
       if ($error = $this->input->get_numeric_input('error', False, "POST", True)) {
          $data['error'] = $error;
       }
    }
    

    In this example, if $_POST['error'] is both set and numeric, it will set $data['error'] to that value. If it is either not set and/or not numeric, it will continue and not set any values in the $data array.

    The first argument is the variable name to be checked. The second variable is the boolean that makes the check required or not. If you have this set to TRUE, then if the variable is not set OR if it is not numeric, it will show an error and immediately terminate the script. If set to False, then it will will simply return False, and the script will move on. The third variable is either POST or GET, and will determine if the function looks for the variable in the $_GET or $_POST arrays. Finally, the fourth variable indicated whether or not the values will be XSS_CLEAN when returned.

    NOTE: Both the second, third, and fourth arguments are optional, and default to True, “GET,” and True, respectively.

    Here is the code:

    function get_numeric_input($name, $required = True, $source = "GET", $xss_clean = True) {
        if ($source === "GET") {
            if ($this->get($name, $xss_clean)) {
                if (is_numeric($this->get($name, $xss_clean))) {
                    return $this->get($name, $xss_clean);
                } else {
                    if ($required) {
                        show_error("$source variable $name is not numeric!");
                        log_message('error', "$source variable $name is not numeric!");
                        return False;
                    } else {
                        return False;
                    }
                }
            } else {
                if ($required) {
                    show_error("$source variable $name is not set!");
                    log_message('error', "$source variable $name is not set!");
                    return False;
                } else {
                    return False;
                }
            }
        } elseif ($source === "POST") {
            if ($this->post($name, $xss_clean)) {
                if (is_numeric($this->post($name, $xss_clean))) {
                    return $this->post($name, $xss_clean);
                } else {
                    if ($required) {
                        show_error("$source variable $name is not numeric!");
                        log_message('error', "$source variable $name is not numeric!");
                        return False;
                    } else {
                        return False;
                    }
                }
            } else {
                if ($required) {
                    show_error("$source variable $name is not set!");
                    log_message('error', "$source variable $name is not set!");
                    return False;
                } else {
                    return False;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I quickly validate if a string is alphabetic only, e.g var str
I'm trying to add a shortcut for mvn test so I can quickly get
using bsearch() in C (standard library) one can quickly find an entry in a
Is there a lazy way I can quickly replace all instances of a word
If my math is right, I can quickly generate a new hash value for
I made a Dictionary <string, string> collection so that I can quickly reference the
How can I quickly prove that the following class is not thread-safe (as it
How can I quickly create a List[Int] that has 1 to 100 in it?
How can I quickly find, given a folder with .Net assemblies, which of these
In PHP how can I quickly tell if all values in array are identical?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.