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

  • Home
  • SEARCH
  • 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 956157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:30:03+00:00 2026-05-16T00:30:03+00:00

When checking that variables passed via GET and POST are correct, I might have

  • 0

When checking that variables passed via GET and POST are correct, I might have something like this:

<?php
//Controller
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
  if(!isset($_POST['new_email']))
    header('Location: somepage.php');
  else if(empty($_POST['new_email']))
    //Report error to user and prompt to try again
  else
    $newEmail = $_POST['new_email'];

  if(!isset($_POST['full_name']))
    header('Location: somepage.php');
  else if(empty($_POST['full_name']))
    //Report error to user and prompt to try again
  else
    $newName = $_POST['full_name'];

  if(!isset($_POST['new_password_a']))
    header('Location: somepage.php');
  else if(empty($_POST['new_password_a']))
    //Report error to user and prompt to try again
  else
    $newPasswordA = $_POST['new_password_a'];

  if(!isset($_POST['new_password_b']))
    header('Location: somepage.php');
  else if(empty($_POST['new_password_b']))
    //Report error to user and prompt to try again
  else
    $newPasswordB = $_POST['new_password_b'];

  //Do some things with the variables
}
else
{
  header('Location: somepage.php');
}

//View
//Display relevant view here
?>

How would you check GET and POST variables in your PHP script? I wonder if there is a better way?

  • 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-05-16T00:30:04+00:00Added an answer on May 16, 2026 at 12:30 am

    Maybe creating a function to avoid the repeated code?

    function check($varname,$destination,$message) {
        if (!isset($_POST[$varname])) {
            header("Location: $destination");
        } else if (empty($_POST[$varname])) {
            //Do something with $message
        } else {
            return $_POST[$varname];
        }
        return NULL;
    }
    

    And then,

    <?php
    //Controller
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
      $newEmail = check('new_email','somepage.php','Error message');
      $newName = check('new_name','somepage.php','Error message');
      $newPasswordA = check('new_password_a','somepage.php','Error message');
      $newPasswordB = check('new_password_b','somepage.php','Error message');
    
      //Do some things with the variables
      //Checking for NULL values (although if some var was null, 
      //it should have either redirected or reported an error)
    }
    else
    {
      header('Location: somepage.php');
    }
    
    //View
    //Display relevant view here
    ?>
    

    What The Pixel Developer says is true though, you should sanitize the inputs at least against SQL injection (if you will use the data in a database) and CSRF attacks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that can have 1 to 4 variables passed to
I have a login screen that I force to be ssl, so like this:
I have a php page. It recieves a value for id via get. 2
How to remove using jquery html table row after checking checkbox that in this
We've noticed that when checking in updates, our .DFM files have added ExplicitWidth and
Checking few RDBMS I find that things like SELECT COUNT (a), SUM (b) FROM
I remember reading somewhere that function arguments get turned into private variables within the
I have a function that should be passed a string with two numbers (as
I have a javascript that loads the contents of a DIV when checking a
Missing something when declaring a DateTime object in PHP 5.3.8 I get a JSON

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.