0

I want to redirect the user if $_GET['id'] is empty or if the user with that id is not found, can anyone provide a simple example?

Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
Norielle Cruz
  • 170
  • 5
  • 14
  • Have you tried any thing to achieve that? – Sougata Bose Mar 09 '16 at 12:39
  • `if (empty($_GET['id']) || $countUserWithThisID > 0) { header("Location: /index.php"); exit; }` -- this code has to be executed before any output, as that's a requirement for `header()` functions. – Qirel Mar 09 '16 at 12:42

5 Answers5

2

You are asking for both conditions to be fulfilled, but I think you can encapsulate all the checks in one function.

/**
 * Checks if a user id is valid and user exists
 * @param integer $id
 * @return boolean
 */               
public function validate_user($id){
    // always validate whatever input you get from users
    $valid_id = filter_var($id, FILTER_VALIDATE_INT);
    if ($valid_id){
        // check if the user_id is a valid user against your APP logic
        return USER::getUser($valid_id);
    }
    return false;
}


if(!validate_user($_GET['id'])){
    header('Location:home.php');
} 
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
1

You can use PHP's empty() to check if the it's empty.

if(empty($_GET['id']) {
    header("Location: page.php");
    exit;
} else
    // not empty
}

As from the literal meaning empty() means variable is empty. It's a BOOL function, which means that it will return true when empty and false when not empty.

Also, remember to use exit after header as the PHP script will not stop executing unless exit is stated.

Panda
  • 6,955
  • 6
  • 40
  • 55
1

I recommend you to use the function empty (documentation here) because it checks other cases, not only empty strings. If is a simple check, you don't need to use braces eather, put this at the beginning of your file and it should work.

if(empty($_GET['id'])) {
    header('Location:index.php');
    exit;
}
SpongePablo
  • 870
  • 10
  • 24
0

This is a simple example. For redirecting you can use the php 'header()'funciton. Hope this helps.

if($_GET['id']==''){
    header('Location:somepage.php');
    } 
DevMan
  • 538
  • 1
  • 5
  • 25
  • 2
    Using `empty()` is better, because you can get *Undefined index* with the code you have. Also, using `exit;` after `header("Location ...");` is a good idea. – Qirel Mar 09 '16 at 12:44
  • yes you're right that the empty function is better in most cases, but I wanted to show a very very simple way because from the question you can see that the questioner is a beginner so I tried to give him a simple and understandable answer. But thanks for your comment :) – DevMan Mar 09 '16 at 13:17
0

If it's empty you can use answer from DevMan.

And if there is no id in the database than you can set query, create variable $find = 0, and use while loop to go through the table and check if $_GET['id'] == $id(that's the id from the database).

If they match than $find++ if not it will stay 0.

if($find == 0){
   header('Location:somepage.php');
}

If you want both first check if it's empty(as DevMan posted) and than if there is that id in the database as i posted.

Dule
  • 19
  • 3