Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If either GET or POST are empty, redirect

Tags:

redirect

php

Very strange problem and I'm scratching my head. If anyone can help it would be much appreciated, thanks.

I have a Search Location result page that should only appear if either 1) the form on the previous page has been submitted; or 2) they are searching via a url, e.g.

www.mywebsite.com?use_url=on&zipcode=UB95BX&radius=50.

So, at the top of the page I have the following code:

// Validate input and sanitize
if (empty($_POST['submit']) || empty($_GET['use_url']))
 {
  header('Location: index1.php');
  exit; 
 } 

Problem is, it's not working and it's redirecting EVERY request. Does anyone know how I can get it to work, so that if there is no submit post, or if there is no get request (basically, if the user types in www.mywebsite.com/locations.php directly into the url bar), the user is redirected.

Strange thing is this, if I leave out the empty($_GET) command and make it therefore

// Validate input and sanitize
if (empty($_POST['submit']))
 {
  header('Location: index1.php');
  exit; 
 } 

It works fine on an empty submit, the problems only occurs when I specify two criteria for a redirect.

Thanks everyone / Luke

like image 750
luke_mclachlan Avatar asked Mar 18 '23 23:03

luke_mclachlan


1 Answers

|| is logical OR. Currently your code reads "if either post or get are empty, redirect"

What im sure you mean is "if both post and get are empty, redirect".

You should use logical AND (&&):

if (empty($_POST['submit']) && empty($_GET['use_url']))
{
  header('Location: index1.php');
  exit; 
} 
like image 74
Steve Avatar answered Apr 02 '23 16:04

Steve